HMAC

acronym

cryptography

Stands for: Hash-based Message Authentication Code

A way to prove a message is authentic and unaltered, using a hash and a shared secret.

Hash-based Message Authentication Code (RFC 2104) combines a secret key with the message so only holders of the key can produce or verify the tag. Unlike a bare hash, it protects against tampering by someone who cannot forge the key.

HMAC is the standard way to build a message authentication code out of a hash function, and it exists because the obvious approaches are broken. Simply prepending a key to the message and hashing it falls to length-extension attacks against Merkle-Damgard constructions like SHA-256, where an attacker who has a valid tag can compute a valid tag for a longer message without knowing the key.

The construction defeats that by hashing twice with two derived keys, an inner and an outer pass. The nesting means the attacker never has access to the internal state that length extension depends on. The details matter less than the principle: this is a case where the safe composition is not obvious, which is exactly why a standard exists and why nobody should invent their own.

Two practical notes. Comparing tags must be done in constant time, because an ordinary comparison returns early on the first differing byte and leaks how much of a forged tag was correct, which is enough to reconstruct it byte by byte. And HMAC underpins far more than it gets credit for: TOTP codes, JWT signatures using the HS family, AWS request signing, and webhook verification are all HMAC wearing different names.

Also known as: hmac

Sources

All glossary entries