Course contentsShow
← Security
Lesson 212 of 3,0525. Cryptographic Hashing and MACsFree lesson

Preimage and Second Preimage Attacks

Distinguishing between preimage resistance, second preimage resistance, and their security implications.

Preimage and Second Preimage Attacks

What you'll learn: You'll understand how preimage and second preimage attacks differ, what makes each dangerous, and why both resistances are critical for hash function security.


Understanding the Two Attack Types

Now that you know what preimage resistance and second preimage resistance mean individually, let's distinguish between the actual attacks on these properties:

Preimage Attack: An attacker has only a hash output h and tries to find any input m where hash(m) = h. Think of this like finding a key that opens a specific lock when you've only seen the lock, never the original key.

Second Preimage Attack: An attacker already knows one input m1 and its hash hash(m1), and tries to find a different input m2 where hash(m2) = hash(m1). This is like having a working key and trying to create a different key that opens the same lock.

Why the Distinction Matters

The difference is subtle but security-critical:

  • Preimage attacks threaten password storage. If an attacker steals a password hash database, they want to reverse hashes back to passwords.
  • Second preimage attacks threaten data integrity. If you've signed a legitimate document, an attacker wants to craft a fraudulent document with the same hash to forge your signature.

Second preimage attacks are typically easier than preimage attacks because the attacker has more information (the original input), but both should be computationally infeasible for secure hash functions.

Security Implications

For a hash with n-bit output:

  • Breaking preimage resistance requires approximately 2^n operations
  • Breaking second preimage resistance also requires approximately 2^n operations (though sometimes less with specific weaknesses)

If either resistance fails, the hash function is compromised for critical applications.

Key Takeaway: Preimage attacks reverse a hash without knowing any input, while second preimage attacks find a collision when given one input—both must be infeasible for a hash function to be secure.