Introduction
MD5 and SHA-1 were once the standard hashes. You will still meet them in old code, old tutorials and old databases. Both are now considered broken for security, and this lesson explains what "broken" means for a hash: not that it stopped working, but that someone found a way to make collisions far faster than the design promised.
The story starts with a puzzle about birthdays, because it sets the speed limit for every hash.
How it works
How many people must be in a room before two of them probably share a birthday? Most people guess around 180. The answer is 23. The trick is that we need any pair to match, and 23 people make 253 pairs.
// The birthday paradox: how likely is it that two people in a room share a birthday?
function shared(people, days) {
let allDifferent = 1;
for (let k = 0; k < people; k++) allDifferent *= (days - k) / days;
return 1 - allDifferent;
}
for (const people of [10, 23, 50, 70]) {
console.log(people + " people: " + (shared(people, 365) * 100).toFixed(1) + "%");
}The same maths applies to hashes. With an n-bit hash there are 2ⁿ possible values, but you expect your first collision after only about 2^(n/2) random inputs. This is the birthday bound, and it is why hash sizes are chosen with collisions in mind:
| Hash | Output size | Birthday bound (collisions expected after) | Status |
|---|---|---|---|
| MD5 | 128 bits | about 2⁶⁴ | Broken: collisions are found in seconds on a laptop |
| SHA-1 | 160 bits | about 2⁸⁰ | Broken: first public collision in 2017 |
| SHA-256 | 256 bits | about 2¹²⁸ | No practical attack known |
"Broken" means attackers beat the birthday bound by using weaknesses in the design. Researchers published MD5 collisions in 2004. In 2008, a team used an MD5 collision to create a fake certificate authority certificate that browsers would have trusted, and the Flame malware, found in 2012, used an MD5 collision to make its code look signed by Microsoft. In 2017, Google and CWI Amsterdam published two different PDF files with the same SHA-1 hash, an attack they called SHAttered.
Why do collisions matter so much? A signature signs a hash. If an attacker can make a harmless document and a harmful one with the same hash, a signature on the harmless one is also a valid signature on the harmful one.
Example
The program cuts FNV-1a down to 8 bits (256 possible values) and hashes messages m0, m1, … until two collide. The birthday bound predicts a first collision after about 2⁴ = 16 messages; on average it is about 20.
// Find two different messages with the same 8-bit hash (FNV-1a cut down to 2 hex digits).
function hash8(text) {
let hash = 2166136261;
for (let k = 0; k < text.length; k++) {
hash = hash ^ text.charCodeAt(k);
hash = Math.imul(hash, 16777619) >>> 0;
}
return (hash & 0xff).toString(16).padStart(2, "0");
}
const seen = new Map();
for (let n = 0; ; n++) {
const message = "m" + n;
const h = hash8(message);
if (seen.has(h)) {
console.log('"' + seen.get(h) + '" and "' + message + '" both hash to ' + h);
console.log("found after " + (n + 1) + " messages, out of 256 possible hashes");
break;
}
seen.set(h, message);
}Line by line
hash8is FNV-1a with only its last 8 bits kept (& 0xff), shown as 2 hex digits.seenmaps each hash to the first message that produced it.- The loop makes a new message each time and hashes it. If the hash is already in
seen, two different messages share it: a collision. - This run needed 21 messages, close to the average of about 20 and tiny next to the 256 possible hashes. Collisions arrive long before the hash values run out.
Expected result
"m6" and "m20" both hash to ca
found after 21 messages, out of 256 possible hashes
Scale the same idea up: for a 128-bit hash the birthday bound is 2⁶⁴, which was once out of reach; MD5's weaknesses cut the real work so far that collisions take seconds.
In the real world
- Browsers and certificate authorities stopped accepting SHA-1 certificates around 2017.
- Git was built on SHA-1; it added detection for the known collision attacks and supports SHA-256 repositories.
- MD5 still appears as a quick checksum for accidental download errors. That is acceptable only when no attacker is involved.
- Collision attacks do not mean MD5 passwords can be reversed directly, but MD5 is so fast that guessing passwords hashed with it is cheap, which the next lesson deals with.
Common mistakes
- Using MD5 or SHA-1 in new code because an old tutorial did.
- Thinking "no one has reversed it" means it is safe. Signatures fail through collisions, not reversal.
- Choosing hash size without the birthday bound. A 64-bit hash gives only about 2³² security against collisions.
- Leaving old hashes in place. Systems should move stored data and signatures to SHA-256 or later.
Best practices
- Use SHA-256, SHA-384 or SHA-3 for anything security-related.
- Treat MD5 and SHA-1 as accident detectors only, never against attackers.
- When a hash is declared broken, plan the move away from it before attacks become cheap.
- Remember the birthday bound: for n bits of collision security, use a hash of 2n bits.
Summary
With an n-bit hash, collisions appear after about 2^(n/2) tries, the birthday bound, just as 23 people are enough for a likely shared birthday. A hash is broken when attackers beat that bound by exploiting its design: MD5 collisions were published in 2004 and have been used to fake a certificate authority and software signatures, and the first public SHA-1 collision came in 2017. Collisions break signatures, because one signature then covers two documents. Use SHA-256 or newer for security.
Quick check
Key takeaways
- Collisions in an n-bit hash appear after about 2^(n/2) tries, not 2ⁿ.
- A hash is broken when attacks beat that bound; MD5 and SHA-1 both are.
- Collisions let one signature cover two different documents.
- Use SHA-256 or newer, and keep MD5 only for spotting accidental errors.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.