Skip to content
ecdsa.com

HMAC

The keyed-hash message authentication code of RFC 2104 and FIPS 198-1, computed as H((K ⊕ opad) ‖ H((K ⊕ ipad) ‖ m)), which proves integrity and origin to anyone who shares the secret key.
Also written asHMAC-SHA256keyed-hash message authentication codeHMAC-SHA1

HMAC works with any hash (HMAC-SHA256 gives a 32-byte tag) and stays secure even with hashes whose collision resistance is broken, which is why HMAC-SHA1 still appears in TOTP. Keys longer than the hash's block size (64 bytes for SHA-256) are first hashed, keys shorter are zero-padded; a key should have at least as many bits of entropy as the security level wanted. RFC 7518 §3.2 requires HS256 keys of at least 256 bits.

Uses: JWT HS256/HS384/HS512, webhook signatures (for example GitHub's X-Hub-Signature-256 and Stripe's Stripe-Signature), AWS Signature Version 4 request signing, HKDF and PBKDF2 internals, TLS 1.2 record MACs and TOTP/HOTP codes. Compare tags with a constant-time function.

An HMAC is not a signature: every verifier holds the same key and could have produced the tag, so it gives no non-repudiation and does not work when verifiers should not be able to issue tokens. The HMAC calculator computes and checks tags locally.

Standards and references

All terms