What Is HMAC?

Run a Speed Test

A hash can tell you whether a file changed — but it cannot tell you who changed it, and that gap is fatal against a real attacker. If anyone can compute a hash, anyone can alter a message and attach a fresh, matching hash, and the check passes as if nothing happened. HMAC closes that gap by folding a secret key into the hash, turning a flimsy "did this change?" into a robust "is this genuine, and did it come from someone who knows the secret?"

The Weakness of a Plain Hash

A hash function like SHA-256 turns any input into a fixed-length fingerprint, and the same input always produces the same fingerprint. That is useful for detecting accidental change: hash a file before and after transfer, compare the two, and a mismatch reveals corruption.

But against a deliberate attacker it falls apart, because a plain hash needs no secret — anyone can compute it. Picture a message sent with its hash attached for verification. An attacker who intercepts it can rewrite the message, compute a brand-new hash of the altered version, attach that, and pass it along. The recipient hashes what arrived, it matches the attached hash perfectly, and the tampering goes undetected. The hash verified integrity against accidents but offered zero protection against someone who controls the message. Something the attacker cannot reproduce is needed — and that something is a secret key.

How HMAC Adds the Key

HMAC — Hash-based Message Authentication Code — produces a tag by combining the message, a cryptographic hash function, and a secret key shared between sender and recipient. Conceptually:

tag = HMAC( secret_key, message )

The internal construction mixes the key into the message in a specific, carefully designed way — hashing the message together with the key, then hashing the result again with the key, using inner and outer padding. The double-hashing structure is not arbitrary; it is what makes HMAC secure even with hash functions that have certain weaknesses, and it is why you use HMAC rather than naively gluing a key onto a message and hashing it. The practical upshot is simple: only someone who knows the secret key can produce a tag that verifies, and the same person can detect any change to the message.

Verification mirrors creation. The recipient, who also holds the secret key, recomputes the HMAC over the message it received and compares it to the tag that arrived. If they match, two facts are established at once.

Two Guarantees in One Tag

A valid HMAC proves both of the following, which is what makes it so useful:

GuaranteeWhat it means
IntegrityThe message was not altered in transit — any change, even one bit, produces a completely different tag
AuthenticityThe message came from someone who holds the secret key — an attacker without it cannot forge a valid tag

This is the leap beyond a plain hash. The attacker from earlier is now stuck: they can still alter the message, but they cannot compute a matching tag without the key, so the recipient's recomputed HMAC will not match and the forgery is caught. The names you will see, like HMAC-SHA256, simply state which hash function is used inside — SHA-256 here.

One Important Caveat: HMAC Is Not Encryption

It is worth being explicit, because it trips people up: HMAC does not hide the message. The data travels in the clear; the tag sits alongside it. HMAC answers "was this tampered with, and is it authentic?" — not "can someone read this?" If you need secrecy as well, you combine encryption with HMAC, or better, use an authenticated encryption mode such as AES-GCM that builds both protections into one operation. HMAC is the integrity-and-authenticity tool, not the confidentiality tool. It is also distinct from a digital signature: a signature uses a public/private key pair so anyone can verify without being able to forge, whereas HMAC uses one shared secret, so anyone who can verify can also create — which is fine when both parties are trusted and is far faster.

Where HMAC Shows Up

Once you know the shape of it, you see HMAC everywhere two parties share a secret and need to trust what passes between them:

  • API request signing — a client computes an HMAC over the request using a shared secret, and the server recomputes it to confirm the request is genuine and unmodified, without sending the secret itself.
  • Session tokens and cookies — a server can attach an HMAC to a cookie so it can detect if the client tampered with the value it was given.
  • Webhooks — a service signs the payload it sends so the receiver can verify it really came from that service and was not spoofed.
  • Inside protocols — HMAC provides the integrity check in many security protocols, including older TLS cipher modes and numerous authentication schemes.

The common thread is a shared secret and a need for confidence. Wherever a system must be sure that a message is both untouched and from the expected sender — and the two sides can hold a key in common — HMAC is usually the quiet mechanism doing that work.

Frequently Asked Questions

What is HMAC?

Hash-based Message Authentication Code — a way to produce a short tag from a message using a hash function and a secret key. The tag proves the message was not altered and that it came from someone holding the shared key. Without the key, you can neither forge nor verify a valid tag.

How is HMAC different from a regular hash?

A plain hash needs no key, so anyone can recompute one — letting an attacker alter a message and attach a matching hash. HMAC mixes in a secret key, so only parties who know it can produce or verify a valid tag. A plain hash catches accidental change; HMAC proves integrity and authenticity against a deliberate attacker.

What is HMAC used for?

Verifying that a message is genuine and unmodified between parties sharing a secret: signing API requests, protecting session tokens and cookies from tampering, verifying webhook payloads, and providing the integrity check inside many security protocols including TLS.

Is HMAC encryption?

No. HMAC does not hide the message — the data stays readable. It only produces a tag that detects tampering and proves the sender knew the secret key. For secrecy and integrity together, combine encryption with HMAC or use an authenticated mode like AES-GCM.

Why does HMAC use a secret key instead of just a hash?

Because a hash alone offers no protection against an attacker who controls the message — anyone can compute a plain hash and attach a fresh valid-looking one. The secret key is what the attacker lacks, so they cannot generate a tag that verifies. The key turns an integrity check into proof of authenticity.

Related Guides

More From This Section