Skip to main content

How Does a Birthday Attack Work?

The birthday paradox shows that matches within a large set of possible values can occur surprisingly quickly.

In cryptography, exactly this effect can be applied to hash functions.

A birthday attack is a generic approach to searching for hash collisions.

The goal is:

Find x and y

such that:

x ≠ y

and

Hash(x) = Hash(y)

So the attacker isn't trying to hit a specific hash value.

They're looking for any pair of different inputs with the same hash.

A Greatly Simplified Example​

Imagine a hash function with only 8 bits of output.

Then there are:

2⁸ = 256

possible hash values.

We generate different inputs:

Input 1 → 73
Input 2 → 194
Input 3 → 21
Input 4 → 238
...

and store the results.

At some point, the following might happen:

Input 12 → 73

That gives us:

Hash(Input 1)
=
Hash(Input 12)

even though:

Input 1 ≠ Input 12

That's a collision.

Why Don't All 256 Values Need to Be Tried?​

Because we're not looking for a specific hash value.

Every new hash value is compared with all the hash values found so far.

With every additional input, the number of possible pairs therefore grows.

For an ideal n-bit hash, the generic collision effort is therefore on the order of:

2^(n/2)

For 8 bits, that means roughly:

2⁴ = 16

For SHA-256:

2¹²⁸

I explain the underlying probability problem in detail in the article The Birthday Paradox.

What Does a Generic Birthday Attack Look Like?​

In simplified form, an attack might look like this:

1. Generate an input.
2. Calculate its hash.
3. Store the input and the hash.
4. Generate another input.
5. Calculate its hash.
6. Check whether this hash already exists.
7. Repeat the process.

As soon as a stored hash appears again, there are two different inputs with the same hash value.

Conceptually:

A → 42
B → 91
C → 17
D → 203
E → 42

That gives us:

Hash(A) = Hash(E)

and the attack has found a collision.

Why Is This Still Not Practical Against SHA-256?​

2¹²⁸ is much smaller than 2²⁵⁶.

But 2¹²⁸ is still an enormous order of magnitude.

NIST therefore assigns SHA-256 a collision-resistance strength of 128 bits.

"Only half as many bits" doesn't mean the attack suddenly becomes practical.

128 bits of security stand for a workload on the order of 2¹²⁸ basic operations. NIST defines security strength precisely in terms of the order of magnitude of the work required to break a cryptographic system.

I explain why SHA-256 has exactly this collision security despite its 256-bit output in the article What Is Collision Resistance?.

A Birthday Attack Is Not a Preimage Attack​

In a birthday attack, an attacker looks for:

any x
and
any y

with the same hash.

In a preimage attack, by contrast, a target hash is already given:

H

and the goal is to find:

x with Hash(x) = H

So in a birthday attack, the attacker has considerably more freedom.

This is exactly what produces the difference between roughly 2^(n/2) for generic collisions and 2^n for generic preimage search with an ideal n-bit hash.

I explain what preimage resistance means in the article What Is Preimage Resistance?.

A Birthday Attack Is Not a Second-Preimage Attack Either​

In a second-preimage attack, a specific input is already given:

x

and the goal is to find a different one:

y

such that:

Hash(x) = Hash(y)

In a birthday attack, by contrast, the attacker may choose both inputs.

Being able to find any collision therefore does not automatically mean that a second preimage can be found for an arbitrary existing message.

I explain more about this in the article What Is Second-Preimage Resistance?.

Try the Attack Yourself​

For SHA-256, a real experiment with complete hash values would of course be completely unsuitable — after all, we don't want to generate 2¹²⁸ values.

Instead, the effect can be demonstrated with an artificially shrunken hash space.

The following tool calculates the full SHA-256 hash for random inputs, but compares only the first 8, 12, 16, or 20 bits.

With 16 bits, there are:

65,536

possible values.

The birthday bound, however, is only on the order of:

2⁸ = 256

attempts.

Start the experiment and watch how many random inputs it takes until the first collision is found:

Birthday Attack on a Truncated Hash

Start the experiment: random inputs are hashed until two of them produce the same truncated hash value.

This experiment calculates SHA-256 but compares only a small part of the hash value. This artificially shrinks the hash space. The experiment says nothing about full SHA-256.

Compared bits of the SHA-256 hash value
Effective bit length
16 bits
Possible truncated hash values
216 = 65,536
Birthday bound (order of magnitude)
28 = 256
Expected attempts √(π·N/2)
≈ 321
Attempts
0
Elapsed time
–

Takeaway: With 216 possible values, attempts on the order of 28 are usually enough. For full SHA-256, that would be about 2128 – practically out of reach.

You can also find this tool in the Tools section.

If you repeat the experiment several times, the number of attempts varies — but it almost always stays on the order of 2^(n/2) and far below the number of possible values.

Important: truncating the hash value is done here purely for demonstration. It says nothing about whether full SHA-256 could practically be made to collide.

What Protects Against Birthday Attacks?​

The most important generic defense is a sufficiently large hash output combined with a cryptographically secure hash function.

If n bits of output provide roughly n/2 bits of generic collision security, the output must be chosen correspondingly large.

SHA-256, for example, provides 128 bits of generic collision resistance.

SHA-512 provides 256 bits.

I explain how to put these security strengths into perspective in the article How Secure Are 128, 256, and 512 Bits?.

The Key Takeaways​

A birthday attack uses the birthday effect for the generic search for hash collisions.

  • The goal is to find two different inputs with the same hash value.
  • No specific target hash is given.
  • For an ideal n-bit hash, the generic effort is on the order of 2^(n/2).
  • For SHA-256, this corresponds to a collision-resistance strength of 128 bits.
  • 2¹²⁸ is still a practically enormous order of magnitude.
  • A birthday attack is not a preimage attack.
  • It is not a second-preimage attack either.
  • With truncated hash values, the effect can be demonstrated very well experimentally.

Where To Go From Here​

Now we know both the mathematics behind the birthday paradox and its significance for collision attacks.

That raises an obvious question:

How Secure Are 128, 256, and 512 Bits, Actually?

Sources and Standards​

The following primary and standards sources form the basis for the technical statements in this article:

  1. National Institute of Standards and Technology (NIST), Hash Functions Security strengths of SHA-2 and SHA-3, including the collision-resistance strengths of SHA-256 and SHA-512.

  2. National Institute of Standards and Technology (NIST), CSRC Glossary – Security Strength Definition of security strength ("bits of security") in terms of the work required to break a system.