SHA-256 vs MD5 — Hash Algorithm Comparison for Developers
MD5 and SHA-256 are both cryptographic hash functions — they take arbitrary input and produce a fixed-length digest. But they differ significantly in output size, speed, and security. This guide compares them head-to-head, explains when to use each, and covers SHA-1 and SHA-512 in the broader landscape.
What Is a Hash Function?
A cryptographic hash function takes an input of any size and produces a fixed-length output (the hash or digest). Good hash functions have three properties:
- Deterministic — the same input always produces the same hash
- One-way — you cannot reverse a hash to find the input (preimage resistance)
- Collision resistant — it is computationally infeasible to find two different inputs with the same hash
MD5 satisfies the first two properties but has been broken for the third. SHA-256 satisfies all three.
MD5 Overview
MD5 (Message Digest Algorithm 5) was designed by Ron Rivest in 1991. It produces a 128-bit (16-byte) hash, typically shown as a 32-character hex string.
MD5 is fast — on modern hardware it can hash gigabytes per second. But practical collision attacks have been known since 2004, and chosen-prefix attacks since 2007. The security community considers MD5 broken for cryptographic purposes.
SHA-256 Overview
SHA-256 is part of the SHA-2 family, designed by NIST in 2001. It produces a 256-bit (32-byte) hash, shown as a 64-character hex string.
SHA-256 is the standard for digital signatures (TLS certificates, code signing, JWT HS256/RS256), blockchain (Bitcoin uses double-SHA-256), and file integrity verification where security matters.
Head-to-Head Comparison
| Property | MD5 | SHA-256 |
|---|---|---|
| Output length | 128 bits (32 hex chars) | 256 bits (64 hex chars) |
| Speed | Very fast (~600 MB/s) | Fast (~300 MB/s) |
| Collision resistance | Broken (practical attacks exist) | No known collisions |
| Security status | Cryptographically broken | Secure |
| Standard | RFC 1321 | FIPS 180-4 |
| Use in TLS/HTTPS | Deprecated | Standard |
| Use in code signing | No | Yes |
| Bitcoin | No | Yes (double SHA-256) |
| Year designed | 1991 | 2001 |
When to Use Which
Use MD5 for:
- File deduplication (comparing files for identical content)
- Cache keys and ETags (non-security checksums)
- Hash maps and data sharding (speed matters, not security)
- Legacy systems where you cannot change the algorithm
Use SHA-256 for:
- File integrity verification (download checksums)
- Digital signatures (JWT, code signing, TLS certificates)
- HMAC-SHA256 for API request signing
- Content addressing (Git object IDs, IPFS)
Do NOT use any SHA for passwords:
SHA algorithms are designed to be fast, which makes them vulnerable to brute-force attacks on passwords. Use bcrypt, Argon2, or PBKDF2 for password hashing — these are deliberately slow and include salting.
SHA-1 vs SHA-256 vs SHA-512
| Algorithm | Output | Security | Use today? |
|---|---|---|---|
| MD5 | 128 bits | Broken | Non-security only |
| SHA-1 | 160 bits | Deprecated | No (use SHA-256) |
| SHA-256 | 256 bits | Secure | Yes — default choice |
| SHA-512 | 512 bits | Secure | Yes — especially on 64-bit systems |
| SHA-3 (Keccak) | 224–512 bits | Secure | Yes — for post-SHA-2 needs |
Generating Hashes in JavaScript
Related Tools
Related Guides
FAQ
Is MD5 still safe to use?
MD5 is considered cryptographically broken. Collision attacks are practical — two different inputs can be crafted to produce the same MD5 hash. MD5 is still fine for non-security uses like checksums and deduplication, but must not be used for passwords, digital signatures, or any security-sensitive purpose.
Should I use SHA-256 or SHA-512?
Both are secure. SHA-512 produces a longer hash (512 bits vs 256 bits) and is actually faster on 64-bit processors because it operates on 64-bit words. SHA-256 is more universally supported and is the default choice for most applications. Use SHA-512 if you need extra margin or are on a 64-bit system doing bulk hashing.
Can SHA-256 be reversed?
No. SHA-256 is a one-way function — there is no algorithm to reverse it. However, short or common inputs can be found by brute force or rainbow tables. For passwords, always use a dedicated password hashing function like bcrypt, Argon2, or PBKDF2 — not raw SHA-256.
What is a hash collision?
A hash collision occurs when two different inputs produce the same hash output. MD5 is vulnerable to chosen-prefix collision attacks, meaning an attacker can craft two different documents with the same MD5 hash. SHA-256 has no known practical collisions.
How do I generate a SHA-256 hash in JavaScript?
Use the Web Crypto API: await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)). This is available in all modern browsers and Node.js 15+. Or use DevKit's hash generator for instant in-browser hashing.