MD5 vs SHA-256: Hash Sizes, Security and When to Use Each
MD5 vs SHA-256 compared: digest sizes, which algorithms have known collisions, what MD5 and SHA-1 are still fine for, and why none should store passwords.
Updated
MD5 and SHA-256 are both hash functions: they turn any input into a short fixed-length fingerprint. MD5 produces a 128-bit digest and is broken for security purposes, because attackers can create two different inputs with the same MD5 hash in seconds. SHA-256 produces a 256-bit digest, has no known practical attacks, and is the standard choice today for signatures, certificates and file verification.
What is a hash?
A hash function takes input of any size, a password, a file, an entire disk image, and returns a fixed-size value called a digest or simply a hash. A cryptographic hash function is designed to have these properties:
- Deterministic. The same input always produces the same digest.
- Fixed length. One byte or ten gigabytes in, the same number of bits out.
- Avalanche effect. Changing a single character of input changes about half the output bits.
- One-way. Given a digest, there is no feasible way to work out an input that produces it.
- Collision resistant. It should be infeasible to find any two different inputs with the same digest.
Here is the word hello through three algorithms:
- MD5:
5d41402abc4b2a76b9719d911017c592 - SHA-1:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d - SHA-256:
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Capitalize one letter, Hello, and the MD5 becomes 8b1a9953c4611296a827abf8c47804d7. Nothing about the two digests suggests the inputs were nearly identical.
Hashing vs encryption
The two are often confused because both produce unreadable output. They do opposite jobs:
| Aspect | Hashing | Encryption |
|---|---|---|
| Reversible | No | Yes, with the key |
| Uses a key | No | Yes |
| Output size | Fixed | Grows with the input |
| Purpose | Verify that data has not changed | Keep data secret |
| Examples | MD5, SHA-1, SHA-256 | AES, RSA, ChaCha20 |
There is no such thing as "decrypting" a hash. Sites that appear to reverse MD5 are looking the digest up in a table of previously hashed common inputs. Encoding is a third, separate thing: Base64 is neither hashing nor encryption, just a reversible change of representation, as Base64 encoding explained describes.
MD5, SHA-1 and SHA-256 compared
| Property | MD5 | SHA-1 | SHA-256 |
|---|---|---|---|
| Digest size | 128 bits | 160 bits | 256 bits |
| Hex length | 32 characters | 40 characters | 64 characters |
| Introduced | 1991, by Ron Rivest | 1995, by NIST | 2001, by NIST, as part of SHA-2 |
| Collisions found | Yes, 2004 | Yes, 2017 | None known |
| Safe for signatures and certificates | No | No | Yes |
| Relative speed | Fastest | Fast | Slower, still very fast |
SHA-256 is one member of the SHA-2 family, alongside SHA-384 and SHA-512, which give longer digests of 96 and 128 hex characters.
What "broken" means
A collision is two different inputs with the same digest. For an ideal hash with an n-bit output, finding one by brute force takes about 2 to the power n/2 attempts: 2^64 for MD5, 2^80 for SHA-1, 2^128 for SHA-256. An algorithm is considered broken when someone finds a shortcut well below that.
- MD5. Researchers led by Xiaoyun Wang published collisions in 2004. Attacks improved quickly, and collisions can now be generated in seconds on an ordinary computer. In 2008 a team used MD5 collisions to create a rogue certificate authority certificate, and in 2012 the Flame malware used one to forge a Microsoft code-signing certificate.
- SHA-1. In 2017 Google and CWI Amsterdam announced SHAttered, two different PDF files with the same SHA-1 digest. Major browsers stopped trusting SHA-1 certificates that same year, and NIST has said SHA-1 should be phased out entirely by the end of 2030.
- SHA-256. No collision has been found, and no attack on the full algorithm comes anywhere near practical.
Collisions matter wherever a hash stands in for a document someone else supplied. If a signature covers only the digest, an attacker who can prepare two files with the same digest gets a harmless one signed and then substitutes the malicious one.
Note the limit of these attacks. They let an attacker create two colliding inputs of their own choosing. Finding an input that matches a given existing digest, a preimage attack, is still impractical even for MD5. That distinction is why MD5 remains usable in some settings.
What each is still fine for
MD5 is acceptable for:
- Detecting accidental corruption in a download or copy, where nobody is trying to fool you.
- Deduplication, cache keys, ETags and hash-based partitioning in systems you control.
- Matching against legacy systems that publish only MD5 checksums.
SHA-1 is in the same position. It survives in older protocols, and Git still identifies objects by SHA-1 by default, hardened with collision detection, with SHA-256 repositories available as an option. Do not choose it for anything new.
Use SHA-256, or another SHA-2 or SHA-3 function, for:
- Verifying downloads against a publisher's checksum when tampering is a concern.
- Digital signatures, TLS certificates and code signing.
- HMAC message authentication, such as the
HS256signatures in JWTs covered in how to decode a JWT.
Why none of them should store passwords
It is tempting to conclude that SHA-256 fixes password storage. It does not. MD5, SHA-1 and SHA-256 are all designed to be fast, and speed is the wrong property here. Someone who steals a database of password hashes can try billions of guesses per second on graphics cards, and most human-chosen passwords fall quickly.
Passwords need a dedicated password hashing function: Argon2id, bcrypt, scrypt or PBKDF2. These are deliberately slow and tunable, some also demand a lot of memory, and each uses a salt, a random value stored with every password so that identical passwords produce different hashes and precomputed tables are useless. Use your framework's built-in password hashing rather than assembling one from a general-purpose hash. For the user's side of the problem, see how to create a strong password.
Fast hashes are for data. Slow, salted hashes are for passwords. Using the first kind for the second job is the mistake, whichever algorithm you pick.
Computing and comparing hashes
To verify a downloaded file, hash the file itself with a command-line tool and compare the result with the published value: sha256sum file.iso on Linux, shasum -a 256 file.iso on macOS, and Get-FileHash file.iso in PowerShell, which defaults to SHA-256. Replace the algorithm name to get MD5 or SHA-1.
For text, the Hash Generator computes MD5, SHA-1, SHA-256, SHA-384 and SHA-512 for whatever you type, all at once, so you can compare digest lengths directly and copy the one you need. It hashes the text you enter; for files, use the commands above. Remember that a trailing newline or a different character encoding changes the input and therefore the digest, which is the usual reason two hashes of "the same" string disagree.
Try it
Type hello into the Hash Generator and check the three digests against the ones on this page. Then change a single letter and watch every hash change completely: that is the avalanche effect, and it is why a hash is such a reliable fingerprint.