What it means
A plain hash has no key, so anyone who changes a message can also recompute its hash. An HMAC mixes a secret key into the calculation. The sender computes a tag over the message and sends both. The receiver repeats the calculation with the same key and compares the two tags. A match shows the message was not changed and that someone holding the key made the tag. HMAC runs the hash twice, once over the key and message and once over the key and that first result. Because both sides hold the same key, either could have made the tag.
See the idea / Data
An HMAC tag fits one message and one shared key.
Follow a message from sender to receiver. Both compute a tag with the same secret key. Then change one digit of the message in transit.
Meridian’s control room sends a scheduling message to a relay station. Both hold the same secret key, K7. The message is pass=P-0412;az=141.5;el=32.0. The tag is computed with HMAC-SHA256. Tags are shown by their first eight hexadecimal digits. The text version gives them in full.
01 / 04
Share key
The control room and the relay both hold the secret key K7. Nothing has been sent.
More about this step
K7 is 32 random bytes that the two sides agreed on earlier over a protected channel. For this page only, a published demo key is used so that you can repeat the calculation. The demo key is the 32 characters meridian-relay-demo-key-K7-00001.
Step through at your own pace.
Keep in mindOne message, one key and SHA-256. Both sides hold the same key, so a valid tag cannot show which of them made it. That is why an HMAC cannot settle a dispute between them. The tag does not hide the message, and it does not stop an old message from being sent again. Sharing and storing K7 safely is assumed. The demo key is published here and must never protect anything.
Read the visual, step by step
Message unchanged
- Share key. The control room and the relay both hold the secret key K7. Nothing has been sent. K7 is 32 random bytes that the two sides agreed on earlier over a protected channel. For this page only, a published demo key is used so that you can repeat the calculation. The demo key is the 32 characters meridian-relay-demo-key-K7-00001.
- Tag. The control room computes HMAC-SHA256 over the message with K7. It sends the message with the tag, which starts 64d61571. The message is pass=P-0412;az=141.5;el=32.0. HMAC hashes twice. The inner hash covers the key mixed with one fixed pad, followed by the message. The outer hash covers the key mixed with a second fixed pad, followed by the inner result. The full tag is 64d61571c9cebbe4bd3f450013662431af6288e49ea9eee47cddca83b6ee9273.
- Recompute. The relay runs the same calculation over the message it received, with its own copy of K7. Its tag also starts 64d61571. The relay does not decrypt anything. It repeats the sender’s calculation on the bytes that arrived. The same key and the same bytes always give the same tag.
- Compare. The two tags are equal, so the relay accepts the message. It arrived unchanged, from someone who holds K7. The relay compares all 64 hexadecimal digits, in a way that takes the same time whether or not they match. A match shows integrity and that a key holder made the tag. It does not show which key holder.
One digit changed
- Share key. The control room and the relay both hold the secret key K7. Nothing has been sent. K7 is 32 random bytes that the two sides agreed on earlier over a protected channel. For this page only, a published demo key is used so that you can repeat the calculation. The demo key is the 32 characters meridian-relay-demo-key-K7-00001.
- Tag. The control room computes HMAC-SHA256 over the message with K7. It sends the message with the tag, which starts 64d61571. The message is pass=P-0412;az=141.5;el=32.0. HMAC hashes twice. The inner hash covers the key mixed with one fixed pad, followed by the message. The outer hash covers the key mixed with a second fixed pad, followed by the inner result. The full tag is 64d61571c9cebbe4bd3f450013662431af6288e49ea9eee47cddca83b6ee9273.
- Alter. In transit, someone changes az=141.5 to az=147.5. They do not hold K7, so the old tag still travels with the changed message. Without K7 there is no way to compute the tag that fits the new message. The attacker can change the message, the tag or both, but cannot make them agree.
- Reject. The relay recomputes the tag over az=147.5 and gets one that starts 9aca754b. It does not match 64d61571, so the relay rejects the message. The full recomputed tag is 9aca754b4dccdd49c0e724318e1d73dc8931b4a2cf46b616921f69d12da05aa8. One changed character gives a completely different tag. The relay cannot tell what was changed or by whom. It knows only that this message and this tag do not belong together under K7.
One message, one key and SHA-256. Both sides hold the same key, so a valid tag cannot show which of them made it. That is why an HMAC cannot settle a dispute between them. The tag does not hide the message, and it does not stop an old message from being sent again. Sharing and storing K7 safely is assumed. The demo key is published here and must never protect anything.
Flow references: RFC 2104: HMAC, keyed hashing for message authentication · NIST FIPS 198-1: The Keyed-Hash Message Authentication Code
AN ILLUSTRATIVE SCENARIO
One changed digit, a different tag
Meridian's control room sends a relay the message pass=P-0412;az=141.5;el=32.0 with an HMAC-SHA256 tag that starts 64d61571. Both hold key K7. In transit someone changes 141.5 to 147.5. The relay recomputes the tag over what arrived and gets one starting 9aca754b. The tags differ, so the relay rejects the message. Without K7 the attacker could not make a matching tag.
Put it to work
- Use HMAC from a maintained cryptographic library with SHA-256 or stronger. Do not build your own keyed hash by joining a key to a message.
- Generate the key from a cryptographic random source. Make it at least as long as the hash output, and store it as you would any secret.
- Compare tags with the library's constant-time function, and add a counter or timestamp inside the message so an old message cannot be sent again.
How to check your work
Write three tests. An unchanged message is accepted. A message with one changed byte is rejected. A correct message with a tag made under a different key is rejected. Then confirm a replayed message is refused by your counter check.
Connect the ideas
- Hashing
A one-way fingerprint of data used to detect change or store verifiers, not a reversible way to hide records you must later read.
- Integrity
The property that data and processes remain complete and unaltered except by authorized action.
- Non-repudiation
Evidence supporting a claim that a particular party originated or approved something, so a later denial can be evaluated. Reliable identity binding and record integrity matter.
- JWT
A compact token that carries claims as JSON. The usual signed form has three base64url parts: header, claims, and signature. A service must verify the signature and check the claims before trusting them.