HMAC
A plain hash tells you whether data changed — but anyone can recompute it, so on its own it can’t tell you who sent something, or stop an attacker from swapping both a message and its hash. Add a shared secret to the hash and you get an HMAC: a seal that only someone holding the key could have produced.
The problem with a bare hash
Suppose I send you a message along with its hash so you can check it arrived intact. An attacker sitting in the middle can simply replace the message and recompute a fresh matching hash — and you would be none the wiser. A hash proves nothing about authenticity if the hash itself can be forged by anyone. What’s missing is a secret.
Hash + key = HMAC
An HMAC — Hash-based Message Authentication Code — mixes a secret key into the hashing process. Only someone who knows the key can produce the correct code, and only someone who knows the key can verify it. Change a single character of the message and the code breaks; without the key, you cannot forge a new one that fits. So an HMAC proves two things at once: the message hasn’t been altered, and it genuinely came from someone who holds the shared secret.
Where you meet it
HMACs quietly authenticate an enormous amount of the internet — securing web sessions, API requests and login tokens behind the scenes. Bitcoin uses one too: the standard that grows a single seed phrase into a whole tree of keys — the “HD wallet” you’ll meet later — is built on HMAC-SHA512. One backed-up phrase, an entire wallet, held together by a keyed hash.
The pattern is worth noticing: a hash on its own proves integrity; a hash plus a secret proves authenticity. Cryptography keeps doing this — taking a simple primitive and adding one more ingredient to unlock a new guarantee.
An HMAC needs a secret key. A salt needs a random value. A private key is a random number. Every one of them is only as strong as the randomness behind it — and good randomness is far harder than it looks. That’s next.
Key takeaways
- A bare hash proves data is unchanged, but anyone can recompute it — it can’t prove who sent it.
- An HMAC mixes in a secret key, so only someone with the key could have produced it: proof of both integrity and authenticity.
Check yourself
What does an HMAC add over a plain hash?
Hash + secret key = proof of both integrity and authenticity.
Why can’t a plain hash alone authenticate a message?
Without a secret, the hash can be forged along with the message.
Spotted an error or have feedback on this lesson? Suggest a correction ↗
Comments
Powered by Nostr — reply from any Nostr client, and zap the lesson over Lightning.