Q&A-1Compare encryption, hashing, MACs, and digital signatures.
Answer
Four primitives, four different questions. Encryption answers "can an eavesdropper read this?" Hashing answers "has this changed?" A MAC answers "did someone holding our shared key produce this?" A signature answers "can a third party verify who produced this without trusting the verifier?"
Assumptions: you are choosing primitives for a protocol, not a product; both ends are under your control or you have a PKI; and you are on current mainstream versions.
| primitive | property | key material | who can produce a valid value | concrete instance |
|---|---|---|---|---|
| AEAD encryption | confidentiality + integrity | symmetric, shared | any key holder | AES-256-GCM, 128-bit tag, 96-bit nonce |
| hash | preimage + collision resistance | none | anyone | SHA-256, 256-bit digest |
| MAC | integrity + authenticity among holders | symmetric, shared | any key holder | HMAC-SHA-256, key ≥ 256 bits |
| signature | integrity + authenticity + third-party verifiability | private/public pair | only the private-key holder | Ed25519, 64-byte signature, 32-byte public key |
Why each behaves as it does. A hash is unkeyed, so anyone can recompute it: replacing a file and its digest together is trivial, which is why a bare SHA-256 checksum does not authenticate an attacker-controlled download. SHA-256 is Merkle–Damgård, so the naive construction H(secret ‖ message) is vulnerable to length extension; HMAC exists to close that. HMAC-SHA-256 with a 256-bit key gives roughly 256-bit forgery resistance, but every holder of the key can forge, so it cannot settle a dispute with an outsider. Ed25519 gives ~128-bit security with 64-byte signatures and deterministic signing, which removes the ECDSA nonce-reuse failure mode that leaked private keys on the PS3 and in some Android Bitcoin wallets. RSA-2048 gives ~112-bit security and 256-byte signatures; verification is cheap (tens of microseconds) and signing is expensive (~1 ms), the opposite balance from Ed25519.
Failure modes worth naming. Reusing an AES-GCM nonce under the same key leaks the authentication subkey and XORs the two plaintexts: enforce a counter-based nonce that survives restarts. AES-CBC without a MAC is malleable and has produced padding oracles (Lucky13, POODLE). HMAC keys shorter than the hash output length weaken the function (RFC 2104). SHA-1 and MD5 are broken for collision resistance: SHAttered produced a SHA-1 collision in 2017, and MD5 collisions take seconds. A valid Ed25519 signature proves possession of a private key, not who owns it; without a certificate chain or a pinned key, it authenticates nothing.
What changes the choice: non-repudiation to a third party forces a signature; a shared key with only tamper detection needed favors HMAC (cheaper, no PKI); confidentiality forces an AEAD, never raw CBC or CTR; comparing a secret against a stored value calls for a password hash (Argon2id), not SHA-256.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.