git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/kdf/hkdf.go (about)

     1  package kdf
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"errors"
     6  	"io"
     7  
     8  	"golang.org/x/crypto/hkdf"
     9  )
    10  
    11  func HkdfSha256(secret, info, salt []byte, size int) (key []byte, err error) {
    12  	if size < 1 {
    13  		err = errors.New("hkdf: size is not valid")
    14  		return
    15  	}
    16  	if secret == nil {
    17  		err = errors.New("hkdf: size can't be null")
    18  		return
    19  	}
    20  	if info == nil {
    21  		err = errors.New("hkdf: info can't be null")
    22  		return
    23  	}
    24  
    25  	key = make([]byte, size)
    26  	hkdf := hkdf.New(sha256.New, secret, salt, info)
    27  	_, err = io.ReadFull(hkdf, key)
    28  	return
    29  }