Someone sends you a message with a hash attached, and the hash checks out. What have you actually learned? Less than most people think. A hash proves the message in front of you matches the digest in front of you, but since anyone can compute a hash, an attacker who changed the message could simply compute a fresh one to match. The proof is circular.

An HMAC breaks that circle by mixing a shared secret key into the signature. This guide explains what that key changes, how verification really works, and where the boundary of the proof honestly sits. You can test every claim here with the free HMAC Generator on Ovatly, which signs in your browser using Web Crypto.

Contents

  • Quick answer
  • A plain hash proves less than you think
  • What the key changes
  • How to create an HMAC signature, step by step
  • Worked example: the published fox test value
  • Limitations and honest tradeoffs
  • Common mistakes
  • FAQs
  • Conclusion
  • References

Quick answer

An HMAC is a keyed hash: the sender signs a message by hashing it together with a shared secret key, and the receiver verifies by signing the received message again with the same key and comparing signatures. A match proves two things at once: the message was not changed, and it was signed by someone holding the key. It does not hide the message, because HMAC authenticates and never encrypts. To try it, open the HMAC Generator, paste a message, enter a key, pick SHA-256, and compute the signature in hex or Base64.

A plain hash proves less than you think

A hash function turns any input into a fixed fingerprint. If the message changes by even one character, the fingerprint changes completely, which makes hashes excellent at detecting accidental damage: a corrupted download, a truncated file, a copy error.

What a hash cannot detect is a deliberate, consistent lie. Suppose an attacker intercepts a message, rewrites it, and recomputes the hash of the rewritten text before forwarding both. Your check passes perfectly. The digest genuinely matches the message you received; it just also matches the attacker's version, because producing a correct hash requires nothing but the message, and the attacker has it.

So a plain hash answers the question "does this message match this digest?" and stays silent on the question you actually care about: "is this the message the sender sent?" Answering that requires something the attacker does not have. In HMAC, that something is a key.

What the key changes

An HMAC mixes a shared secret key into the hashing process, so the signature depends on two inputs: the message and the key. The sender and receiver both hold the key; nobody else does. Now the attacker's trick collapses. They can change the message, but they cannot produce the signature the new message should carry, because computing it requires the key. A forged message arrives with a missing or wrong signature, and verification exposes it.

This is also why verification looks the way it does. You cannot decrypt an HMAC or unwrap it to reveal the original message, because there is nothing wrapped. To verify, you sign the message you received, using the same key, and compare your signature with the one that arrived. Equal signatures mean the bytes and the key both matched.

One consequence follows from the shared secret: HMAC is symmetric. Anyone who can verify can also sign, because both operations use the same key. That makes HMAC right for two parties who trust each other, such as your server and a payment provider, and wrong for proving to a third party who signed something. That job belongs to public-key signatures, a different primitive.

How to create an HMAC signature, step by step

  1. Open the HMAC Generator and paste the exact message, byte for byte. Whitespace and line endings are part of the message: a trailing newline changes the signature completely.
  2. Enter the secret key. Treat it like a password, and never sign production secrets on a machine you do not trust, even though the signing itself happens on your device.
  3. Pick the hash: SHA-256, SHA-384 or SHA-512. Weaker options are deliberately absent; HMAC-SHA-1 and MD5 are not offered.
  4. Click Compute HMAC and copy the signature in the format the receiving system expects, hex or Base64. They are the same bytes in two encodings, so choose by the specification, not by preference.

Worked example: the published fox test value

HMAC has a standard test case that implementations are checked against. With the key "key" and the message "The quick brown fox jumps over the lazy dog", HMAC-SHA-256 must produce:

f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8

Run those exact inputs through the HMAC Generator and you will get that exact value, in hex, with a Base64 rendering of the same bytes alongside it. That reproducibility is the verification model in miniature: anyone holding the key who receives the fox sentence can sign it again, compare, and know it arrived untouched from a fellow key holder.

Now change one thing: add a trailing newline after "dog", as many editors and copy-paste operations silently do. The signature changes completely, even though the visible text looks identical. Nearly every real-world HMAC failure is this failure, a byte-level difference hiding inside text that appears the same.

Limitations and honest tradeoffs

The boundary first: authentication is not encryption. An HMAC travels next to a readable message and leaves it readable. If the content is sensitive, it needs encryption as a separate step; the signature only vouches for origin and integrity.

Second, the proof is only as strong as the key's secrecy. Because signing and verifying use the same key, every verifier is also a potential forger. Hand the key to too many systems and the claim "a key holder signed this" stops narrowing things down. A random key of 32 bytes or more is a sound default for HMAC-SHA-256, and it belongs in secret storage, not in source code.

Third, an HMAC proves a message matches what was signed, not that what was signed was sensible. If a system signs a payload after re-serialising it, the signature honestly covers the re-serialised bytes, and verifying against the raw bytes will fail. The primitive is exact; the discipline of signing the precise bytes that travel is yours to keep.

Common mistakes

  • Signing a re-serialised payload. If the data was parsed and written back out before signing, key order or spacing may have shifted. Sign the raw bytes that are actually sent.
  • Ignoring the trailing newline. Editors, terminals and copy-paste add them quietly. When a signature mismatches, compare bytes before suspecting keys.
  • Mixing up key encodings. A key stored as hex must be decoded to bytes before use. Feeding the hex text in as the key itself signs with the wrong key and produces a valid-looking, wrong signature.
  • Confusing hex and Base64 output. They encode the same signature. Sending one where the system expects the other reads as a total mismatch even though nothing is wrong.
  • Expecting encryption. Teams sometimes treat a signed message as a protected one. It is authenticated, not hidden, and anyone who intercepts it can read every word.

FAQs

What does an HMAC prove that a plain hash does not? A plain hash proves the message matches the digest, but anyone can recompute a hash, so it says nothing about who produced it. An HMAC mixes in a shared secret key, so a valid signature proves the message is unchanged and that whoever signed it held the key.

Does HMAC encrypt the message? No. HMAC authenticates a message; it does not hide it. The message travels in readable form next to its signature. If the content must be secret as well, it needs encryption, which is a separate step.

Why does my HMAC not match the one I expected? Almost always a byte-level difference: a trailing newline added to the message, a different text encoding, a key supplied as hex but read as plain text (or the reverse), or a payload re-serialised before signing instead of the raw bytes that were sent. Sign the exact bytes, byte for byte.

Can an HMAC be decrypted or reversed? No. There is nothing to decrypt: the output is a signature, not ciphertext. Verification works by signing the received message again with the same key and comparing the two signatures.

How long should an HMAC key be? For HMAC-SHA-256, a random key of 32 bytes or more is a sound default. The key must stay secret: anyone holding it can both sign and verify, because HMAC is a shared-secret scheme, not a public-key signature.

Conclusion

A plain hash proves a message matches itself. An HMAC proves the message matches and came from someone holding the shared secret, because the key is mixed into every signature. Keep the boundary in view: the message stays readable, and every verifier can also sign. The HMAC Generator reproduces the published test value above if you want to see the mechanism agree with the standard. The companion piece in this series, Salt Explained: Why Every bcrypt Hash Is Different, covers the randomness trick that protects stored passwords. For more guides in this series, see our Security and Privacy guides.

References