▾ Example (Usage)
Usage example that expands one master key into three other cryptographically
secure keys.
Code:
hash := sha256.New
master := []byte{0x00, 0x01, 0x02, 0x03}
salt := make([]byte, hash().Size())
n, err := io.ReadFull(rand.Reader, salt)
if n != len(salt) || err != nil {
fmt.Println("error:", err)
return
}
info := []byte{0x03, 0x14, 0x15, 0x92, 0x65}
hkdf := hkdf.New(hash, master, salt, info)
keys := make([][]byte, 3)
for i := 0; i < len(keys); i++ {
keys[i] = make([]byte, 24)
n, err := io.ReadFull(hkdf, keys[i])
if n != len(keys[i]) || err != nil {
fmt.Println("error:", err)
return
}
}
for i := 1; i <= len(keys); i++ {
fmt.Printf("Key #%d: %v\n", i, !bytes.Equal(keys[i-1], make([]byte, 24)))
}
Output:
Key #1: true
Key #2: true
Key #3: true