crypto/hkdf: fix example to derive three different 128-bit keys

Change-Id: I039044500849f4579d5387cb63c5d38b921d141a
Reviewed-on: https://go-review.googlesource.com/c/go/+/775500
Auto-Submit: Neal Patel <nealpatel@google.com>
Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
Reviewed-by: Nicholas Husin <husin@google.com>
Reviewed-by: Nicholas Husin <nsh@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Neal Patel 2026-05-07 18:16:25 -04:00 committed by Gopher Robot
parent 2568174249
commit 11a3b27b91

View file

@ -10,6 +10,7 @@ import (
"crypto/rand"
"crypto/sha256"
"fmt"
"strconv"
)
// Usage example that expands one master secret into three other
@ -17,7 +18,7 @@ import (
func Example_usage() {
// Underlying hash function for HMAC.
hash := sha256.New
keyLen := hash().Size()
keyLen := hash().Size() / 2
// Cryptographically secure master secret.
secret := []byte{0x00, 0x01, 0x02, 0x03} // i.e. NOT this.
@ -34,8 +35,8 @@ func Example_usage() {
// Generate three 128-bit derived keys.
var keys [][]byte
for i := 0; i < 3; i++ {
key, err := hkdf.Key(hash, secret, salt, info, keyLen)
for i := range 3 {
key, err := hkdf.Key(hash, secret, salt, info+strconv.Itoa(i), keyLen)
if err != nil {
panic(err)
}