MD5 vs SHA-256: Which Hash Should You Use?

MD5 has been broken since 2004 and is still the most common hash on the internet. What broken means, where it still works, and when you need SHA-256 (or something slower).

Same job, thirty years apart

Both functions take input of any length and produce a fixed-size digest: 128 bits (32 hex characters) for MD5, 256 bits (64 hex characters) for SHA-256. Both are deterministic and one-way, and both show the avalanche effect where one changed bit scrambles the whole output. The difference is what has happened to each since it was published.

What “broken” means for MD5

Hash functions promise three things. Preimage resistance: given a hash, you cannot find an input. Second-preimage resistance: given an input, you cannot find another with the same hash. Collision resistance: you cannot find any two inputs with the same hash.

MD5 lost collision resistance in 2004, when Wang Xiaoyun’s team produced colliding pairs on a normal PC. By 2008 researchers used that to forge a certificate authority certificate that browsers trusted. In 2012 the Flame malware carried a forged Microsoft signature built on an MD5 collision. Today a laptop finds a collision in seconds, and “chosen-prefix” collisions, where an attacker makes two files of their choosing collide, take a few hours of rented GPU time.

Preimage resistance, running the hash backwards, is still intact for MD5. That is why MD5 hashes of strong random data cannot be reversed, and why a “decrypt MD5” site is really a lookup table of hashes of common words.

SHA-256 today

SHA-256 was published in 2001 with a different internal structure and twice the state. No practical attack on any of its three properties exists; the best academic results reach 31 of its 64 rounds. It anchors TLS certificates, code signing, Git’s new object format and Bitcoin, and NIST considers it secure for the foreseeable future. Its successor SHA-3 exists as insurance, not as a replacement.

Where each one belongs

Use MD5 SHA-256 Notes
Detecting accidental file corruption Acceptable Better No adversary, so collisions do not matter. Use the file checksum tool.
Verifying a download against tampering No Yes An attacker can craft a malicious file with a matching MD5.
Cache keys, deduplication, ETags Acceptable Fine Internal use, no adversary.
Digital signatures, certificates Never Yes Collision attacks forge signatures.
HMAC message authentication Legacy only Yes HMAC-MD5 is technically still unbroken but has no reason to exist in new code. See the HMAC generator.
Password storage Never Never Both are far too fast. Use bcrypt, scrypt or Argon2id.
Content-addressed storage (Git, IPFS) No Yes Git is migrating from SHA-1 to SHA-256.

Speed, and why it cuts both ways

MD5 is roughly twice as fast as SHA-256 in software, which is why it lingers in checksum scripts. For hashing terabytes of backups, speed is a feature. For passwords, speed is the vulnerability: a GPU rig computes 100 billion MD5s per second against a leaked database. That is the reason password hashing has its own family of deliberately slow functions, covered in hashing and salting explained.

What about SHA-1?

Between the two in age and in status. Collisions were demonstrated in 2017 for about $100,000 of compute, now far less. Treat it as you would MD5: legacy checksums and Git ids only, nothing that an attacker could exploit. The SHA-1 generator exists for those legacy needs.

Decision in one line

If nobody is trying to fool you, MD5 is fine. If anybody might be, use SHA-256. If the input is a password, use neither. Compare all of them on the same input with the hash generator.

New tools and guides, about once a month.

No tracking pixels, no upsells. Unsubscribe with one click.

Newsletter