1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 "golang.org/x/crypto/hkdf"
17 // Usage example that expands one master secret into three other
18 // cryptographically secure keys.
19 func Example_usage() {
20 // Underlying hash function for HMAC.
23 // Cryptographically secure master secret.
24 secret := []byte{0x00, 0x01, 0x02, 0x03} // i.e. NOT this.
26 // Non-secret salt, optional (can be nil).
27 // Recommended: hash-length random value.
28 salt := make([]byte, hash().Size())
29 if _, err := rand.Read(salt); err != nil {
33 // Non-secret context info, optional (can be nil).
34 info := []byte("hkdf example")
36 // Generate three 128-bit derived keys.
37 hkdf := hkdf.New(hash, secret, salt, info)
40 for i := 0; i < 3; i++ {
41 key := make([]byte, 16)
42 if _, err := io.ReadFull(hkdf, key); err != nil {
45 keys = append(keys, key)
49 fmt.Printf("Key #%d: %v\n", i+1, !bytes.Equal(keys[i], make([]byte, 16)))