The one-sentence version
With just 23 people in a room, there is a better than even chance that two share a birthday. The reason is that the number of "pairs" grows with the square of the number of people. In the same way, for a hash function with -bit output, you don't need tries to find two inputs with the same hash, only about . So the 256-bit SHA-256 provides only 128 bits of collision resistance.
This article has three layers:
- The intuition: the birthday paradox and the three properties a hash function must have
- Real-world use: blockchains, Git, certificates and the breaks of MD5 and SHA-1
- The mathematics: deriving , memoryless Pollard-rho-style collision search and a real truncated-collision experiment
Layer 1: The Intuition
1.1 The birthday paradox
There are 365 days in a year. How many people must be in a room for the probability that "two share a birthday" to exceed 50%?
Intuition often answers 183 (half of 365). The correct answer is 23.
The key is that we aren't asking "does someone share a birthday with me", but "do any two people share one". 23 people form pairs, each with a chance of matching, and across 253 pairs the odds become substantial.
| People | Probability that at least two share a birthday |
|---|---|
| 10 | 11.7% |
| 23 | 50.7% |
| 50 | 97.0% |
| 70 | 99.9% |
1.2 What a hash function is
A hash function compresses data of any length into a fixed-length "fingerprint"; SHA-256, for example, outputs 256 bits. A good hash function must satisfy:
| Property | Meaning | Ideal difficulty (-bit output) |
|---|---|---|
| Preimage resistance | Given a fingerprint, you can't find an input for it | |
| Second-preimage resistance | Given an input, you can't find another with the same fingerprint | |
| Collision resistance | You can't find any two inputs with the same fingerprint | (the birthday bound) |
The third corresponds exactly to the birthday paradox: a collision attacker freely chooses both inputs, like finding "any two people" with the same birthday in a room, so only tries are needed.
1.3 Why collisions are dangerous
If you can find two files A and B with the same hash: get someone to sign the harmless A (a signature really signs the hash), and that signature is equally valid for the harmful B.
Layer 2: Real-World Use
Hash functions are everywhere
- Blockchains: Bitcoin uses SHA-256 for proof of work and for linking blocks; an Ethereum address is the last 20 bytes of the Keccak-256 hash of a public key.
- Merkle trees: thousands of transactions are hashed into a single root, and hashes prove a given transaction is in a block.
- Git: every commit and file is named by a hash (historically SHA-1, now migrating to SHA-256).
- Digital signatures, certificates, password storage and file integrity checks.
A history of collision attacks
SHAttered took about SHA-1 computations, far fewer than the of a birthday attack. This shows it exploited structural weaknesses in SHA-1, not just the birthday paradox.
The design conclusion: for 128-bit security, collision-resistant uses need 256-bit output. This is why SHA-256 became the default choice.
How quantum computers affect hashing
A theoretical quantum collision algorithm (BHT) can reach , but it needs massive quantum memory, and its practical significance is doubtful. The mainstream view is that SHA-256 and SHA-3 remain sufficient in the post-quantum era, and hash-based signatures (SPHINCS+) are even one of the post-quantum standards. See "Post-Quantum Cryptography".
Layer 3: The Mathematics
3.1 Deriving the birthday bound
Drawing values independently and uniformly from possibilities, the probability that all are different is
Using (approximately equal for small ):
Setting the collision probability to and solving gives
With , , which matches 23. With , .
3.2 A worked example: actually finding a collision
SHA-256 truncated to 16 bits
Take only the first 4 hex characters of the SHA-256 output (16 bits, ). The birthday bound predicts about tries.
Hashing msg0, msg1, msg2 and so on while recording prefixes, a collision appeared on try 122:
| Input | First 16 hex characters of SHA-256 |
|---|---|
msg114 |
98978261783e81fd |
msg121 |
9897dbca5f8d00ea |
Both begin with the 16 bits 9897. (Actually run in Python; this run was on the lucky side.)
For comparison, finding an input with the specified prefix 9897 (a second preimage) takes about tries on average.
3.3 Collision search without much memory
The method above stores every hash seen, which is infeasible at the scale of . Floyd's cycle detection solves this:
Treat the hash function as a map on a finite set, and starting from iterate . Because the set is finite, the sequence eventually enters a cycle, shaped like the Greek letter . At the moment it enters the cycle, two different points map to the same point: a collision.
A "tortoise and hare" race (one moves one step at a time, the other two) finds it in time and memory. This is the same idea as Pollard rho for solving discrete logarithms. The parallel version by van Oorschot and Wiener lets thousands of machines search at once.
3.4 Why the birthday bound can't be "designed" away
The birthday attack is generic: it ignores the hash function's internal structure and works on any function with -bit output. So the ceiling on collision resistance is always bits. Cryptographers can do only one thing about it: make the output longer.
Summary
| Layer | Key points |
|---|---|
| Intuition | 23 people give an even chance of a shared birthday, because the number of "pairs" grows quadratically |
| Mathematics | About tries give an even chance of collision |
| Security | An -bit hash has at most bits of collision resistance, hence SHA-256 |
| History | MD5 (2004) and SHA-1 (2017) fell in turn, through structural weaknesses + the birthday idea |