HASHES, MACS & SIGNATURES

Integrity, authenticity, and who can prove what

L115 MINSECURITY
PREDICT

An API authenticates requests with tag = SHA-256(secret ‖ params), the hash of its secret followed by the request parameters. You capture one request and its tag. You do not know the secret. What can you do?

A hash maps any input to a short fingerprint. It catches accidents, not attackers: it has no secret, so anyone can recompute it. A MAC mixes in a key that only the sender and receiver share. Use HMAC, not hash(key ‖ msg): that shortcut lets an attacker extend the message without the key. A signature uses a private key to sign and a public key to verify, so a third party can check it and the receiver cannot forge it.

Passwords are the opposite problem: you want guessing to be slow. Store a per-user salt and a deliberately slow hash. The salt stops one guess covering every user; the slow hash makes each guess cost more. Neither saves a password that sits near the top of a word list.

▶ SIMULATION
ALICE (SENDER)
msg = —
tag = —
holds: shared key
ON THE WIRE
msg = —
tag = —
last written by: —
BOB (RECEIVER)
expected = —
verdict: —
holds: shared key
scheme— · toy 8-byte hash, not cryptographic
mallorywatching
✓ a tampered message is rejected✓ a leaked password table gives up no password within the budget
SCHEDULE
01[ALICE]Alice and Bob agree: tag = hash(msg)
02[ALICE]Alice sends "amount=10&to=bob" with its tag
03[MALLORY]Mallory rewrites it to "amount=9000&to=mallory" and recomputes the hash
04[BOB]Bob recomputes the tag and compares
05[BOB]report
Start of plain hash scenario. 5 steps.
// integrity with a public hash: anyone can recompute it
const tag = sha256(msg);

// 'keyed' by prefixing: SHA-256 is Merkle–Damgård, so
// sha256(key + msg + pad + ext) can be computed from the tag alone
const tag2 = sha256(key + msg);

// early-exit comparison leaks how many bytes matched
if (expected === received) accept();

// passwords: fast and unsalted
db.save(user, sha256(password));

Every tag here comes from a toy 8-byte hash that is not cryptographic. It keeps the property that matters: its output is its state, so the length extension in HASH(KEY ‖ MSG) is real, not faked. Watch the first invariant turn red in PLAIN HASH and HASH(KEY ‖ MSG), and stay green in HMAC and SIGNATURE against the same Mallory. In HMAC, Carol cannot check the tag; in SIGNATURE, she can. The two password scenarios hit the second invariant. It holds in the slow-hash run only within the run’s small budget: the row under the verdict shows how fast a real attacker still finds the weak passwords.

BREAK IT NOT YET

Alice and Bob protect the message with a plain hash. Pick what Mallory does and when, so that Bob accepts a message Alice never sent.

GOAL: Bob accepts a tampered message

FIX IT NOT YET

Bob receives msg and an 8-byte tag. Return true only if Alice made it. Use lib.hmac(key, msg) and compare in constant time: read every byte of tag whether or not an early byte mismatches. The grader counts your reads of tag.

Mental model:

Ask who holds the secret. A hash has no secret, so it proves nothing about who wrote the message. A MAC has a shared secret, so it proves the message came from one of the two holders, and only they can check it. A signature has a private key, so anyone can check it and only one party could have made it. For passwords, flip the goal: make each guess expensive and give every user their own salt.

IN PRODUCTION

Webhook signatures (GitHub: HMAC-SHA256 over the body; Stripe: HMAC-SHA256 over the timestamp and the body), JWTs (HS256 is an HMAC, RS256 and EdDSA are signatures), signed URLs and cookies, package and commit signing, and every users table with a password column. Node's crypto.timingSafeEqual and Python's hmac.compare_digest exist for the comparison step.

IN INTERVIEWS

“How do you verify a webhook came from the provider?” or “How do you store passwords?” Say HMAC over the raw body with a constant-time compare, explain why sha256(secret + body) is not a MAC, know when a signature is needed (third-party verification, non-repudiation), and for passwords name Argon2id or bcrypt and say what the salt and the work factor each buy you.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: SECRETS, ROTATION & REPLAY →