Q&A-2Contrast preimage, second-preimage, and collision resistance.
Answer
The 90-second answer
Three properties, three different attacker goals against the same function:
- Preimage resistance: given a digest
h, find any messagemwithH(m) = h. Generic cost ~2^n. - Second-preimage resistance: given a specific message
m1, findm2 ≠ m1withH(m2) = H(m1). Generic cost ~2^n for a fixed target. - Collision resistance: find any two distinct messages with equal digests. Generic cost ~2^(n/2), by the birthday bound.
Collision resistance implies second-preimage resistance (a second preimage is a collision), but not the reverse, and neither implies preimage resistance. Collision resistance is the strongest property and the first to fall as n shrinks.
| property | attacker starts with | attacker wants | generic cost, n-bit digest |
|---|---|---|---|
| preimage | a digest | any message with that digest | ~2^n |
| second preimage | a digest + one message | a different message with that digest | ~2^n (fixed target) |
| collision | nothing | any two messages, same digest | ~2^(n/2) |
Birthday arithmetic: 50% collision probability at k ≈ 1.177·2^(n/2). SHA-1 (n=160) → 2^80. SHA-256 → 2^128. Preimage hits 50% at ~0.69·2^n.
Which property your protocol actually needs
- Password storage: preimage resistance is the property; collisions are irrelevant. Use Argon2/bcrypt/scrypt for cost, not a bare SHA-256.
- Digital signatures: collision resistance, because the signature covers the digest, not the message. A collision lets an attacker get a signature on a benign document and transplant it onto a hostile one.
- MACs: preimage/second-preimage. HMAC-SHA-256 is fine even though SHA-256 is Merkle–Damgård.
- Content addressing (git, IPFS): collision resistance, because the digest is the identity.
Failure modes in practice
-
MD5 (128-bit). Collisions published in 2004 (Wang et al.); chosen-prefix collisions in 2009 (Stevens et al.), which produced the rogue CA certificate using a cluster of a couple hundred PlayStation 3s. A plain MD5 collision is now a laptop-scale computation. Anything still verifying MD5 signatures is broken, not weak.
-
SHA-1 (160-bit). Birthday bound is 2^80. SHAttered (2017) produced two colliding PDFs for ~2^63.1 SHA-1 compressions: roughly 6,500 CPU-years plus 100 GPU-years. Leurent and Peyrin (2020) then built a chosen-prefix SHA-1 collision (~2^63.4 compression calls) and used it to forge a PGP key signature. Chosen-prefix is the variant that matters: the attacker controls both messages, so "sign this innocuous file" becomes "sign this malware."
-
Second preimage is cheaper than 2^n on long messages. Kelsey and Schneier (2005): for a Merkle–Damgård hash and a message of 2^k blocks, a second preimage costs about 2^(n−k). A 1 GB message is 2^24 blocks, so SHA-256 second-preimage on it is ~2^232, not 2^256. Still infeasible, but it shows why "second preimage = 2^n" is a short-message statement. Collision resistance is unaffected.
-
Length extension. MD5, SHA-1 and SHA-256 are Merkle–Damgård, so
H(secret ‖ message)is forgeable without knowing the secret. Not a collision, but it is the failure people actually hit when they hand-roll a MAC. Use HMAC, or SHA-3 / SHA-512/256, which are not vulnerable.
Detection and migration cost
You cannot detect a collision by looking at a digest: two colliding files look like two random 256-bit strings. Detection is inventory and policy: know which algorithms appear in your certificates, signatures and content addresses, and track the deprecation calendar. NIST disallowed SHA-1 for digital signature generation in 2013 (SP 800-131A); browsers stopped accepting SHA-1 certificates in 2017.
The real bill is migration. Git adopted SHA-1 collision detection (sha1dc) in 2017 and has supported SHA-256 repositories since 2.29, but the default is still SHA-1 because every object ID, packfile and third-party tool assumes 20 bytes. Certificate chains, HSMs, firmware signing keys and long-lived signed documents all have to be re-issued. Budget the migration when you pick the algorithm, not when the collision lands.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.