Skip to main content

What Is a Hash Collision?

A hash collision occurs when two different inputs produce the same hash value.

Simplified:

Input A ──┐
├── Hash function ──→ same hash value
Input B ──┘

even though:

Input A ≠ Input B

At first glance, this sounds like a flaw. After all, you'd expect a cryptographic hash function like SHA-256 to produce different hash values for different data.

In fact, though, collisions are mathematically unavoidable.

The decisive question is therefore not whether collisions exist, but:

How difficult is it to find a collision?

This is exactly where one of the most important security properties of cryptographic hash functions lies.

What a hash function fundamentally does is explained in the article What Is a Hash Function?. If you haven't already, it's worth taking a look there before continuing here.

Why Must Hash Collisions Exist?​

A hash function can process inputs of very different lengths.

SHA-256, however, always produces a hash value with exactly 256 bits.

That means there are exactly

2²⁵⁶

possible hash values for SHA-256.

That's a huge number — approximately:

1.16 × 10⁷⁷

Still, the number of possible SHA-256 hash values is finite.

Consider, for example, all possible inputs with exactly 257 bits in length. There are:

2²⁵⁷

of those.

That's already more distinct 257-bit inputs than possible SHA-256 hash values.

So it's impossible for all of these inputs to get their own hash value. At least two different inputs must have the same hash value.

This principle can be explained with the pigeonhole principle.

Imagine you have more objects than drawers. If every object has to go into a drawer, at least one drawer must contain more than one object.

For a hash function:

  • the inputs correspond to the objects,
  • the possible hash values correspond to the drawers.

Collisions are therefore not an unusual special case, but a mathematical consequence of a hash function's fixed output length.

Does That Mean SHA-256 Is Insecure?​

No.

The fact that collisions exist doesn't by itself say anything about whether a hash function is secure.

What matters for a cryptographic hash function is that it should be practically impossible to deliberately find two different inputs with the same hash value.

This property is called collision resistance.

Simplified:

Find A and B with

A ≠ B

but

Hash(A) = Hash(B)

For a collision-resistant hash function, this task should not be practically solvable with realistically available computing resources.

256 Bits Doesn't Mean 256 Bits of Collision Security​

SHA-256 has a 256-bit hash value.

You might therefore initially assume that roughly

2²⁵⁶

attempts would be required to find a collision.

For an ideal hash function, however, the generic effort for finding a collision is substantially lower.

For a hash function with an output length of n bits, the relevant order of magnitude, due to what's known as the birthday bound, is approximately:

2^(n/2)

For SHA-256, that works out to:

2^(256/2)
= 2^128

SHA-256 is therefore classified by NIST as having a collision-resistance strength of 128 bits.

For comparison:

PropertySHA-256
Hash length256 bits
Generic preimage securityapproximately 256 bits
Generic collision securityapproximately 128 bits

Why the effort for collisions differs so much from a preimage search can be explained by the birthday problem.

What Does a Hash Collision Have to Do with Birthdays?​

The so-called birthday problem delivers a surprising result.

With 23 randomly chosen people in a room, the probability is already around 50.7% that at least two of them share the same calendar birthday — if we, for example, ignore leap years and assume birthdays are evenly distributed.

That initially seems surprising.

The reason is that we're not asking:

Who shares the same birthday as one particular person?

But rather:

Is there any pair of people with the same birthday?

With every additional person, the number of possible pairs increases sharply.

A similar effect occurs with hash values.

In a collision attack, we're not looking for an input for one particular, predetermined hash value.

We're looking for any two different inputs whose hash values are identical.

That's why a collision becomes statistically likely much earlier than you might initially expect just by looking at the number of possible hash values.

For an ideal n-bit hash, the relevant order of magnitude is approximately:

2^(n/2)

This relationship is often referred to as the birthday bound.

I explain the probability calculation behind this in detail in the article The Birthday Paradox, and how the effect can be deliberately exploited for a collision search in the article How Does a Birthday Attack Work?.

Collision Is Not the Same as Preimage​

At this point, one distinction is especially important.

There are several cryptographic tasks that look similar at first glance.

Collision​

We're looking for any two different inputs with the same hash:

Find A and B:

A ≠ B
Hash(A) = Hash(B)

Preimage​

Here, a specific hash value H is already given.

We're looking for an input A with:

Hash(A) = H

That's a different task.

For an ideal 256-bit hash function, the generic effort for a preimage search is approximately:

2²⁵⁶

while the relevant order of magnitude for a collision search, due to the birthday bound, is approximately:

2¹²⁸

That's why collision resistance and preimage resistance shouldn't be confused with each other.

What preimage resistance means exactly, and why SHA-256 is assigned a security strength of 256 bits for it, is explained in the article What Is Preimage Resistance?.

What Is Second-Preimage Resistance?​

There's a third, related property.

Here, a specific input A is already known.

We're looking for a different input B such that:

A ≠ B

and

Hash(A) = Hash(B)

This is called second-preimage resistance.

This, too, is different from a normal collision attack.

In a collision, both inputs can be freely chosen.

In a second-preimage attack, one of the two inputs is already given.

This distinction matters because these are different cryptographic problems.

Have Real Hash Collisions Already Been Found?​

Yes.

For older cryptographic hash functions, practical collision attacks have been developed.

A well-known example is MD5.

Practical methods have been developed for MD5 that can be used to deliberately produce different data with the same MD5 hash. That's why MD5 is no longer suitable for applications that need collision resistance, according to RFC 6151.

SHA-1 is also no longer considered a suitable cryptographic hash function for new applications. NIST recommends replacing SHA-1 with SHA-2 or SHA-3.

In 2017, Marc Stevens and other researchers demonstrated a practical collision for the full SHA-1 hash function with work that became known as SHAttered.

Two different PDF files were created that have the same SHA-1 hash value.

This shows an important distinction:

Collisions exist mathematically.

is something completely different from:

Collisions can be practically, deliberately produced.

The second case is a serious security problem for a cryptographic hash function.

Why Can Collisions Be Dangerous?​

Collisions become especially problematic when an attacker can deliberately construct two different documents so that both have the same hash value.

If one of these documents is subsequently signed, or otherwise processed based on its hash value, the colliding second document could, under certain conditions, be used maliciously.

What matters here is:

In a normal collision attack, the attacker can influence both colliding inputs.

The task of finding a different document with the same hash value for an already fixed document, on the other hand, is a different cryptographic problem — a second-preimage attack.

This distinction matters when assessing the practical impact of hash collisions.

How Secure Is SHA-256 Against Collisions?​

NIST classifies SHA-256 as having a collision-resistance strength of 128 bits.

For an ideal 256-bit hash, the generic effort for a collision search is accordingly in the order of magnitude of:

2¹²⁸

operations.

That's far beyond what's practically feasible with today's classical computing technology.

That doesn't mean, however:

A SHA-256 collision doesn't exist.

We know mathematically that collisions must exist.

The security, rather, rests on the fact that it should be practically impossible to deliberately find such a collision.

This exact distinction is central to understanding cryptographic hash functions.

What Does Collision Resistance Mean?​

A cryptographic hash function is considered collision-resistant if it's practically impossible to find two different inputs that produce the same hash value.

We're looking for:

A ≠ B

with:

Hash(A) = Hash(B)

Collision resistance therefore doesn't mean such pairs don't exist.

It means they can't be practically found with realistically available resources.

The Key Takeaways​

A hash collision occurs when two different inputs produce the same hash value.

The most important points:

  • Collisions are mathematically unavoidable for hash functions with a fixed output length.
  • The existence of collisions doesn't automatically make a hash function insecure.
  • What matters is how difficult it is to deliberately find a collision.
  • This security property is called collision resistance.
  • For an ideal n-bit hash, the relevant order of magnitude for a generic collision search is approximately 2^(n/2).
  • SHA-256 is classified with 128 bits of collision resistance.
  • Collision resistance is not the same as preimage resistance or second-preimage resistance.
  • MD5 is no longer suitable for applications that need collision resistance.
  • SHA-1 should be replaced by SHA-2 or SHA-3.

The fact that collisions are unavoidable is therefore not a contradiction to the security of cryptographic hash functions.

The real security question isn't:

Does a collision exist?

But rather:

Can we find one?

Where To Go From Here​

Once you've understood why hash collisions exist, the next interesting topics are:

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 Primary source for the security properties of hash functions and the collision-resistance strength of SHA-256.

  2. National Institute of Standards and Technology (NIST), NIST Policy on Hash Functions Current NIST status on SHA-1, SHA-2, and SHA-3.

  3. National Institute of Standards and Technology (NIST), FIPS 180-4 – Secure Hash Standard (SHS) Specification of the SHA-1 and SHA-2 hash algorithms, including SHA-256.

  4. IETF, RFC 6151 – Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms Security assessment of MD5, in particular its known collision problems.

  5. IETF, RFC 6194 – Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms Security considerations and known weaknesses of SHA-1.

  6. Marc Stevens, Elie Bursztein, Pierre Karpman, Ange Albertini, Yarik Markov, The First Collision for Full SHA-1, CRYPTO 2017. Practical collision for the full SHA-1 hash function, known as "SHAttered."