Use secure random, hash, HMAC and AEAD
Use secure random, hash, HMAC and AEAD
Choose a cryptographic primitive for its actual purpose. crypto/rand supplies security-sensitive randomness. A SHA-256 digest detects changes against a trusted expected digest but does not authenticate data by itself. HMAC authenticates bytes using a shared secret; use hmac.Equal to verify tags. Neither a plain hash nor HMAC encrypts data. Password storage requires a dedicated password-hashing scheme and reviewed parameters, not SHA-256 alone.
package main
import ( "crypto/hmac" crand "crypto/rand" "crypto/sha256" "fmt")
func tag(key, msg []byte) []byte { h := hmac.New(sha256.New, key); h.Write(msg); return h.Sum(nil) }func main() { key := make([]byte, 32) if _, e := crand.Read(key); e != nil { panic(e) } msg := []byte("lab") sum := sha256.Sum256(msg) fmt.Println(len(sum), hmac.Equal(tag(key, msg), tag(key, msg)), hmac.Equal(tag(key, msg), tag(key, []byte("other"))))}Random bytes are generated locally and never printed. HMAC comparison checks equality without ordinary early-exit comparison on equal-length tags. Ensure the verifier uses the intended key and canonical message representation. Use reviewed protocols that define key distribution and replay protection; HMAC alone does not prevent replay.
Expected output:
32 true falseAuthenticated encryption with AES-GCM
package main
import ( "crypto/aes" "crypto/cipher" crand "crypto/rand" "fmt")
func fresh(n int) []byte { b := make([]byte, n) if _, e := crand.Read(b); e != nil { panic(e) } return b}func cipherBox() cipher.AEAD { b, e := aes.NewCipher(fresh(32)) if e != nil { panic(e) } g, e := cipher.NewGCM(b) if e != nil { panic(e) } return g}func main() { g := cipherBox() nonce := fresh(g.NonceSize()) aad := []byte("lab-v1") sealed := g.Seal(nil, nonce, []byte("note"), aad) plain, e := g.Open(nil, nonce, sealed, aad) fmt.Println(string(plain), e == nil) _, e = g.Open(nil, nonce, sealed, []byte("other")) fmt.Println(e != nil)}AEAD provides confidentiality and integrity with optional authenticated but unencrypted associated data. Use a unique nonce for each encryption under a key; never reuse it. This fixture uses a fresh key for a single message; real random-nonce GCM usage requires a per-key message bound and key lifecycle (Go docs specify no more than 2^32 random nonces per key). Preserve the nonce with the ciphertext and reject authentication failures without using plaintext. Do not design a custom protocol; use reviewed key management and established formats.
Expected output:
note truetrueTry it locally
Encrypt a synthetic message and verify that changing a ciphertext byte makes Open fail.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Security engineering 07: Use secure random, hash, HMAC and AEAD
Check the lesson objective before moving on.
Which primitive authenticates data with a shared key without encrypting it?
What must never repeat under the same GCM key?