hkdf: improve example
Change-Id: I540c699baf1f7cbf27da458961d581773f442864 Reviewed-on: https://go-review.googlesource.com/c/144397 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Adam Langley <agl@golang.org>
This commit is contained in:
+23
-28
@@ -9,49 +9,44 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/crypto/hkdf"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/hkdf"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Usage example that expands one master key into three other cryptographically
|
// Usage example that expands one master secret into three other
|
||||||
// secure keys.
|
// cryptographically secure keys.
|
||||||
func Example_usage() {
|
func Example_usage() {
|
||||||
// Underlying hash function to use
|
// Underlying hash function for HMAC.
|
||||||
hash := sha256.New
|
hash := sha256.New
|
||||||
|
|
||||||
// Cryptographically secure master key.
|
// Cryptographically secure master secret.
|
||||||
master := []byte{0x00, 0x01, 0x02, 0x03} // i.e. NOT this.
|
secret := []byte{0x00, 0x01, 0x02, 0x03} // i.e. NOT this.
|
||||||
|
|
||||||
// Non secret salt, optional (can be nil)
|
// Non-secret salt, optional (can be nil).
|
||||||
// Recommended: hash-length sized random
|
// Recommended: hash-length random value.
|
||||||
salt := make([]byte, hash().Size())
|
salt := make([]byte, hash().Size())
|
||||||
n, err := io.ReadFull(rand.Reader, salt)
|
if _, err := rand.Read(salt); err != nil {
|
||||||
if n != len(salt) || err != nil {
|
panic(err)
|
||||||
fmt.Println("error:", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non secret context specific info, optional (can be nil).
|
// Non-secret context info, optional (can be nil).
|
||||||
// Note, independent from the master key.
|
info := []byte("hkdf example")
|
||||||
info := []byte{0x03, 0x14, 0x15, 0x92, 0x65}
|
|
||||||
|
|
||||||
// Create the key derivation function
|
// Generate three 128-bit derived keys.
|
||||||
hkdf := hkdf.New(hash, master, salt, info)
|
hkdf := hkdf.New(hash, secret, salt, info)
|
||||||
|
|
||||||
// Generate the required keys
|
var keys [][]byte
|
||||||
keys := make([][]byte, 3)
|
for i := 0; i < 3; i++ {
|
||||||
for i := 0; i < len(keys); i++ {
|
key := make([]byte, 16)
|
||||||
keys[i] = make([]byte, 24)
|
if _, err := io.ReadFull(hkdf, key); err != nil {
|
||||||
n, err := io.ReadFull(hkdf, keys[i])
|
panic(err)
|
||||||
if n != len(keys[i]) || err != nil {
|
|
||||||
fmt.Println("error:", err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keys should contain 192 bit random keys
|
for i := range keys {
|
||||||
for i := 1; i <= len(keys); i++ {
|
fmt.Printf("Key #%d: %v\n", i+1, !bytes.Equal(keys[i], make([]byte, 16)))
|
||||||
fmt.Printf("Key #%d: %v\n", i, !bytes.Equal(keys[i-1], make([]byte, 24)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
|
|||||||
Reference in New Issue
Block a user