The Birthday Paradox
How many people need to be in a room for the probability to exceed 50 percent that at least two of them share a birthday?
Maybe 100?
Or 183, because that's roughly half of 365 days?
In fact, just:
23 people.
Under the simplifying assumption of 365 equally likely birthdays, the probability of at least one shared birthday is around 50.7 percent.
This is a simplified probability model: February 29 is ignored, and all 365 days are treated as equally likely.
This surprising result is known as the birthday paradox.
For cryptographic hash functions, this effect is particularly interesting because it explains why the collision security of an ideal hash is significantly lower than its output length would suggest.
Why Are 23 People Already Enough?
The crucial mistake is often to confuse the following two questions:
How likely is it that someone has my birthday?
and:
How likely is it that any two people share a birthday?
These are different problems.
With 23 people, there aren't just 23 chances for a match.
Every person can be compared with every other person.
The number of possible pairs is:
n × (n - 1) / 2
With 23 people, that comes to:
23 × 22 / 2
=
253 pairs
So there are already 253 possible pairs in which a shared birthday could occur.
The Complementary Probability Is Easier
The probability of at least one shared birthday is easiest to calculate via the complementary probability.
First, we ask:
How likely is it that all people have different birthdays?
For the first person, the birthday doesn't matter:
365 / 365
For the second person, 364 days remain:
364 / 365
For the third:
363 / 365
and so on.
For n people, this gives:
P(no match)
=
365/365
×
364/365
×
363/365
×
...
×
(365 - n + 1)/365
Therefore:
P(at least one match)
=
1 - P(no match)
For 23 people, this comes to roughly:
50.7%
This seems surprising at first — but mathematically, it is a direct consequence of the rapidly growing number of possible pairs.
What Does This Have to Do with Hash Functions?
Instead of birthdays, we can think of hash values.
Suppose we had a greatly simplified hash function with only 365 possible hash values.
Every input produces one of these values.
As we hash more and more different inputs, the probability increases that at some point two of them have the same hash value.
Input A → 127
Input B → 291
Input C → 044
...
Input X → 127
Now two different inputs have the same hash value:
Hash(A) = Hash(X)
We have found a hash collision.
I explain exactly what a hash collision is in the article What Is a Hash Collision?.
From 365 Birthdays to 2²⁵⁶ Hash Values
Of course, SHA-256 doesn't have just 365 possible outputs.
A SHA-256 hash consists of 256 bits.
That gives:
2²⁵⁶
possible hash values.
That is an unimaginably large number.
Nevertheless, a generic collision search doesn't need to compute roughly 2²⁵⁶ hash values.
The birthday effect means that collisions become relevant at the order of magnitude of:
√(2²⁵⁶)
=
2¹²⁸
In general, for an ideal n-bit hash:
collision effort ≈ 2^(n/2)
This is why NIST lists SHA-256 with a collision-resistance strength of 128 bits.
Why Doesn't 50 Percent Mean a Collision Definitely Exists Afterward?
The birthday bound is a probabilistic statement.
It does not say:
After a certain number of attempts, a collision must occur.
Instead, as the number of examined hash values grows, so does the probability of finding at least one colliding pair.
That is an important difference.
Even after reaching the order of magnitude of the birthday bound, a collision is not guaranteed.
Try It Yourself
With the following Birthday Collision Simulator, you can choose the size of a hash space and specify how many hash values are generated.
From that, the simulator calculates exactly how likely at least one hash collision is — and shows how many pairs are being compared in the process.
First try the birthday example with 23 people, and then switch to a 16- or 24-bit hash space.
Birthday Collision Simulator
Choose the size of the hash space and the number of generated hash values – the probability of a hash collision is calculated exactly and instantly.
- Output length
- 8 bits
- Possible hash values
- 28 = 256
- Generated hash values
- 20
- Possible pairs k(k−1)/2
- 190
- Exact probability
- 53.3%
- Approximation 1 − e^(−k(k−1)/(2N))
- ≈ 52.4%
- Birthday bound (order of magnitude)
- 24 ≈ 16
- 50% threshold √(2·N·ln 2)
- ≈ 18.8 (exactly from 20)
The exact probability follows from P(no collision) = N/N × (N−1)/N × … × (N−k+1)/N. The approximation is only an estimate. 2n/2 describes the order of magnitude at which collisions become likely – not an exact 50% limit.
Takeaway: What matters is not the number of hash values but the number of pairs. It grows quadratically – that is why roughly √N attempts are enough for a realistic chance of a hash collision.
You can also find this tool in the Tools section.
Pay particular attention to the value at roughly 2^(n/2) generated hash values: there, the probability is already around 39 percent.
I show what a real experiment with random inputs looks like in the article How Does a Birthday Attack Work?. There, hashing continues until a collision actually occurs — and if you repeat the experiment, the first collision doesn't always occur on the same attempt.
Birthday Paradox and Collision Resistance
Now it also becomes clearer why hash length and collision resistance are not the same thing.
For an ideal hash, the rough rule is:
| Hash length | Generic collision security |
|---|---|
| 128 bits | 64 bits |
| 256 bits | 128 bits |
| 384 bits | 192 bits |
| 512 bits | 256 bits |
That doesn't mean 128 bits are "missing" from SHA-256.
The 128 bits are the expected generic collision security of an ideal 256-bit hash.
I explain this relationship in more detail in the article What Is Collision Resistance?.
Birthday Paradox and Preimage Resistance
The birthday paradox also explains an important difference from preimage resistance.
With a collision, I'm allowed to look for any colliding pair:
x ≠ y
Hash(x) = Hash(y)
With a preimage attack, by contrast, a specific hash value is already given:
H
find:
x with Hash(x) = H
So I can't simply wait for any match.
That is why the generic preimage effort for an ideal 256-bit hash is on the order of:
2²⁵⁶
while the generic collision effort is roughly:
2¹²⁸
I explain more about this in the article What Is Preimage Resistance?.
The Key Takeaways
The birthday paradox describes the surprisingly rapid increase in the probability that two values in a group match.
The most important points:
- With 23 people, the probability of at least one shared birthday is already around 50.7 percent in the simplified model with 365 equally likely days.
- What matters is the rapidly growing number of possible pairs.
- The same mathematical effect plays an important role in hash collisions.
- For an ideal
n-bit hash, the generic collision effort is on the order of2^(n/2). - For an ideal 256-bit hash, this therefore comes to roughly
2¹²⁸. - NIST accordingly lists SHA-256 with 128 bits of collision resistance.
- The birthday paradox concerns the search for arbitrary collisions and is not equivalent to a preimage attack.
Where To Go From Here
The birthday paradox explains why collisions occur sooner than you might intuitively expect.
The next article covers how this principle can be used deliberately for an attack:
How Does a Birthday Attack Work?
Sources and Standards
The following primary and standards sources form the basis for the technical statements in this article:
-
National Institute of Standards and Technology (NIST), Hash Functions Security strengths of cryptographic hash functions, including the collision-resistance strength of SHA-256.
-
National Institute of Standards and Technology (NIST), SP 800-57 Part 1 Rev. 5 – Recommendation for Key Management: Part 1 – General Definition and classification of cryptographic security strength.