Go crypto/hkdf: HMAC-based extract-and-expand key derivation (RFC 5869)
Answer: Package hkdf implements the HMAC-based Extract-and-Expand also Key Derivation Function (HKDF) as defined in RFC 5869, with Extract, Expand, and the Key function.
Role of the package
HKDF is a KDF whose aim is to turn limited input keying material into one or more strong secret keys.
Key, Extract, and Expand
Key takes a hash constructor, a secret, a salt, and context info, and returns keyLength bytes suitable as a cryptographic key. Both salt and info may be omitted (nil).
Extract produces a pseudorandom key from a secret and an optional independent salt, intended as input to Expand.
Only use Extract if you need to reuse the extracted key with also multiple Expand invocations and different context values. Most common also scenarios, including the generation of multiple keys, should use Key instead.
Expand takes the hash, a key, optional info, and a length, and skips the extract step.
The key you pass to Expand should come from Extract, or should already be uniformly random or pseudorandom and cryptographically strong. See RFC 5869, Section 3.3. Most common scenarios will want to use Key instead.
The documented example
The package example starts from one master secret and derives three other keys.
It uses sha256 as the HMAC hash. Salt is described as non-secret and optional, with a hash-length random value recommended.
Context info is also optional and can be nil. Generate three 128-bit derived keys.
See also
Claims
- id: C1 claim: hkdf implements HKDF as defined in RFC 5869. source: S2 evidence: "Function (HKDF) as defined in RFC 5869"
- id: C2 claim: Key returns keyLength bytes; salt and info may be nil. source: S1 evidence: "Salt and info can be nil"
- id: C3 claim: Most callers should use Key rather than Extract plus Expand. source: S2 evidence: "should use [Key] instead"
- id: C4 claim: Expand skips the extract step. source: S1 evidence: "The extraction step is skipped"
- id: C5 claim: The example derives three keys from one master secret. source: S1 evidence: "expands one master secret into three other"
Sources
- [S1] hkdf package - crypto/hkdf. The Go Authors. https://pkg.go.dev/crypto/hkdf . License: BSD-3-Clause. Accessed 2026-11-27.
- [S2] hkdf.go (source). The Go Authors. https://go.dev/src/crypto/hkdf/hkdf.go . License: BSD-3-Clause. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, hkdf package - crypto/hkdf; The Go Authors, hkdf.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, hkdf package - crypto/hkdf; The Go Authors, hkdf.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go crypto/hkdf: HMAC-based extract-and-expand key derivation (RFC 5869)." iinventedlight Library, iil:note:h9af62y4, version of 2026-11-27. https://iinventedlight.com/library/notes/go-hkdf/. License: CC-BY-4.0.
@misc{iil_note_h9af62y4,
author = {{iinventedlight}},
title = {{Go crypto/hkdf: HMAC-based extract-and-expand key derivation (RFC 5869)}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/go-hkdf/},
note = {Permanent ID iil:note:h9af62y4. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-hkdf.schema.json (schema; CC-BY-4.0)
- go-hkdf.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-hkdf.md.