HASHES, MACS & SIGNATURES
Integrity, authenticity, and who can prove what
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.
// 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));// a MAC: needs the key, immune to length extension
const tag = crypto.createHmac('sha256', key).update(msg).digest();
if (crypto.timingSafeEqual(tag, received)) accept();
// third parties must verify: sign with a private key
const sig = crypto.sign(null, Buffer.from(msg), privateKey); // Ed25519
crypto.verify(null, Buffer.from(msg), publicKey, sig);
// passwords: salted, deliberately slow, memory-hard
db.save(user, await argon2.hash(password)); // salt is inside the encoded hash 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- Lawful interception and data retentionHow Mobile Networks Work
- The SIM: identity in a cardHow Mobile Networks Work
- Integrating a payment provider: charges, verification, webhooksPayments
- Open banking: the CBN framework, with UK and Brazil comparedPayments
- Transfers and virtual accountsPayments
- Identity, access and secretsSecurity
- Security philosophies: defence in depth, least privilege, assume breach, risk not fearSecurity
- Software supply chain securitySecurity
- Threat modellingSecurity