Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
HMAC combines a secret key with a hash function to produce a message authentication code, proving that a message came from someone holding the key and has not been altered in transit.

What Is HMAC?
A plain hash tells you that data has not changed, provided you obtained the hash from somewhere the attacker cannot touch. On a network, that proviso fails immediately: anyone who can modify a message can recompute its hash. HMAC closes the gap by mixing a secret key into the hash, so that only parties holding the key can produce or verify the result.
It was specified in RFC 2104 in 1997 and is defined for any hash function — HMAC-SHA256 is the usual form today, and HMAC-SHA1 survives in older protocols. The output proves two things at once: integrity, because any change alters the code, and authenticity, because computing a valid code requires the key.
The obvious construction, SHA256(key || message), is broken against the hash families built on the Merkle–Damgård design, which includes MD5, SHA-1, and SHA-2. These functions expose their internal state in the output, so an attacker who has seen one valid digest can continue hashing from that state and produce a valid digest for message || extra without knowing the key. This is the length extension attack, and it has compromised real APIs that signed requests this way.
HMAC avoids it with a nested structure: the key is padded and combined with two different constants, the inner hash runs over one version and the message, and the outer hash runs over the other version and the inner result. The attacker never sees the internal state of the outer hash, so the extension trick fails. SHA-3 and BLAKE2 are not vulnerable to length extension and offer their own keyed modes, such as KMAC, but HMAC remains the interoperable default.
| HMAC | Digital signature | |
|---|---|---|
| Key model | One shared secret | Private key signs, public key verifies |
| Speed | Microseconds | Orders of magnitude slower |
| Non-repudiation | No — either party could have produced it | Yes |
| Best for | Two systems you control, high volume | Public verification, legal weight, software distribution |
That third row decides most architectural arguments. If you need to prove to a third party that a specific sender produced a message, HMAC cannot do it, because the verifier could have created the same code. Use a digital signature instead.
==. A byte-by-byte comparison that returns early leaks, through timing, how many leading bytes matched, allowing an attacker to recover a valid code one byte at a time. Use the constant-time comparison function your platform provides, such as hmac.compare_digest in Python or crypto.timingSafeEqual in Node.js.None of these is exotic, and all of them appear regularly in integration code we review during our web application security services, usually in the handler that receives a payment or provisioning callback.
No. HMAC produces an authentication code alongside a message that remains readable to anyone. It proves the message is unmodified and came from a key holder, but hides nothing. If you need confidentiality as well, encrypt with an authenticated mode such as AES-GCM, which provides both in one operation.
In practice yes, because the collision attacks that broke SHA-1 for signatures do not translate into HMAC forgery, and protocols such as TOTP still rely on it. It is nonetheless a legacy choice: new designs should use HMAC-SHA256, and any system still using HMAC-MD5 or HMAC-SHA1 should have a migration path recorded.
Generate a random key at least as long as the hash output, so 32 bytes for HMAC-SHA256, from a cryptographically secure random source. Longer keys are hashed down and gain nothing. Never derive the key from a password or reuse it across environments, and store it in a secrets manager rather than in configuration files.
Almost always because the body being verified is not the body that was signed. Middleware that parses, re-encodes, or pretty-prints JSON changes the bytes, as does a proxy altering whitespace or character encoding. Capture the raw body before any parsing, verify against that, and only then deserialise.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.