MD5 vs SHA256 vs bcrypt: Which Hash Function to Use
Hash functions are the silent workhorses of modern security. They verify file integrity, secure passwords, sign digital documents, and power blockchain consensus. But not all hash functions are created equal.
Using MD5 for password storage is like using a padlock to guard a vault. Using bcrypt for file checksums is like using a vault to guard a padlock. Each hash function has a specific purpose, and using the wrong one creates unnecessary risk or overhead.
This guide compares the most common hash functions and tells you exactly which one to use for each scenario.
What Is a Hash Function
A hash function takes an input of any length and produces a fixed-length output called a digest or hash. Good hash functions have three critical properties:
- Deterministic - The same input always produces the same output
- One-way - You cannot reverse the hash to recover the original input
- Collision-resistant - It is computationally infeasible to find two different inputs that produce the same hash
MD5 - Fast but Broken
MD5 produces a 128-bit (32-character hex) hash. It was designed in 1991 by Ronald Rivest and was the standard for over a decade.
Input: "hello world"
MD5: 5eb63bbbe01eeed093cb22bb8f5acdc3
Why MD5 Is Insecure
MD5's collision resistance was broken in 2004. Researchers demonstrated that two different files could produce the same MD5 hash. By 2008, researchers created a rogue CA certificate by exploiting MD5 collisions.
MD5 should never be used for:
- Password hashing
- Digital signatures
- Certificate verification
- Any security-critical application
MD5 is acceptable for:
- Non-security checksums (verifying download integrity where tampering is not a concern)
- Cache invalidation keys
- Non-cryptographic deduplication
SHA-256 - The Current Standard
SHA-256 is part of the SHA-2 family designed by the NSA. It produces a 256-bit (64-character hex) hash and is the backbone of TLS, code signing, and blockchain.
Input: "hello world"
SHA-256: b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
SHA-256 Strengths
- No known practical collision attacks
- Widely supported across every platform and language
- Required by security standards (PCI DSS, HIPAA, GDPR)
- Powers Bitcoin and most blockchain systems
SHA-256 vs SHA-512
SHA-512 produces a 512-bit hash and is actually faster than SHA-256 on 64-bit processors, despite producing a longer hash. Use SHA-512 when storage is not a concern and you want maximum collision resistance.
| Property | SHA-256 | SHA-512 |
|---|---|---|
| Output Size | 32 bytes | 64 bytes |
| Block Size | 64 bytes | 128 bytes |
| Rounds | 64 | 80 |
| Speed (64-bit) | Fast | Faster |
| Security Level | 128-bit | 256-bit |
When to Use SHA-256
- File integrity verification
- Digital signatures
- API request signing (HMAC-SHA256)
- Data deduplication
- Blockchain and merkle trees
When NOT to Use SHA-256
Do not use SHA-256 for password hashing. SHA-256 is intentionally fast. Modern GPUs can compute billions of SHA-256 hashes per second, making brute-force attacks trivial against password databases.
bcrypt - Purpose-Built for Passwords
bcrypt was designed specifically for password hashing in 1999. It incorporates three critical features that generic hash functions lack:
- Salt - Automatically generates and embeds a random salt, preventing rainbow table attacks
- Cost factor - Configurable work factor that makes hashing deliberately slow
- Adaptive - The cost factor can be increased as hardware gets faster
Input: "mypassword"
bcrypt: $2b$12$LJ3m4ys6mY8RlK7eSVqPBOxWJsK5rYPGCTZJ5Q6P2y3h5VpRqWVe
Why bcrypt Wins for Passwords
At a cost factor of 12, bcrypt takes approximately 250ms to hash a single password. This is imperceptible for a user logging in, but it means an attacker can only try 4 passwords per second per CPU core - compared to billions with SHA-256.
bcrypt Alternatives
- Argon2id - The winner of the Password Hashing Competition (2015). Memory-hard, resistant to GPU attacks. Recommended for new projects.
- scrypt - Memory-hard, used by some cryptocurrencies. More complex to configure than bcrypt.
- PBKDF2 - Standards-compliant (NIST). Uses SHA-256 internally with configurable iterations. Simpler but not memory-hard.
The Decision Matrix
| Use Case | Recommended | Why |
|---|---|---|
| Passwords | bcrypt or Argon2id | Intentionally slow, salted |
| File checksums | SHA-256 | Fast, collision-resistant |
| API signing (HMAC) | SHA-256 | Industry standard |
| Digital signatures | SHA-256/SHA-512 | Proven security |
| Quick fingerprinting | SHA-256 | Fast and reliable |
| Legacy non-security | MD5 | Only when no security needed |
| Blockchain | SHA-256 | Proven, widely adopted |
Implementing Hash Functions
In JavaScript (Browser)
async function sha256(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
In Node.js
const crypto = require('crypto');
const bcrypt = require('bcrypt');
// SHA-256
const sha256Hash = crypto.createHash('sha256').update('hello').digest('hex');
// bcrypt
const bcryptHash = await bcrypt.hash('mypassword', 12);
const isValid = await bcrypt.compare('mypassword', bcryptHash);
Try It Yourself
Generate hashes instantly with our free Hash Generator. It supports MD5, SHA-1, SHA-256, SHA-512, and bcrypt - all computed locally in your browser. Your input data is never transmitted or stored.
For encrypting data (not just hashing), try our AES Encryption Tool which uses AES-256-GCM for secure bidirectional encryption.
Frequently Asked Questions
Can you reverse a hash to get the original input?
No. Hash functions are mathematically one-way. You cannot derive the original input from the hash output. The only attack is brute-force - trying every possible input until you find a match.
Is MD5 still used anywhere?
Yes, for non-security purposes like cache keys and quick checksums. It should never be used for passwords, signatures, or any context where collision resistance matters.
How many rounds should I use for bcrypt?
A cost factor of 12 is the current recommended minimum (approximately 250ms per hash). Increase the cost factor every 2-3 years as hardware improves. Aim for 250ms-500ms per hash operation.
Found this helpful?
Join thousands of developers using our tools to write better code, faster.