Network Security

a message authentication code

Imagine you and a friend share a secret word. Before sending any note, you stir the secret word into the note's text in an agreed way and stamp the result as a short tag at the bottom. Your friend, who knows the secret word, can recompute the tag from the note they received and check it matches. An attacker who alters the note can't produce a matching tag — they don't know the secret word. A message authentication code (MAC) is exactly this: a short tag, computed from the message and a shared secret key, that proves the message wasn't tampered with and came from someone who knows the key.

It fixes the weakness of a plain hash. A bare hash detects accidental change but not a deliberate attacker, who could just edit the message and recompute the hash. A MAC mixes a secret key into the computation, so only key-holders can produce a valid tag. The most common construction is HMAC, which combines a cryptographic hash like SHA-256 with the secret key in a specific, secure way (HMAC-SHA256). To send: compute tag = HMAC(key, message), send message plus tag. To verify: recompute the tag from the received message and your key, and accept only if it matches. Any change to the message, or anyone without the key, fails the check.

Why it matters: a MAC delivers integrity and authentication together, cheaply and fast, and it's everywhere — TLS uses MACs (or authenticated-encryption modes that include one) on every record so tampered or injected data is rejected. The honest limit is that a MAC uses a shared secret, so it can't prove to a third party who sent a message — both the sender and the receiver hold the same key, so either could have made the tag. That's exactly where a digital signature differs: it uses a private key only the sender has, giving non-repudiation that a MAC cannot.

An API request sends a body plus tag = HMAC-SHA256(shared_key, body). The server recomputes the tag from the body it received using the same key. If a proxy flipped one byte of the body, the recomputed tag won't match and the server rejects the request — tampering caught.

A keyed tag: only key-holders can make it, anyone with the key can check it.

A MAC proves integrity and that the sender knew the key, but not which of the two key-holders sent it — so it gives no non-repudiation. For that you need a digital signature, which uses a private key the sender alone holds.

Also called
MACHMACkeyed hash訊息鑑別碼金鑰雜湊