What Is Preimage Resistance?
An important security property of cryptographic hash functions is what's known as preimage resistance.
It concerns a task that sounds simple at first:
A hash value is known. Can I find an input that produces exactly this hash value?
For a cryptographically secure hash function, that should practically not be possible.
Simplified:
given:
hash value H
find:
input x
such that:
Hash(x) = H
This property is called preimage resistance.
What Is a Preimage?
Take a hash function:
x
↓
hash function
↓
H
The input x produces the hash value H.
That makes x a preimage of H.
In a preimage attack, we look at the situation exactly the other way around:
?
↓
hash function
↓
H
The hash value is already known.
We're looking for some input that produces exactly this hash value.
A preimage attack therefore doesn't necessarily try to "reverse" an original input. It looks for any input whose hash matches the given hash value.
An Example with SHA-256
Suppose we know the following SHA-256 hash:
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
We don't initially know which input produced this hash.
The task is:
Find x such that:
SHA-256(x)
=
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
One possible solution would be:
Hello World
because:
SHA-256("Hello World")
=
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
The decisive question, though, is:
How do I find such an input if I only know the hash value beforehand?
For a preimage-resistant hash function, there shouldn't be a practical general way to do that.
Why Can't You Just Reverse a Hash?
Hashing is sometimes described as a kind of one-way street.
That's a helpful mental model, but technically a bit oversimplified.
A hash function takes an input and calculates a hash value from it:
input
↓
hash function
↓
hash value
There is, however, no corresponding decryption algorithm:
hash value
↓
???
↓
original input
That's what distinguishes hashing from encryption, for example.
With encryption, the reverse direction is explicitly provided for:
plaintext
↓
encryption + key
↓
ciphertext
↓
decryption + key
↓
plaintext
For a hash function, there's no corresponding key that could simply decrypt the hash value again.
What fundamentally distinguishes hashing from encryption and encoding is explained in more detail in the article What Is a Hash Function?.
Why Can Different Inputs Have the Same Hash Value?
Hash functions map a very large input space onto a limited output space.
SHA-256 has:
2²⁵⁶
possible hash values.
For sufficiently large input spaces, however, there are more possible inputs than possible hash values.
That's why different inputs must exist that produce the same hash value.
This phenomenon is called a hash collision.
For a preimage attack, though, a different point matters:
We don't need to recover one particular original input.
It's enough to find any input that maps to the given hash value.
Why collisions are mathematically unavoidable, and why they represent a different cryptographic problem, is explained in the article What Is a Hash Collision?.
Preimage Resistance Doesn't Mean "Impossible"
The claim:
You can't reverse a hash.
is a common, simplified explanation, but technically too absolute.
Preimage resistance doesn't mean it's mathematically impossible to find a matching input.
It means, rather, that it should be practically impossible to find a matching input for a given hash value.
An attacker could, for example, try candidates:
Input 1 → hash → doesn't match
Input 2 → hash → doesn't match
Input 3 → hash → doesn't match
...
Input n → hash → matches the target hash
For a sufficiently large search space, though, this approach quickly becomes impractical.
Why hashes aren't decrypted, but attackers can still find matching inputs, is explained in the article Can You Reverse a Hash?.
How Difficult Is a Preimage Attack on SHA-256?
SHA-256 produces a hash value with 256 bits.
That means there are:
2²⁵⁶
possible hash values.
For an ideal 256-bit hash function, the generic effort to find a preimage with high probability is on the order of:
2²⁵⁶
hash operations.
NIST accordingly assigns SHA-256 a preimage-resistance strength of 256 bits.
The number 2²⁵⁶ is enormous.
It's approximately:
1.16 × 10⁷⁷
That's far beyond what could practically be exhaustively searched with classical computing technology.
Do You Really Need to Make Exactly 2²⁵⁶ Attempts?
No.
2²⁵⁶ describes the order of magnitude of the generic search effort.
A matching input could be found earlier in a specific search.
What matters, therefore, isn't that exactly 2²⁵⁶ calculations must be performed.
The figure of 256 bits of preimage security describes, rather, the order of magnitude of the security strength against a generic preimage attack.
What such bit figures mean is explained in the article How Secure Are 128, 256, and 512 Bits?.
Preimage Resistance Is Not Collision Resistance
Preimage resistance is easily confused with collision resistance.
The tasks, however, differ significantly.
Preimage
A specific hash value H is given:
given:
H
find:
x
such that:
Hash(x) = H
Collision
For a collision, no specific hash value is given.
We're looking for two different inputs:
find:
x ≠ y
such that:
Hash(x) = Hash(y)
That makes a big difference in the generic search effort.
For an ideal 256-bit hash, roughly:
| Property | Generic effort |
|---|---|
| Preimage | 2²⁵⁶ |
| Collision | 2¹²⁸ |
Why collisions already become relevant at the order of magnitude of 2¹²⁸ is explained in the article What Is a Hash Collision?.
Preimage Is Also Not Second Preimage
Another related property is second-preimage resistance.
In a preimage attack, a hash value is given:
H
We're looking for an x with:
Hash(x) = H
In a second-preimage attack, on the other hand, a specific input x is already known.
We're looking for a different input y with:
x ≠ y
and
Hash(x) = Hash(y)
These are different cryptographic problems.
I explain second-preimage resistance in more detail in the next article, What Is Second-Preimage Resistance?.
What Does This Have to Do with Passwords?
This is where a misunderstanding can easily arise.
If a password were simply hashed with a fast, general-purpose hash function like SHA-256, an attacker could, after a database leak, try known or likely passwords:
"password" → SHA-256 → compare
"123456" → SHA-256 → compare
"summer2026" → SHA-256 → compare
That doesn't contradict the preimage resistance of SHA-256.
The attacker isn't trying to search the entire theoretical space of a 256-bit hash.
They're exploiting the fact that human passwords often come from a much smaller and more predictable search space.
That's why a fast general-purpose hash function like SHA-256 shouldn't be used on its own to store passwords.
For passwords, dedicated password-hashing procedures are used that incorporate a salt and a cost factor, which deliberately makes individual guessing attempts more expensive. NIST describes the corresponding requirements in SP 800-63B.
I cover this topic in detail in a dedicated article on password hashing, not yet available in English.
Preimage Resistance Doesn't Mean Short Inputs Are Safe
Suppose a hash value is guaranteed to come from a four-digit PIN.
Then there are only:
0000
through
9999
that is:
10,000
possible inputs.
An attacker could try all possibilities:
0000 → hash
0001 → hash
0002 → hash
...
9999 → hash
and compare each resulting hash value with the target hash.
The cryptographic strength of the hash function doesn't change that.
The security strength of the hash function and the size, or entropy, of the input space are two different things.
The preimage resistance of a hash function therefore doesn't automatically protect a small or predictable input space.
The security of a concrete system doesn't depend only on the hash algorithm used.
Why Is Preimage Resistance Important?
Preimage resistance prevents a matching input from being easily constructed from a known hash value.
That matters for many cryptographic applications.
Hash functions are used as building blocks in numerous cryptographic procedures.
If an attacker could produce a matching input for a given hash value with practical effort, security assumptions built on top of that could be violated.
Preimage resistance is therefore one of the fundamental security properties of cryptographic hash functions.
Preimage Resistance and the Avalanche Effect Are Not the Same
The avalanche effect is also an important property of cryptographic hash functions, but it describes something different.
The avalanche effect is about how much the hash value changes when the input is changed slightly.
Preimage resistance, on the other hand, asks:
Can I find a matching input for an already-given hash value?
Good avalanche behavior therefore doesn't prove preimage resistance.
How even a single changed bit can affect a SHA-256 hash is shown in the article What Is the Avalanche Effect?.
The Key Takeaways
Preimage resistance describes the difficulty of finding a matching input for a given hash value.
Given is:
H
We're looking for:
x
with:
Hash(x) = H
The most important points:
- In a preimage attack, the hash value is already given.
- We're looking for any input that produces this hash value.
- It doesn't have to be the originally used input.
- Preimage resistance doesn't mean the search is mathematically impossible.
- For an ideal 256-bit hash, the generic search effort is on the order of
2²⁵⁶. - SHA-256 is assigned a preimage-resistance strength of 256 bits.
- Preimage resistance is not the same as collision resistance.
- Preimage resistance is not the same as second-preimage resistance.
- Good avalanche behavior also doesn't prove preimage resistance.
- Small or predictable input spaces can still be searched by trying candidates.
- That's why SHA-256 alone, for example, isn't a suitable method for storing passwords.
The decisive question, then, isn't:
Can any input produce this hash value?
But rather:
Can I find such an input with realistically available resources?
Where To Go From Here
Preimage resistance is one of several important security properties of cryptographic hash functions.
Next up is the related question:
What Is Second-Preimage Resistance?
After that, the three properties can be directly compared:
Preimage resistance
↓
Second-preimage resistance
↓
Collision resistance
Other relevant topics:
- What Is Collision Resistance?
- What Is a Hash Collision?
- How Secure Are 128, 256, and 512 Bits?
- Why isn't SHA-256 suitable for passwords?
- How Does SHA-256 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), CSRC Glossary – Preimage Resistance Definition of preimage resistance.
-
National Institute of Standards and Technology (NIST), Hash Functions Overview of cryptographic hash functions and the security strengths of SHA-2 and SHA-3.
-
National Institute of Standards and Technology (NIST), SP 800-107 Rev. 1 – Recommendation for Applications Using Approved Hash Algorithms NIST recommendations on security properties and applications of cryptographic hash algorithms. NIST announced in late 2022 that it would withdraw this publication; relevant content was moved to the current NIST Hash Functions page.
-
National Institute of Standards and Technology (NIST), SP 800-63B – Authentication and Lifecycle Management Current NIST requirements and recommendations for the secure storage of passwords.