Hash Functions Explained: MD5, SHA-1, SHA-256 & When to Use Each
Hash functions are everywhere in software: verifying file integrity, storing passwords, generating cache keys, signing data, and powering blockchains. But not all hash functions are equal — some are cryptographically broken, some are fast by design, and some are intentionally slow. Choosing the wrong one for the wrong job can introduce serious security vulnerabilities.
What Is a Hash Function?
A hash function takes an input of any size and produces a fixed-size output called a hash, digest, or checksum. A good cryptographic hash function has four properties:
- Deterministic: The same input always produces the same hash
- Fast to compute: Generating the hash is efficient
- Pre-image resistant: Given a hash, it is infeasible to find the input
- Collision resistant: It is infeasible to find two different inputs with the same hash
Example — the SHA-256 hash of "hello":
SHA256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
Change even one character and the hash changes completely (the avalanche effect).
MD5
MD5 produces a 128-bit (32 hex character) hash. It was designed in 1991 and was once widely used for security purposes.
Status: Cryptographically broken
Collisions in MD5 can be generated in seconds on modern hardware — two different inputs can produce the same MD5 hash. This makes MD5 completely unsuitable for any security purpose.
Still acceptable for: Non-security checksums, content hashing for cache keys, deduplication of files in trusted environments where collision attacks are not a concern.
MD5("hello") = 5d41402abc4b2a76b9719d911017c592
SHA-1
SHA-1 produces a 160-bit (40 hex character) hash. It was the standard for many years and was used in SSL certificates and Git commits.
Status: Deprecated for security use
In 2017, Google's SHAttered attack demonstrated a practical SHA-1 collision. Major CAs stopped issuing SHA-1 certificates in 2017. Git still uses SHA-1 internally but is migrating to SHA-256.
Still acceptable for: Non-security checksums, legacy system compatibility. Do not use for new security-sensitive applications.
SHA1("hello") = aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
SHA-256
SHA-256 is part of the SHA-2 family and produces a 256-bit (64 hex character) hash. It is the current industry standard for most applications.
Status: Secure — recommended
No practical collisions have been found. It is used in TLS certificates, code signing, blockchain (Bitcoin), HMAC signing, and JWT tokens.
SHA256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-512
SHA-512 produces a 512-bit (128 hex character) hash. It offers a larger security margin than SHA-256 and can actually be faster on 64-bit processors.
Status: Secure — good for high-security applications
Use when: You need a larger hash for additional collision resistance, or when hashing large amounts of data on 64-bit systems where SHA-512 can outperform SHA-256.
Comparison Table
| Algorithm | Output Size | Security | Speed | Common Uses |
|---|---|---|---|---|
| MD5 | 128-bit / 32 chars | Broken | Very fast | Checksums, cache keys |
| SHA-1 | 160-bit / 40 chars | Deprecated | Fast | Legacy systems, Git |
| SHA-256 | 256-bit / 64 chars | Secure | Fast | TLS, code signing, JWT, blockchain |
| SHA-512 | 512-bit / 128 chars | Very secure | Fast on 64-bit | High-security applications |
| bcrypt | 60 chars | Secure | Intentionally slow | Password hashing only |
| Argon2 | Variable | Best | Intentionally slow | Password hashing (recommended) |
The Critical Rule: Don't Hash Passwords with SHA
For passwords, use a deliberately slow, memory-hard algorithm:
- Argon2id — winner of the Password Hashing Competition, recommended by OWASP in 2025
- bcrypt — widely supported, proven, good default choice
- scrypt — memory-hard, good for high-security contexts
Practical Use Cases for SHA-256
- File integrity verification: Compute SHA-256 of a downloaded file and compare to the published hash to verify no corruption or tampering
- HMAC signing:
HMAC-SHA256is used to sign API requests and JWT tokens - Content-addressable storage: Use the hash of content as its storage key (Git does this for objects)
- Deduplication: Hash files to detect duplicates without byte-by-byte comparison
- Cache keys: Hash a request's parameters to create a cache key
Frequently Asked Questions
Is MD5 safe to use?
MD5 is cryptographically broken and must not be used for security purposes. It is still acceptable for non-security use cases like generating content hashes for cache keys or deduplicating files in a trusted environment.
Should I use SHA-256 to hash passwords?
No. SHA-256 is too fast — GPUs can compute billions per second. Use bcrypt, scrypt, or Argon2id for passwords instead. These are intentionally slow and memory-intensive, making brute-force attacks impractical.
What is the difference between a hash and encryption?
Hashing is one-way: you cannot reverse a hash to get the original input (by design). Encryption is two-way: data encrypted with a key can be decrypted back to its original form. Use hashing for integrity verification and password storage; use encryption when you need to recover the original data.