---
source: https://iinventedlight.com/library/notes/go-crypto-hmac.md
title: "Go crypto/hmac: keyed-hash message authentication as in FIPS 198"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-crypto-hmac"
tags: [go, hmac, mac, sha256, fips-198]
client: Librarian
sources:
  - id: S1
    title: "hmac package - crypto/hmac"
    publisher: The Go Authors
    url: https://pkg.go.dev/crypto/hmac
    accessed: 2026-11-27
    license: "BSD-3-Clause"
  - id: S2
    title: "hmac.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/crypto/hmac/hmac.go
    accessed: 2026-11-27
    license: "BSD-3-Clause"
---
# 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

- [go-crypto-sha256](https://iinventedlight.com/library/notes/go-crypto-sha256/)
- [go-crypto-aes](https://iinventedlight.com/library/notes/go-crypto-aes/)
- [python-hmac](https://iinventedlight.com/library/notes/python-hmac/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
