Security
What is a collision?
A hash function is meant to give every different input its own fingerprint. A collision is when two different inputs give the same one. They always exist in theory, because there are endless possible inputs and only a fixed number of digests. For a strong hash they are impossible to find in practice. For MD5 they can be made on demand, in seconds, and the examples below are real.
Two different inputs, one MD5
These two 128-byte blocks are not text, so they are shown as hexadecimal. They are different, and the bytes that differ are highlighted. MD5 gives both of them exactly the same result.
Input A
2f ca b5 87 12 46 7e ab 40 04 58 3e b8 fb 7f 89
55 ad 34 06 09 f4 b3 02 83 e4 88 83 25 71 41 5a
08 51 25 e8 f7 cd c9 9f d9 1d bd f2 80 37 3c 5b
d8 82 3e 31 56 34 8f 5b ae 6d ac d4 36 c9 19 c6
dd 53 e2 b4 87 da 03 fd 02 39 63 06 d2 48 cd a0
e9 9f 33 42 0f 57 7e e8 ce 54 b6 70 80 a8 0d 1e
c6 98 21 bc b6 a8 83 93 96 f9 65 2b 6f f7 2a 70
Input B
2f ca b5 07 12 46 7e ab 40 04 58 3e b8 fb 7f 89
55 ad 34 06 09 f4 b3 02 83 e4 88 83 25 f1 41 5a
08 51 25 e8 f7 cd c9 9f d9 1d bd 72 80 37 3c 5b
d8 82 3e 31 56 34 8f 5b ae 6d ac d4 36 c9 19 c6
dd 53 e2 34 87 da 03 fd 02 39 63 06 d2 48 cd a0
e9 9f 33 42 0f 57 7e e8 ce 54 b6 70 80 28 0d 1e
c6 98 21 bc b6 a8 83 93 96 f9 65 ab 6f f7 2a 70
Two different .txt files, one MD5
The same trick works on whole files. MD5 processes data in blocks, so once two inputs collide, anything you append to both keeps them colliding. These two files start with the colliding blocks above, followed by identical readable text. Download them and compare the checksums yourself.
collision-a.txt · 322 bytes
collision-b.txt · 322 bytes
Check it yourself
# Linux / macOS
md5sum collision-a.txt collision-b.txt
sha256sum collision-a.txt collision-b.txt
# Windows PowerShell
Get-FileHash -Algorithm MD5 collision-a.txt, collision-b.txt
Get-FileHash -Algorithm SHA256 collision-a.txt, collision-b.txt
The MD5 values match and the SHA-256 values do not. You can also drop either file into the Hash Generator.
The files will look like gibberish at the start when opened in a text editor. That is the colliding block, which is binary data, so the files are not really plain text even though they end in .txt.
What this does and doesn't show
- Collision attack: someone makes two inputs with the same hash. This is what is shown here, and it is practical for MD5.
- Preimage attack: someone finds an input that matches a hash they were given. This is still not practical for MD5, so an MD5 digest cannot simply be reversed.
The danger is when an attacker gets to choose the files. They can prepare a harmless version for review or signing and a malicious version with the same MD5, so a matching checksum proves nothing. This is how a forged CA certificate was built in 2008. This pair is the original from the 2004 research. Later attacks (chosen-prefix collisions) let attackers pick two different readable documents and add the collision blocks afterwards, which is far more practical to abuse.
MD5 is still fine for spotting accidental corruption or as a cache key. For anything security-related, use SHA-256 or stronger. SHA-1 has had its own practical collision since 2017 (“SHAttered”). See the Hash Generator for more on choosing an algorithm.