toolhq.io

All posts
4 min readby Jameel Haider

What is a hash collision

A hash function turns input of any length into a fixed size output. Because the inputs are unlimited and the outputs are not, two different inputs can produce the same hash. That is a collision, and understanding when it matters is the difference between trusting a hash and being caught out by one.

Why collisions must exist

This is not a flaw that better engineering can remove. It follows directly from the pigeonhole principle: if you put more items into fewer boxes, at least one box holds more than one item.

A hash with a 256 bit output has 2^256 possible results. That is an enormous number, but it is still finite. The set of possible inputs, every file and string of any length, is infinite. Mapping an infinite set onto a finite one guarantees that some inputs land on the same output. Collisions are mathematically certain for any hash function; the only question is how hard they are to find.

Accidental collisions

An accidental collision is two unrelated inputs happening to hash the same by chance. For a good hash with a large output, this is so unlikely that you can plan as if it never happens.

  • A 256 bit hash has about 1.2 x 10^77 possible values.
  • You would need to hash on the order of 2^128 random inputs before a chance collision became likely, far more than any system will ever process.

This is why content addressing, deduplication, and integrity checks rely on hashes. The odds of two different real files colliding by accident are negligible.

Adversarial collisions

The dangerous case is when an attacker deliberately constructs two inputs that collide. Here the question is not probability but the strength of the algorithm, and some older algorithms have been broken.

  • MD5 has had practical collision attacks for years. An attacker can craft two different files with the same MD5, so MD5 must not be used where an attacker could benefit, such as signing or certificates.
  • SHA-1 is also broken for collisions, demonstrated by real crafted PDF files sharing a SHA-1 digest. It has been retired from security use.

For current work, use SHA-256 or stronger. No practical collision attack is known against the SHA-2 family, so a crafted collision is not feasible with today's methods.

Why the distinction matters

The two cases call for different thinking. Accidental collisions are a probability question, and a wide modern hash makes them irrelevant. Adversarial collisions are a cryptographic question, and the answer depends entirely on whether the algorithm has been broken.

Integrity check against random error  → any decent hash is fine
Signature an attacker wants to forge  → only a collision-resistant hash

Choosing the right algorithm is where this becomes concrete. The Hash Generator computes MD5, SHA-1, SHA-256, and more so you can compare outputs and see the fixed length for yourself, all in your browser so the input never leaves your device. For a side by side on strength, see MD5 vs SHA-1 vs SHA-256.

The short version: collisions always exist, accidental ones are too rare to worry about with a wide hash, and the real risk is a deliberately crafted collision against a broken algorithm. Pick SHA-256 or better for anything an attacker might target.