Hash Functions: When to Use Which Algorithm
Hash Functions: When to Use Which Algorithm
Hash functions are everywhere — password storage, data integrity, caching, digital signatures, and content addressing. But using the wrong algorithm for the job can mean security vulnerabilities or wasted performance. This guide covers when to use which hash.
The Common Algorithms
MD5 (128-bit)
MD5 is cryptographically broken. Collisions can be generated in seconds. Never use it for security purposes.
Acceptable uses: Checksums for non-adversarial data integrity (verifying file downloads from trusted sources), cache keys, deduplication hashes.import { createHash } from "crypto";
const hash = createHash("md5").update("hello").digest("hex");
// "5d41402abc4b2a76b9719d911017c592"
SHA-1 (160-bit)
SHA-1 is deprecated for security. Google demonstrated a practical collision in 2017. Git still uses SHA-1 for commit hashes (transitioning to SHA-256) but this is for identification, not security.
Acceptable uses: Legacy system compatibility, non-security checksums. Avoid in new projects.SHA-256 (256-bit)
SHA-256 is the current standard for general-purpose cryptographic hashing. It is part of the SHA-2 family, is well-studied, and has no known practical attacks.
Use for: Data integrity verification, digital signatures, certificate fingerprints, blockchain, content addressing.const hash = createHash("sha256").update("hello").digest("hex");
// "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
SHA-512 (512-bit)
SHA-512 is faster than SHA-256 on 64-bit systems (it processes 64-bit words natively) and provides a larger output. Use it when you need a longer hash or are processing large amounts of data on modern hardware.
bcrypt, scrypt, Argon2 (Password Hashing)
These are not general-purpose hash functions — they are password hashing algorithms designed to be deliberately slow.
import bcrypt from "bcrypt";
const hashed = await bcrypt.hash("userPassword123", 12);
const isValid = await bcrypt.compare("userPassword123", hashed);
Argon2id is the current best practice for password hashing (winner of the Password Hashing Competition). bcrypt is still widely used and acceptable. scrypt is a solid alternative.
Never use MD5, SHA-1, or SHA-256 alone for passwords. Fast hashes allow billions of guesses per second in brute-force attacks.
Decision Matrix
| Use Case | Algorithm | Why |
|----------|-----------|-----|
| Password storage | Argon2id or bcrypt | Deliberately slow, salt built-in |
| File integrity | SHA-256 | Cryptographically secure, standard |
| Cache keys | SHA-256 or MD5 | Speed matters, security does not |
| Digital signatures | SHA-256 or SHA-512 | Cryptographic strength required |
| Content addressing | SHA-256 | Collision resistance critical |
| Non-security checksums | MD5 or CRC32 | Fastest option for simple verification |
| HMAC authentication | SHA-256 | Standard for API auth tokens |
Performance Considerations
On modern hardware, SHA-256 processes roughly 500 MB/s per core. MD5 is about 2x faster. For most applications, the difference is negligible. Only optimize for hash speed in high-throughput pipelines processing gigabytes of data.
Conclusion
Default to SHA-256 for general-purpose hashing. Use Argon2id or bcrypt for passwords. Use MD5 only for non-security checksums where speed matters. Avoid SHA-1 in new projects entirely. The right hash function depends on whether you need security, speed, or both.