learn

MD5 vs SHA-256

MD5 has practical collisions and SHA-256 does not. Compare size, speed and security, see where MD5 is still fine and why neither should store passwords.

The short answer

Use SHA-256 whenever someone might try to cheat: file integrity, signatures, certificates, anything an attacker could craft. MD5 is broken for that purpose, because anyone can produce two different files with the same MD5 in seconds, but it is still acceptable as a fast checksum against accidental corruption or as a cache key. And for passwords, use neither: both are fast, and passwords need a slow, salted hash.

Side by side

MD5SHA-256
Published1992 (RFC 1321)2001 (SHA-2, FIPS 180-4)
Digest128 bits, 32 hex characters256 bits, 64 hex characters
Collisionspractical since 2004none known
Speedfaster in plain softwarefast; very fast on CPUs with SHA extensions
Safe for signaturesnoyes
Fine fornon-adversarial checksums, ETags, cache keysintegrity, signatures, HMAC, Git (SHA-256 mode), Bitcoin

For the text hello:

MD5     5d41402abc4b2a76b9719d911017c592
SHA-256 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Compute both, and the other algorithms, in the MD5 generator, the SHA-256 generator or the full hash generator.

How MD5 and SHA-1 broke

  • 2004: Xiaoyun Wang's team published the first MD5 collisions.
  • 2008: researchers used MD5 collisions to create a rogue certificate authority certificate trusted by browsers.
  • 2012: the Flame malware used a chosen-prefix MD5 collision to forge a Microsoft code-signing certificate.
  • 2017: Google and CWI published SHAttered, two PDFs with the same SHA-1; in 2020 a chosen-prefix SHA-1 collision followed.

A collision attack means two inputs with the same hash that the attacker prepares. Finding an input for a given hash (a preimage) is still impractical even for MD5, but collisions are enough to forge signed documents and certificates.

Where MD5 is still fine

  • Checking that a download was not corrupted by the network, when the checksum comes from a trusted place.
  • Cache keys, ETags and deduplication where nobody can choose the content to collide on purpose.
  • Legacy systems that require it, as long as no security decision depends on it.

For anything else, pick SHA-256, SHA-512 or SHA-3. For passwords, read how to store passwords.

FAQ

Can MD5 be reversed?

No. There is no way back from a digest; "MD5 decrypters" only look up hashes of common texts in huge tables, which is exactly why short passwords hashed with MD5 are found instantly.

Is SHA-256 safe for passwords?

Not on its own. It is designed to be fast, so attackers test billions of guesses per second. Use Argon2id, bcrypt, scrypt or PBKDF2 with a high cost.

Is SHA-256 slower than MD5?

In plain software MD5 is faster, but modern CPUs with SHA instructions compute SHA-256 at gigabytes per second. For almost every program the difference does not matter.

Should I replace MD5 checksums in my project?

If the checksum protects against tampering, yes. If it only detects accidental corruption and the value comes from a trusted source, it can stay.