MD5 vs SHA-256: What a Hash Is and Which One to Use
MD5 gives 128 bits, SHA-256 gives 256. MD5 and SHA-1 are broken against attackers but fine for catching accidental corruption. Real hashes of 'hello'.
Use SHA-256 for anything where someone could be trying to fool you, and treat MD5 and SHA-1 as checksums only. MD5 produces a 128-bit hash, SHA-1 a 160-bit hash and SHA-256 a 256-bit hash. MD5 and SHA-1 are broken against a deliberate attacker, but they still catch accidental corruption just fine. None of the three is the right tool for storing passwords.
What a hash is
A hash function turns input of any length into a fixed-length fingerprint called a digest. The same input always gives the same digest, a small change in the input changes the digest completely, and you cannot work backwards from the digest to the input. Digests are usually written as hexadecimal, four bits per character.
Here is the word hello run through three algorithms, computed with Python’s hashlib:
| Algorithm | Bits | Hex characters | Digest of hello |
|---|---|---|---|
| MD5 | 128 | 32 | 5d41402abc4b2a76b9719d911017c592 |
| SHA-1 | 160 | 40 | aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d |
| SHA-256 | 256 | 64 | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 |
The length of the output never depends on the length of the input. A one-word string and a multi-gigabyte file both give 64 hex characters under SHA-256.
The avalanche effect
Change a single letter and nothing about the digest stays recognisable. Hashing Hello with a capital H instead of hello:
| Algorithm | Digest of Hello |
Bits that differ from hello |
|---|---|---|
| MD5 | 8b1a9953c4611296a827abf8c47804d7 |
63 of 128 |
| SHA-1 | f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 |
75 of 160 |
| SHA-256 | 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969 |
125 of 256 |
In each case about half of the bits flip, which is what a good hash should do. Close inputs give no hint that they are close.
What “broken” means
A secure hash is supposed to resist three attacks:
- Preimage: given a digest, find any input that produces it.
- Second preimage: given one input, find a different input with the same digest.
- Collision: find any two different inputs with the same digest.
Collisions are the easiest of the three because of the birthday effect: for an n-bit hash an attacker needs about 2^(n/2) tries by brute force, not 2^n. That is 2^64 (18,446,744,073,709,551,616) for MD5’s 128 bits and 2^128 (about 3.4 × 10^38) for SHA-256’s 256 bits. The first is a number a well-funded attacker can plan around, and the second is not.
MD5 fell much further than brute force. Researchers published practical collisions in 2004, and today a collision can be produced on ordinary hardware. RFC 6151 updates the security considerations of the original MD5 specification, RFC 1321, to describe these collision attacks. SHA-1 followed: the SHAttered project published the first practical collision in 2017, and NIST has deprecated it for security uses. SHA-256 belongs to the SHA-2 family specified in FIPS 180-4, and no practical attack on its collision resistance is known.
Why MD5 is still fine for checksums
An attacker who can craft a collision is a threat model. A flaky network, a bad disk sector or an interrupted download is not. Random corruption will not accidentally produce a matching MD5, so MD5 and SHA-1 still work for:
- Checking that a file copied or downloaded without error.
- Finding duplicate files in your own folders.
- Cache keys, ETags and database partitioning, where nobody benefits from forging a match.
The moment an attacker could swap the file, the picture changes. If a download page publishes an MD5 next to a file and both come from the same server, a person who can replace the file can replace the checksum too. And if the digest is used to prove authenticity, such as in a signature or a certificate, a collision attack lets someone create two documents with the same hash, show you the harmless one and substitute the other later. Use SHA-256 there.
Which one to use
| Situation | Use |
|---|---|
| Verifying a download against a published checksum | SHA-256 if offered; MD5 only if that is all the publisher gives |
| Detecting accidental corruption or duplicates | MD5, SHA-1 or SHA-256 all work; SHA-256 costs little more |
| Digital signatures, certificates, integrity under attack | SHA-256 or stronger |
| Message authentication | HMAC with SHA-256 and a secret key |
| Storing passwords | None of these. Use a slow password hash |
For passwords, a general-purpose hash is the wrong tool precisely because it is fast. An attacker with a stolen database can test billions of guesses per second against SHA-256 or MD5 on a modern graphics card. Password storage needs an algorithm that is deliberately slow and memory-hungry and that adds a unique salt for each user: Argon2id, scrypt or bcrypt are the usual choices, with PBKDF2 where a standard is required. Do not roll your own scheme out of SHA-256.
Try it
Paste text or drop a file into the hash generator to compute MD5, SHA-1, SHA-256 and the other digests side by side, add an HMAC key, and compare a result against a published checksum. Hashing hello there reproduces the digests in the table above.
Updated 2026-11-02