"Always hash your passwords" is common advice. This guide explains what a hash is, how MD5, SHA-1, SHA-256 and SHA-512 differ, and which one to use.
What Exactly is a Hash?
Simply put, a hash function transforms any data into a fixed-length "fingerprint." Like human fingerprints, even a tiny change in input produces a completely different result.
"hello": 5d41402abc4b2a76b9719d911017c592
"hello!": 5a8dd3ad0756a93ded72b823b19dd877
One exclamation mark changes the whole result.
Hashing is not encryption. A hash is one-way and cannot recover the original data, while encryption uses a key and can be reversed with the right key. Hashes are used to verify integrity and to store passwords; encryption protects confidentiality.
Three Key Properties of Hash Functions
Hash functions are useful because of these three properties:
- One-way: You can't reverse a hash to get the original input. Just like you can't clone a person from their fingerprint.
- Deterministic: The same input always produces the same hash. Every single time.
- Collision resistant: It's (theoretically) nearly impossible for different inputs to produce the same hash.
The Common Algorithms
MD5: fast but retired for security
MD5 was created in 1991. It produces a 128-bit (32 character) hash and it's fast.
The problem? Collisions were found in 2004. That means different inputs can produce the same hash. Don't use it for security anymore.
Where it's still okay: File checksums, cache keys, duplicate detection—anywhere security doesn't matter. It's fast, after all.
SHA-1: legacy compatibility
SHA-1 produces a 160-bit (40 character) hash. It was thought to be more secure than MD5, but Google successfully demonstrated a collision attack in 2017.
Git famously uses SHA-1, but not for security—just to identify commits. Though Git is transitioning to SHA-256.
SHA-256: the current standard
SHA-256 produces a 256-bit (64 character) hash. Bitcoin, SSL certificates, and most security systems use it. It's the current standard.
No successful collision attacks yet, and it's expected to remain secure for the foreseeable future. If you're not sure what to use, use SHA-256.
SHA-512: extra margin
512-bit (128 character) hash. Longer than SHA-256, and sometimes even faster on 64-bit systems.
Overkill for most purposes, but useful for long-term storage or extremely high-security requirements.
Practical Guide: When to Use What
| Use Case | Recommended | Why |
|---|---|---|
| Password storage | bcrypt, Argon2 | Regular hashes are too fast, vulnerable to brute force |
| File integrity | SHA-256 | Industry standard, secure |
| Cache keys | MD5 | Fast, security not needed |
| Digital signatures | SHA-256+ | Collision resistance required |
| Blockchain | SHA-256 | Bitcoin standard |
Common Mistakes
1. Using regular hashes for passwords
Even SHA-256 isn't suitable for password storage. It's too fast—attackers can try billions of hashes per second. Use bcrypt or Argon2, which are intentionally slow.
2. Hashing without salt
The SHA-256 hash of "password123" is the same for everyone. Rainbow tables can reverse it. Always add a random salt before hashing.
3. Using MD5 for security
MD5 is fine for checksums, not for authentication or signatures.