Hash Functions Explained: MD5 vs SHA-1 vs SHA-256
Hash functions are the unsung heroes of modern security. Every time you download a file, log into a website, or verify data integrity, hash functions are working behind the scenes to keep your data secure.
But not all hash functions are created equal. MD5? Broken since 2004. SHA-1? Deprecated by Google in 2017. SHA-256? Still going strong. In this guide, I'll explain what hash functions are, compare the major algorithms, and help you choose the right one for your use case.
What is a Hash Function?
A cryptographic hash function takes an input (any data—text, files, images) and produces a fixed-size string of characters called a hash or digest. Think of it as a digital fingerprint.
Key Properties
- Deterministic: Same input always produces same hash
- Fixed Size: Output is always the same length (MD5=128 bits, SHA-256=256 bits)
- Fast to Compute: Generating a hash is quick
- One-Way: Can't reverse the hash to get original input
- Avalanche Effect: Tiny input change = completely different hash
- Collision Resistant: Extremely hard to find two inputs with same hash
Example
Input: "Hello World"
MD5: b10a8db164e0754105b7a99be72e3fe5
SHA-1: 0a4d55a8d778e5022fab701977c5d840bbc486d0
SHA-256: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
Change one character:
Input: "Hello World!"
SHA-256: 7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069
Completely different hash! This is the avalanche effect.
MD5 (Message Digest 5)
Created: 1991 by Ronald Rivest
Output Size: 128 bits (32 hex characters)
Status: ❌ Cryptographically broken
Why MD5 is Broken
In 2004, researchers demonstrated practical collision attacks—finding two different inputs that produce the same MD5 hash. By 2008, attackers created fake SSL certificates using MD5 collisions.
When to Use MD5:
- ✅ Non-cryptographic checksums (file integrity checks where attacker isn't a concern)
- ✅ Cache keys or hash tables
- ❌ Password hashing
- ❌ Digital signatures
- ❌ SSL certificates
SHA-1 (Secure Hash Algorithm 1)
Created: 1995 by NSA
Output Size: 160 bits (40 hex characters)
Status: ⚠️ Deprecated but still used
The 2017 Collision
Google's SHAttered attack in 2017 proved SHA-1 collisions were practical. Major browsers stopped trusting SHA-1 certificates.
Git Still Uses SHA-1: Git is migrating to SHA-256 but still uses SHA-1 for commit hashes. It's considered safe for Git because generating malicious collisions requires knowing the exact content—impractical for version control.
When to Use SHA-1:
- ✅ Legacy systems (if you must)
- ✅ Git commit hashes (being phased out)
- ❌ New applications
- ❌ Password hashing
SHA-256 (SHA-2 Family)
Created: 2001 by NSA
Output Size: 256 bits (64 hex characters)
Status: ✅ Secure and widely recommended
SHA-256 is part of the SHA-2 family (SHA-224, SHA-256, SHA-384, SHA-512). It's the gold standard for modern cryptographic hashing.
Where SHA-256 is Used
- Bitcoin: Proof-of-work mining
- SSL/TLS: Certificate signatures
- Docker: Image layer verification
- File Integrity: Download checksums
When to Use SHA-256:
- ✅ Digital signatures
- ✅ SSL/TLS certificates
- ✅ Blockchain applications
- ✅ File integrity verification
- ⚠️ Password hashing (use bcrypt/Argon2 instead—see below)
SHA-512 (Stronger SHA-2)
Output Size: 512 bits (128 hex characters)
Status: ✅ Very secure, overkill for most uses
SHA-512 is more secure than SHA-256 but slower. Use it when you need maximum security and performance isn't critical.
Password Hashing: A Special Case
⚠️ Important: Don't use MD5, SHA-1, or even SHA-256 for password hashing!
Why Fast Hashes Are Bad for Passwords
Cryptographic hashes are designed to be FAST. That's great for file verification but terrible for passwords. Fast hashing means attackers can test billions of passwords per second using GPUs.
Use These Instead
- bcrypt: Intentionally slow, configurable work factor (industry standard)
- Argon2: Winner of 2015 Password Hashing Competition (best modern choice)
- scrypt: Memory-hard algorithm (good for high-value targets)
- PBKDF2: Older but still acceptable with high iteration count (100,000+)
🔐 Generate Hashes Instantly
Use our free Hash Generator to create MD5, SHA-1, SHA-256, and SHA-512 hashes for files and text. All processing happens locally—your data never leaves your browser.
Generate Hashes →Comparison Table
| Algorithm | Output Size | Security | Speed | Use Cases |
|---|---|---|---|---|
| MD5 | 128 bits | ❌ Broken | Very Fast | Checksums only |
| SHA-1 | 160 bits | ⚠️ Deprecated | Fast | Legacy only |
| SHA-256 | 256 bits | ✅ Secure | Fast | General purpose |
| SHA-512 | 512 bits | ✅ Very Secure | Medium | High security |
Real-World Examples
File Integrity Verification
# Download file
curl -O https://example.com/file.zip
# Generate SHA-256 hash
shasum -a 256 file.zip
# Compare with published hash
# If hashes match → file is authentic
Git Commit Hashes
commit 0a4d55a8d778e5022fab701977c5d840bbc486d0
Author: John Doe
Date: Sun Jan 12 2026
This is a SHA-1 hash of the commit content
Bitcoin Mining
Bitcoin miners repeatedly hash block data with different nonces until they find a hash starting with enough zeros (proof-of-work).
Common Hash Function Mistakes
Mistake #1: Using MD5 for Security
// DON'T
const hash = md5(password); // Broken!
// DO
const hash = await bcrypt.hash(password, 12);
Mistake #2: Not Salting Hashes
If you hash the same password twice, you get the same hash. Attackers use "rainbow tables" of precomputed hashes to crack passwords instantly.
// Bad: No salt
hash("password123") → always same hash
// Good: With random salt
hash("password123" + "randomsalt123") → unique hash
Mistake #3: Comparing Hashes with ==
Use timing-safe comparison to prevent timing attacks:
// Bad
if (hash1 === hash2) // Vulnerable to timing attacks
// Good
if (crypto.timingSafeEqual(hash1, hash2)) // Constant-time comparison
Conclusion: Choose the Right Hash
Quick Decision Guide:
- Password Hashing: bcrypt or Argon2
- General Cryptography: SHA-256
- Maximum Security: SHA-512
- File Checksums (non-adversarial): MD5 acceptable, SHA-256 better
- Legacy Systems: Migrate away from MD5/SHA-1 ASAP
Remember: Hashing is encoding + one-way transformation. It's not encryption (two-way), and it's not obfuscation (security through obscurity). Use the right tool for the right job, and when in doubt, go with SHA-256.
🚀 Try Our Free Developer Tools
Generate hashes, encode/decode data, format code, and more—all processed locally in your browser for maximum privacy.
Explore Tools →