When I first started programming, someone told me "always hash your passwords" and I just nodded along, having no idea what a hash actually was. This guide covers everything I wish I'd known back then.
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!" → 9a2b7f2a7c8e9f0d1b2c3d4e5f6a7b8c
Just one exclamation mark, and the result is completely different. That's the essence of hashing.
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.
MD5 - Fast but Ready for Retirement
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 - When You Need Extra Security
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
Using MD5 for authentication or signatures in 2024? Just don't. Checksums only.
Summary
Quick reference:
- Passwords → bcrypt/Argon2
- Security-sensitive hashing → SHA-256
- Quick checksums → MD5 (non-security only)
Hashing is a simple concept, but understanding it properly can prevent a lot of problems.
Frequently Asked Questions
What is the difference between hashing and encryption?
Hashing is a one-way function that cannot recover original data. Encryption uses keys to transform data and can be reversed with the correct key. Hashing is used for data integrity verification and password storage, while encryption protects data confidentiality.
Why is MD5 considered insecure?
MD5 is vulnerable to collision attacks. In 2004, researchers discovered methods to quickly find different inputs producing the same hash. MD5 should only be used for non-security purposes like file checksums. For security applications, SHA-256 or higher is recommended.