...
1
2
3
4
5 package hkdf_test
6
7 import (
8 "bytes"
9 "crypto/rand"
10 "crypto/sha256"
11 "fmt"
12 "io"
13
14 "golang.org/x/crypto/hkdf"
15 )
16
17
18
19 func Example_usage() {
20
21 hash := sha256.New
22
23
24 secret := []byte{0x00, 0x01, 0x02, 0x03}
25
26
27
28 salt := make([]byte, hash().Size())
29 if _, err := rand.Read(salt); err != nil {
30 panic(err)
31 }
32
33
34 info := []byte("hkdf example")
35
36
37 hkdf := hkdf.New(hash, secret, salt, info)
38
39 var keys [][]byte
40 for i := 0; i < 3; i++ {
41 key := make([]byte, 16)
42 if _, err := io.ReadFull(hkdf, key); err != nil {
43 panic(err)
44 }
45 keys = append(keys, key)
46 }
47
48 for i := range keys {
49 fmt.Printf("Key #%d: %v\n", i+1, !bytes.Equal(keys[i], make([]byte, 16)))
50 }
51
52
53
54
55
56 }
57
View as plain text