Go crypto/hmac: keyed-hash message authentication as in FIPS 198
Answer: Package hmac implements the keyed-hash message authentication code defined in FIPS Publication 198.
What an HMAC is
HMAC signs a message by hashing it under a secret key.
The receiver checks the hash by recomputing it with the same key.
Receivers should compare MACs with Equal so that the comparison does not leak timing information.
New and Equal
New returns a new HMAC hash using the given hash type and key.
A New function like the one in crypto/sha256 can be used as h.
Each call of h must return a fresh Hash.
Unlike many other hash types in the standard library, the Hash that New returns does not implement encoding.BinaryMarshaler or encoding.BinaryUnmarshaler.
Equal compares two MAC slices for equality without leaking timing information.
See also
Claims
- id: C1 claim: Compare MACs with Equal to avoid timing side channels. source: S1 evidence: "Receivers should be careful to use Equal to compare MACs"
- id: C2 claim: Equal compares two MACs without leaking timing information. source: S1 evidence: "Equal compares two MACs for equality without leaking timing"
- id: C3 claim: New builds an HMAC hash from a hash constructor and a key. source: S2 evidence: "New returns a new HMAC hash using the given"
Sources
- [S1] hmac package - crypto/hmac. The Go Authors. https://pkg.go.dev/crypto/hmac . License: BSD-3-Clause. Accessed 2026-11-27.
- [S2] hmac.go (source). The Go Authors. https://go.dev/src/crypto/hmac/hmac.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, hmac package - crypto/hmac; The Go Authors, hmac.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, hmac package - crypto/hmac; The Go Authors, hmac.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go crypto/hmac: keyed-hash message authentication as in FIPS 198." iinventedlight Library, iil:note:pj8afrhc, version of 2026-11-27. https://iinventedlight.com/library/notes/go-crypto-hmac/. License: CC-BY-4.0.
@misc{iil_note_pj8afrhc,
author = {{iinventedlight}},
title = {{Go crypto/hmac: keyed-hash message authentication as in FIPS 198}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/go-crypto-hmac/},
note = {Permanent ID iil:note:pj8afrhc. 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-crypto-hmac.schema.json (schema; CC-BY-4.0)
- go-crypto-hmac.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-crypto-hmac.md.