hkdf.mx raw

   1  package hkdf
   2  
   3  import (
   4  	stdhkdf "crypto/hkdf"
   5  	"crypto/sha256"
   6  )
   7  
   8  // Extract computes HKDF-Extract(salt, ikm) using SHA-256.
   9  // Note: parameter order is (salt, ikm) matching musiquay convention;
  10  // stdlib expects (h, secret, salt) - mapping is applied internally.
  11  func Extract(salt, ikm []byte) (v [32]byte) {
  12  	if len(salt) == 0 {
  13  		salt = []byte{:32}
  14  	}
  15  	prk, _ := stdhkdf.Extract(sha256.New, ikm, salt)
  16  	var out [32]byte
  17  	copy(out[:], prk)
  18  	return out
  19  }
  20  
  21  // Expand derives length bytes of keying material from prk using SHA-256 HKDF-Expand.
  22  func Expand(prk, info []byte, length int32) (buf []byte) {
  23  	out, _ := stdhkdf.Expand(sha256.New, prk, info, length)
  24  	return out
  25  }
  26