SHA-256 vs MD5: Which Is Broken, and What For
MD5 and SHA-1 are broken, SHA-256 is not, and none of the three should hash a password. What each one is actually safe to do.
· 3 min read
A hash function takes input of any size and returns a fixed-length fingerprint. The same input always gives the same fingerprint, and changing a single bit changes roughly half the output bits — an effect usually called the avalanche property. What separates a usable hash from a broken one is not speed or output length, but whether anyone has found a way to produce two inputs with the same fingerprint on purpose.
What "broken" actually means
Two different inputs sharing a fingerprint is called a collision. With a large enough output, collisions exist mathematically but cannot be found in any practical amount of time. A hash is broken when someone finds a shortcut — a method that produces collisions to order rather than by brute force.
MD5 fell to exactly that in 2004, and the attack has only got cheaper since; collisions can now be produced on ordinary hardware in seconds. SHA-1 followed in 2017, when a research team demonstrated two different PDF files with identical SHA-1 digests. SHA-256 has no such shortcut and, after two decades of public analysis, no credible sign of one.
Why a collision is worse than it sounds
The danger is not that two random files might clash. It is that an attacker who controls both inputs can prepare a harmless document and a malicious one that hash identically. Anything that trusts the fingerprint as proof of identity — a signature, a certificate, an integrity check, a deduplication key — will treat the two as the same object. That is why a broken hash is unusable for security even though it still computes fine.
The mistake that matters most: passwords
Reaching for SHA-256 to store passwords is the most common misuse of a good hash, and it is a serious one. SHA-256 is designed to be fast, which is exactly what you want for verifying a download and exactly what you do not want for a password. Speed is the attacker asset: modern hardware tries billions of SHA-256 guesses per second against a stolen database.
Password hashing needs an algorithm that is deliberately slow and deliberately memory-hungry, with a per-user salt and a cost factor you can raise as hardware improves. bcrypt, scrypt and Argon2 exist for this and nothing else. A general-purpose hash, however strong, is the wrong tool.
For file integrity you want the fastest secure hash you can get. For passwords you want the slowest one you can afford. Same primitive, opposite requirements.
What each one is fine for
- SHA-256 — signatures, certificates, integrity checks, content addressing, commit identifiers. The sensible default.
- SHA-384 and SHA-512 — the same guarantees with a longer digest; on 64-bit hardware SHA-512 is often faster than SHA-256.
- SHA-1 — legacy compatibility only, and never where an adversary controls the input.
- MD5 — non-adversarial checksums against accidental corruption, cache keys, and nothing else.
Hashing is not encryption
Encryption is reversible with a key; that is its purpose. Hashing is not reversible at all, because output far smaller than input cannot possibly contain it. When a site claims to have recovered a hashed value, it did not decrypt anything — it guessed inputs until one produced a matching fingerprint. That works precisely because unsalted fast hashes of common passwords have already been tabulated, which is the whole argument for salt.
Reading a digest
Digests are shown as hexadecimal, so the character count is twice the byte count: MD5 is 32 characters, SHA-1 is 40, SHA-256 is 64. Length alone tells you which algorithm produced a value, and it tells you nothing about whether that algorithm is still safe. Comparing two digests should be a constant-time comparison in security contexts, since a naive one leaks how many leading characters matched.
Frequently asked questions
- Is MD5 safe for checking a file downloaded correctly?
- Against accidental corruption, yes — a truncated or bit-rotted file will not match. Against a deliberate attacker, no, because they can craft a malicious file with the same MD5 as the legitimate one. Prefer SHA-256 where the source is not fully trusted.
- Can a SHA-256 hash be reversed?
- Not by any known method. Recovery attacks work by guessing inputs and comparing digests, which succeeds only when the input was predictable. That is why passwords need a salt and a deliberately slow algorithm rather than a fast general-purpose hash.
- Should I use SHA-512 instead of SHA-256?
- Only if you have a reason. Both are considered secure and SHA-512 is often faster on 64-bit hardware, but SHA-256 is more widely supported and its shorter digest is easier to store and transmit.
Related reading
- 3 min read
camelCase, snake_case or kebab-case: Which Goes Where
Naming conventions are not preferences. Each language and format has one the tooling expects, and going against it costs more than it looks.
- 3 min read
encodeURI or encodeURIComponent? One Rule That Works
The two differ by eleven characters, and picking wrong either breaks the URL or corrupts the value. Here is the rule, and the plus-sign trap.
- 3 min read
Favicon Sizes You Actually Need
Generators offer dozens of sizes and almost nobody needs them all. Which files earn their place, which link tags matter, and why 16px decides it.
- 3 min read
How Much Can You Compress an Image Before It Shows?
The quality number is not a percentage of anything. What the artefacts look like, where they appear first, and how to find your own limit.