Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is HMAC?

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.

Illustration for the cybersecurity term HMAC
Home / Blog / Cybersecurity Terms / What Is HMAC?
17 November 2026

What Is HMAC?

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.

Why Not Simply Hash the Key and the Message?

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.

Where You Will Find It

Signature or MAC?

HMACDigital signature
Key modelOne shared secretPrivate key signs, public key verifies
SpeedMicrosecondsOrders of magnitude slower
Non-repudiationNo — either party could have produced itYes
Best forTwo systems you control, high volumePublic 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.

How Verification Goes Wrong

  1. Comparing with ==. 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.
  2. Verifying the wrong bytes. Webhook signatures cover the raw request body. A framework that parses JSON and re-serialises it before verification will produce a different byte sequence and either reject valid requests or, worse, be configured to skip the check.
  3. Forgetting replay protection. A valid message stays valid forever. Include a timestamp and a nonce in the signed data, reject anything outside a short window, and remember recent nonces.
  4. Weak or shared keys. HMAC keys should be random values of at least the hash output length. A key that is also a password, or shared across environments, is the usual root cause when webhook forgery succeeds.
  5. Truncating carelessly. Cutting a code to 32 bits to save space makes brute-force forgery practical. Keep at least 128 bits unless a specification says otherwise.

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.

FAQ – Frequently Asked Questions

Is HMAC Encryption?

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.

Is HMAC-SHA1 Still Safe?

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.

How Long Should an HMAC Key Be?

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.

Why Did My Webhook Signature Check Fail Intermittently?

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.

Related Terms

You May Be Interested In These

What Are Kerberos Ticket Lifetimes?

Cybersecurity Terms

17 November 2026

What Are Kerberos Ticket Lifetimes?

Read More
What Is a Security Level in IEC 62443?

Cybersecurity Terms

17 November 2026

What Is a Security Level in IEC 62443?

Read More
What Is Post-Quantum Cryptography?

Cybersecurity Terms

17 November 2026

What Is Post-Quantum Cryptography?

Read More