Skip to main content

What Is Second-Preimage Resistance?

An important security property of cryptographic hash functions is what's known as second-preimage resistance.

Here, a specific input is already known.

The task is to find a different input with the same hash value.

Simplified:

given:

input x

find:

input y

such that:

x ≠ y

and

Hash(x) = Hash(y)

For a cryptographically secure hash function, it should be practically impossible to find such a second input.

This property is called second-preimage resistance.

What Is a Second Preimage?​

Take an input x:

x
↓
hash function
↓
H

The input produces the hash value H.

In a second-preimage attack, we know both x and the resulting hash value.

We're now looking for a different input y:

x ──→ hash function ──→ H
↑
y ──→ hash function ────┘

where:

x ≠ y

The second input y is called a second preimage.

An Example with SHA-256​

Let's take, again:

Hello World

Its SHA-256 hash is:

a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

In a second-preimage attack, the task would now be:

Find a different input than Hello World that produces exactly the same SHA-256 hash.

We're looking, in other words, for a y with:

y ≠ "Hello World"

but:

SHA-256(y)
=
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

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

The Key Difference from Preimage Resistance​

Second-preimage resistance sounds very similar to preimage resistance at first.

The difference lies in what's already given.

In a preimage attack, a hash value is given:

given:

H

find:

x

such that:

Hash(x) = H

In a second-preimage attack, on the other hand, a specific input is already given:

given:

x

find:

y

such that:

x ≠ y

and

Hash(x) = Hash(y)

These are different cryptographic problems.

What preimage resistance means exactly is explained in the article What Is Preimage Resistance?.

Second Preimage Is Not the Same as a Collision​

An even more important distinction is from a hash collision.

In a collision, an attacker may search for both inputs:

find:

x and y

such that:

x ≠ y

and

Hash(x) = Hash(y)

So, initially, neither input is given.

In a second-preimage attack, the situation looks different:

x is already given

find:

y

such that:

x ≠ y

and

Hash(x) = Hash(y)

This additional constraint makes a decisive difference.

A successful collision attack, therefore, doesn't automatically mean you can find a second preimage for any arbitrary, already existing message.

Why collisions are nonetheless an important security property of cryptographic hash functions is explained in the articles What Is a Hash Collision? and What Is Collision Resistance?.

Why Does This Distinction Matter in Practice?​

Imagine a particular file has a known hash value.

For example:

original.pdf
↓
SHA-256
↓
H

A second-preimage attack would try to produce a different file:

manipulated.pdf
↓
SHA-256
↓
H

with:

original.pdf ≠ manipulated.pdf

The original file is already fixed here.

This is exactly what distinguishes this scenario from a normal collision attack, where the attacker can construct both colliding files.

This distinction matters when assessing practical attacks on hash functions.

How Secure Is SHA-256 Against Second-Preimage Attacks?​

A bit more caution is needed here than with preimage or collision security.

For SHA-256, NIST gives the second-preimage-resistance strength as:

256 − L(M)

bits.

L(M) depends on the length of the message under consideration.

NIST defines, for SHA-256:

L(M) = log₂(len(M) / 512)

where len(M) is the message length in bits and 512 is SHA-256's block size.

That means:

The second-preimage security of SHA-256 isn't stated as a flat 256 bits for every conceivable message length.

For ordinary messages, the security strength stays very high. For very long messages, though, it can be lower.

This peculiarity is one of the reasons why preimage resistance and second-preimage resistance, despite their similarity, are considered separately.

Why Does the Message Length Matter?​

SHA-256 processes messages in blocks.

The block size, according to FIPS 180-4, is:

512 bits

Very long messages therefore consist of many individual blocks.

For iterative hash constructions, generic second-preimage attacks are known that can require less work for very long messages than the naively expected 2²⁵⁶ operations.

That's why NIST accounts for message length in the stated second-preimage-resistance strength for SHA-256.

For a fundamentals article, what matters most is:

Second-preimage resistance is its own security property, and its concrete security strength doesn't always have to match preimage resistance.

Does a Found Collision Also Mean a Second Preimage?​

No.

Suppose an attacker finds:

A ≠ B

with:

Hash(A) = Hash(B)

Then they've found a collision.

That doesn't automatically mean, though, that they can find a different message with the same hash for any arbitrary already-existing message X.

For the collision, they were allowed to choose A and B themselves.

In a second-preimage attack, on the other hand:

X

is already fixed.

That's a substantially different starting situation.

This is exactly why cryptography distinguishes between collision resistance and second-preimage resistance.

Why Is Second-Preimage Resistance Important?​

Hash values are often used to uniquely represent data or as a component of other cryptographic procedures.

If an attacker could simply produce a second message with the same hash value for an already fixed message, that could be problematic depending on the application.

Second-preimage resistance is meant to make exactly such attacks harder.

Together with:

  • preimage resistance
  • collision resistance

it's one of the fundamental security properties of cryptographic hash functions.

The Three Problems Compared​

At this point, the three terms can already be distinguished clearly.

Preimage Resistance​

given:

H

find:

x

such that:

Hash(x) = H

Second-Preimage Resistance​

given:

x

find:

y

such that:

x ≠ y

and

Hash(x) = Hash(y)

Collision Resistance​

find:

x and y

such that:

x ≠ y

and

Hash(x) = Hash(y)

The decisive difference is therefore:

PropertyWhat's given?What's being searched for?
Preimagehash valuematching input
Second preimageone inputsecond input with the same hash
Collisionnothing specifictwo different inputs with the same hash

These three tasks look similar, but they have different cryptographic security requirements.

The Key Takeaways​

Second-preimage resistance describes the difficulty of finding a different input with the same hash value for an already given input.

Given is:

x

We're looking for:

y

with:

x ≠ y

and

Hash(x) = Hash(y)

The most important points:

  • A specific first input is already given.
  • We're looking for a different input with the same hash value.
  • Second-preimage resistance is not the same as preimage resistance.
  • Second-preimage resistance is not the same as collision resistance.
  • In a normal collision attack, an attacker may search for both inputs.
  • A found collision therefore doesn't automatically give a second preimage for any arbitrary existing message.
  • For SHA-256, the second-preimage-resistance strength NIST states depends on the message length.
  • For SHA-256, NIST states this strength as 256 − L(M) bits.
  • Second-preimage resistance is one of the fundamental security properties of cryptographic hash functions.

One of the three central properties is still missing:

Collision resistance.

Where To Go From Here​

The next article is therefore about the question:

What Is Collision Resistance?

It also explains why SHA-256 produces a 256-bit-long hash but has a collision-resistance strength of 128 bits.

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 Definition and security strengths of preimage, second-preimage, and collision resistance.

  2. National Institute of Standards and Technology (NIST), CSRC Glossary – Second Preimage Resistance Definition of second-preimage resistance.

  3. National Institute of Standards and Technology (NIST), FIPS 180-4 – Secure Hash Standard (SHS) Official specification of SHA-256, including hash and block size.

  4. John Kelsey, Bruce Schneier, Second Preimages on n-bit Hash Functions for Much Less than 2^n Work, EUROCRYPT 2005. Scientific basis for generic second-preimage attacks on long messages and the message-length dependency NIST accounts for.