Sub topic 12
When a hash breaks: MD5 and SHA-1
This chapter assumes you know the three cryptographic resistance properties (preimage, second-preimage, collision) and the birthday bound from earlier chapters. You should also be comfortable with Python's hashlib.
What "broken" means
A hash is broken when an attack finds a weakness faster than the generic cost for its output size. Broken does not mean every input can be reversed. For MD5 and SHA-1, the broken property is collision resistance. Preimage resistance still holds in practice.
| Hash | Output | Generic collision cost | Best known collision attack | Status |
|---|---|---|---|---|
| MD5 | 128 bits | about 2^64 | Seconds on a laptop | Broken |
| SHA-1 | 160 bits | about 2^80 | Practical, including chosen-prefix | Broken |
| SHA-256 | 256 bits | about 2^128 | None known | Sound |
How the attacks developed
- MD5. Wang and colleagues published practical collisions in 2004. By 2007 researchers could build chosen-prefix collisions, where an attacker picks two different prefixes and appends data that makes the hashes equal. In 2012 the Flame malware used an MD5 chosen-prefix collision to forge a Microsoft code-signing certificate.
- SHA-1. In 2017 the SHAttered project produced two different PDF files with the same SHA-1 digest. It took about 2^63 operations, far below 2^80. In 2020 the Shambles work made chosen-prefix collisions practical, which threatened PGP key signatures.
Why a collision matters
A collision attack needs the attacker to create both inputs. That is enough to break any scheme where a signature or checksum is trusted to identify one file.
Because the signature covers only the hash, it is equally valid for both files. A preimage attack is not needed.
What you can still use them for
Neither function is dangerous when no adversary is involved. Deduplication of your own files, cache keys, and detecting accidental corruption are all fine. Modern Python lets you state that intent:
import hashlib
data = b"report.pdf contents"
for name in ("md5", "sha1", "sha256"):
h = hashlib.new(name, data)
print(name, h.digest_size * 8, "bits", h.hexdigest()[:16])
# Non-security use: declares the hash is only a checksum.
cache_key = hashlib.md5(data, usedforsecurity=False).hexdigest()
print(cache_key)The usedforsecurity=False flag (Python 3.9 and later) lets the call work on FIPS-restricted systems. It also tells future readers the choice was deliberate.
Common errors
- Assuming "broken" means "reversible." Finding the input behind an MD5 digest is still infeasible, apart from guessing weak passwords. That is why unsalted MD5 password hashes fall to dictionary attacks, but for a different reason: MD5 is fast.
- Using HMAC-MD5 as a reason to keep MD5. HMAC does not depend on collision resistance, so HMAC-MD5 is not broken by these attacks. New designs should still pick SHA-256.
- Trusting a digest that came over the same channel as the file. A checksum next to a download protects only against corruption, not tampering.
- Ignoring legacy use. Git still uses SHA-1 for object IDs. It adds collision detection and is moving to SHA-256. NIST has deprecated SHA-1 and plans to retire it by 2030.
Exercise
A download page lists the MD5 of an installer on the same web page as the installer. Which attack does this fail to stop, and which does it still help with?
Show answer
It does not stop tampering. An attacker who can replace the installer can also replace the listed MD5, and MD5 collisions make crafted files easy to build. It still helps detect accidental corruption in transit, because random bit errors will not preserve the digest.