← All notes

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

Sources

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

Permanent ID: iil:note:h9af62y4

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.},
}

Markdown

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.

All artifact files (JSON index)

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.