MD5 and SHA-1: A History of Collisions
MD5 and SHA-1 were the standard hashes of the 1990s and 2000s. Both are now broken for collision resistance, and the history of how that happened is the best argument in security for retiring algorithms before the practical attack arrives. Each step took years, and each time some systems were still relying on the hash when the next step landed.
The timeline
| Year | Event |
|---|---|
| 1992 | MD5 published by Ron Rivest (RFC 1321), 128-bit digest |
| 1995 | SHA-1 published by NIST (FIPS 180-1), 160-bit digest |
| 1996 | Hans Dobbertin finds collisions in MD5's compression function; experts recommend moving away |
| 2004 | Xiaoyun Wang and colleagues publish real MD5 collisions, computed in hours |
| 2005 | Wang, Yin and Yu describe a SHA-1 collision attack well below the birthday bound; NIST plans the move to SHA-2 |
| 2008 | Sotirov, Stevens and others use an MD5 chosen-prefix collision to create a rogue CA certificate accepted by browsers |
| 2012 | The Flame espionage malware is found signed with a forged Microsoft certificate, made with a new MD5 chosen-prefix collision |
| 2017 | SHAttered: Google and CWI Amsterdam publish the first SHA-1 collision, two different PDFs with the same SHA-1, after about SHA-1 computations |
| 2020 | Gaëtan Leurent and Thomas Peyrin's "SHA-1 is a Shambles": a practical SHA-1 chosen-prefix collision, used to forge a PGP key certification; cost estimated at about 45,000 US dollars of GPU time |
Browsers stopped accepting SHA-1 TLS certificates in early 2017, Git started moving to SHA-256 object ids (still optional in 2026; Git 3.0, planned for 2027, is to make SHA-256 the default for new repositories), and NIST has announced that SHA-1 will be withdrawn entirely by the end of 2030.
Two kinds of collision
- Identical-prefix collision: the attacker chooses a common prefix , then computes two different blocks and with . SHAttered is of this kind.
- Chosen-prefix collision: the attacker picks two different prefixes and (say, two certificates with different names) and computes suffixes , so that . Much more powerful, much more expensive; used for the rogue CA, Flame and the 2020 PGP forgery.
Both are dangerous because of how MD5, SHA-1 and SHA-2 are built. They are Merkle–Damgård hashes: the message is cut into blocks, and a compression function updates an internal state block by block. Once two messages reach the same internal state, appending any identical suffix keeps them equal. One collision therefore yields infinitely many: two documents that differ in a small hidden block and agree everywhere else.
The structure on a toy hash
The example builds a toy Merkle–Damgård hash with a 32-bit state (its compression function is SHA-256 truncated to 4 bytes), finds two colliding first blocks by a birthday search, then appends the same human-readable suffix to both. A real attack does the same with MD5 or SHA-1, using cryptanalysis instead of the birthday search.
import hashlib, struct
# A toy Merkle-Damgard hash with a 32-bit state, built from SHA-256 as its compression function.
# Same structure as MD5, SHA-1 and SHA-256, but tiny, so collisions are cheap to find.
def compress(state, block): # 32-bit state, 16-byte blocks
return int.from_bytes(hashlib.sha256(state.to_bytes(4, "big") + block).digest()[:4], "big")
def toy_hash(msg):
msg += b"\x80" + b"\x00" * ((-len(msg) - 9) % 16) + struct.pack(">Q", len(msg))
state = 0x01234567 # fixed IV
for i in range(0, len(msg), 16):
state = compress(state, msg[i:i + 16])
return state
# 1. Find two different 16-byte blocks that collide from the IV (birthday search, about 2^16 tries).
seen = {}
i = 0
while True:
block = b"COLLIDE-%08d" % i
s = compress(0x01234567, block)
if s in seen:
a, b = seen[s], block
break
seen[s] = block
i += 1
print(f"colliding blocks after {i + 1:,} tries: {a!r} {b!r}")
# 2. Merkle-Damgard: once the states agree, ANY common suffix keeps them equal.
suffix = b" -- pay 100 EUR to Alice. Signed, Bob."
m1, m2 = a + suffix, b + suffix
print("m1 != m2:", m1 != m2)
print(f"toy_hash(m1) = {toy_hash(m1):08x}")
print(f"toy_hash(m2) = {toy_hash(m2):08x}")
print("a signature over toy_hash(m1) also verifies for m2:", toy_hash(m1) == toy_hash(m2))colliding blocks after 79,078 tries: b'COLLIDE-00068484' b'COLLIDE-00079077'
m1 != m2: True
toy_hash(m1) = ecb9bc03
toy_hash(m2) = ecb9bc03
a signature over toy_hash(m1) also verifies for m2: True
About 79,000 tries found the collision (the birthday estimate for 32 bits is about , roughly 82,000). In a real document format, the colliding blocks hide in a place the viewer ignores (a JPEG comment, a PDF object, a certificate's public-key field), and the visible content is steered by which block is present. SHAttered's two PDFs show different pictures for exactly that reason.
Why it matters for signatures
A signature is computed over the hash of a document. If an attacker can produce two documents with the same hash, they ask the victim (or a CA) to sign the harmless one and attach the signature to the harmful one. In 2008 the researchers got a commercial CA to sign an ordinary website certificate whose MD5 collided with a certificate that had CA:TRUE: they became a certificate authority trusted by every browser. They had to predict the serial number and validity time the CA would put into the certificate, which is why CAs now must use at least 64 bits of random serial number.
Where MD5 and SHA-1 are still acceptable
Where no attacker chooses the input: checksums against accidental corruption, cache keys of trusted data, and HMAC-SHA1 (HMAC's security doesn't rest on collision resistance, though new designs should still use SHA-256). Even then, using SHA-256 everywhere avoids the argument.
How MD5 and SHA-1 use is detected
- Certificates and signatures:
openssl x509 -noout -text | grep 'Signature Algorithm'showingmd5WithRSAEncryptionorsha1WithRSAEncryption; code-signing and document-signing policies; old internal CAs. - Collision detection: the
sha1collisiondetectionlibrary (Marc Stevens, Dan Shumow) recognises the disturbance patterns of known SHA-1 collision attacks in a single input. Git and GitHub use it, so a SHAttered-style file is rejected; it is a stopgap, not a fix. - Code:
hashlib.md5,sha1(,MessageDigest.getInstance("MD5"),md5sumin integrity checks and deduplication of uploaded content.
How it is fixed
- Replace with SHA-256 (or SHA-3, BLAKE2) everywhere an attacker controls any input: signatures, certificates, file identity, deduplication, commitments.
- Re-issue certificates and re-sign artefacts that used SHA-1, and remove trust in old intermediate CAs that sign with it.
- Plan the next migration while the current algorithm is still sound: record which hash each system uses ("crypto inventory") so swapping is a configuration change, not a project.
Pitfalls
- "SHA-1 is only broken in theory": SHAttered and the 2020 chosen-prefix collision are concrete, and costs only fall.
- Using collision detection as a reason to keep SHA-1: it recognises known attack patterns only.
- Forgetting embedded uses: firmware update checks, licence files, old VPN and SSH configurations (
hmac-md5,ssh-rsawith SHA-1 signatures).
Practice
1. A document-management system deduplicates uploads by MD5. What could an attacker do?
Upload one file, then a different file with the same MD5 (easy to construct with an identical-prefix collision); the system treats the second as a duplicate, so one user may receive the other's content or a malicious file may replace a benign one in shared storage. Use SHA-256.
2. Why did the 2008 rogue-CA attack need a chosen-prefix collision rather than an identical-prefix one?
The two certificates had to differ in meaningful fields (subject name, CA:TRUE) that come early in the signed data, so their prefixes were different; only a chosen-prefix collision connects two different prefixes to one hash.
3. In the toy example, if you append b"PS: and 900 EUR to Eve" to both colliding messages, do they still collide? Why?
Yes. Once the internal states are equal after the colliding blocks, identical further blocks keep them equal (and the lengths are equal, so the padding matches too).
Test yourself
What did SHAttered (2017) demonstrate?
- The first MD5 collision
- The first practical SHA-1 collision
- A SHA-256 preimage
- A break of HMAC
Why does one collision in a Merkle-Damgard hash give many?
- The hash is linear
- Equal internal states stay equal when the same suffix is appended
- Collisions are random
- The padding hides differences
Which type of collision was used for the Flame malware's forged certificate?
- Identical-prefix SHA-1
- Chosen-prefix MD5
- Birthday collision on SHA-256
- A second preimage
Where is MD5 still defensible?
- Certificate signatures
- Deduplicating user uploads
- Detecting accidental corruption of trusted data
- Password storage