Caloogy Article
中文

The Birthday Paradox and Hash Collisions: Why SHA-256 Has Only 128-Bit Collision Resistance

Date
Apr 24, 2026
Series
Cryptography
Reading
4 min
Topics
HashBirthday paradoxCollisionSHA-256

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 nn-bit output, you don't need 2n2^n tries to find two inputs with the same hash, only about 2n=2n/2\sqrt{2^n} = 2^{n/2}. So the 256-bit SHA-256 provides only 128 bits of collision resistance.

This article has three layers:

  1. The intuition: the birthday paradox and the three properties a hash function must have
  2. Real-world use: blockchains, Git, certificates and the breaks of MD5 and SHA-1
  3. The mathematics: deriving N\sqrt{N}, 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 (232)=253\binom{23}{2} = 253 pairs, each with a 1/3651/365 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 (nn-bit output)
Preimage resistance Given a fingerprint, you can't find an input for it 2n2^n
Second-preimage resistance Given an input, you can't find another with the same fingerprint 2n2^n
Collision resistance You can't find any two inputs with the same fingerprint 2n/22^{n/2} (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 2n\sqrt{2^n} 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

A history of collision attacks

2004Xiaoyun Wang's teamannounces MD5collisions, shaking thecryptography world2005Wang's team gives atheoretical attack onSHA-1, far faster thanthe birthday bound2008Researchers use MD5collisions to forge atrusted CA certificate2012The Flame spywareuses an MD5 collisionto forge Microsoftcode signing2017SHAttered, the firstpublic SHA-1 collision(two PDFs)2020A SHA-1 chosen-prefixcollision (Shambles),usable to forge PGPkeysMilestones in hash collisions

SHAttered took about 2632^{63} SHA-1 computations, far fewer than the 2802^{80} 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 2n/32^{n/3}, 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 kk values independently and uniformly from NN possibilities, the probability that all are different is

Pno collision=∏i=0k−1(1−iN).P_{\text{no collision}} = \prod_{i=0}^{k-1}\Big(1 - \frac{i}{N}\Big).

Using 1−x≤e−x1 - x \le e^{-x} (approximately equal for small xx):

Pno collision≈exp⁡(−∑i=0k−1iN)=exp⁡(−k(k−1)2N)≈e−k2/(2N).P_{\text{no collision}} \approx \exp\Big(-\sum_{i=0}^{k-1}\frac{i}{N}\Big) = \exp\Big(-\frac{k(k-1)}{2N}\Big) \approx e^{-k^2/(2N)}.

Setting the collision probability to 1/21/2 and solving gives

k≈2Nln⁡2≈1.18N.k \approx \sqrt{2N \ln 2} \approx 1.18\sqrt{N}.

With N=365N = 365, k≈22.5k \approx 22.5, which matches 23. With N=2256N = 2^{256}, k≈2128k \approx 2^{128}.

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, N=65536N = 65536). The birthday bound predicts about 1.18×256≈3001.18 \times 256 \approx 300 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 6553665536 tries on average.

3.3 Collision search without much memory

The method above stores every hash seen, which is infeasible at the scale of 2642^{64}. Floyd's cycle detection solves this:

Treat the hash function HH as a map on a finite set, and starting from x0x_0 iterate xi+1=H(xi)x_{i+1} = H(x_i). Because the set is finite, the sequence eventually enters a cycle, shaped like the Greek letter ρ\rho. 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 O(N)O(\sqrt{N}) time and O(1)O(1) 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 nn-bit output. So the ceiling on collision resistance is always n/2n/2 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 k≈1.18Nk \approx 1.18\sqrt{N} tries give an even chance of collision
Security An nn-bit hash has at most n/2n/2 bits of collision resistance, hence SHA-256
History MD5 (2004) and SHA-1 (2017) fell in turn, through structural weaknesses + the birthday idea