Cryptographic hash functions are widely used to check data integrity, support digital signatures and content-addressed storage, and build other cryptographic systems. A hash can identify content efficiently, but collisions are possible and a digest alone does not authenticate its publisher. Despite their ubiquity, many developers don't fully understand how they work, which algorithm to choose, and — critically — where hashing alone is insufficient.
This guide covers SHA-1, SHA-256, SHA-512, MD5, bcrypt, and modern alternatives, explaining the mathematics behind them, their security properties, and the right tool for each job.
What is a Hash Function?
A cryptographic hash function takes an input of any size and produces a fixed-size output (the hash or digest) with these essential properties:
1. Deterministic: The same input always produces the same output
2. One-way (Pre-image resistance): Given a hash, it's computationally infeasible to find the original input
3. Avalanche effect: A tiny change in input produces a completely different hash
4. Collision resistance: It's computationally infeasible to find two different inputs with the same hash
5. Fixed output size: Regardless of input size, the output is always the same length
SHA-256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
SHA-256("hello!") = ce06092fb948d9af675d0de94fa401b4ece32d9520f77daa24e30e5c4f6d3a09Notice how adding a single ! completely changes the output — that's the avalanche effect.
MD5: Legacy, Broken, Still Everywhere
MD5 (Message Digest Algorithm 5) was designed by Ron Rivest in 1991 and produces a 128-bit (32 hex character) hash. For decades it was the default choice for checksums and password hashing.
MD5 is cryptographically broken. In 2004, researchers demonstrated practical collision attacks — finding two different inputs with the same MD5 hash. By 2008, forged SSL certificates using MD5 collisions were demonstrated, leading to browsers dropping MD5.
When MD5 is still acceptable:
- Non-security checksums (detecting accidental file corruption, not malicious tampering)
- Deduplication keys in internal databases
- Legacy systems where changing the algorithm would break compatibility
When to never use MD5:
- Password storage
- Digital signatures
- Certificate fingerprints
- Any security-sensitive application
MD5 is fast, which is one reason it is unsuitable for password storage. Its collision weakness also means it should not be used where an attacker can choose or alter inputs.
SHA-1: Deprecated but Not Dead
SHA-1 (Secure Hash Algorithm 1) produces a 160-bit (40 hex character) hash. Designed by the NSA and published by NIST in 1995, it was the internet's backbone for years — used in TLS/SSL, SSH, PGP, and Git.
SHA-1 is cryptographically broken for collision resistance. Google's SHAttered attack demonstrated a practical SHA-1 collision in 2017, showing that SHA-1 no longer meets modern collision-resistance expectations.
SHA-1 in Git: SHA-1 remains the traditional object format for many repositories. Git documents a separate SHA-256 repository format and its interoperability limits; do not assume existing repositories or hosting services have automatically migrated.
Current status: All major certificate authorities stopped issuing SHA-1 certificates in 2017. Do not use SHA-1 for new security applications.
SHA-256 and SHA-2 Family
SHA-256 is part of the SHA-2 family designed by the NSA and published by NIST in 2001. It produces a 256-bit (64 hex character) hash.
The SHA-2 family includes:
- SHA-224: 224-bit output, using the SHA-256 family design with its own initialization value
- SHA-256: 256-bit output — the most widely used
- SHA-384: 384-bit output, using the SHA-512 family design with its own initialization value
- SHA-512: 512-bit output — slower but larger, useful for very long messages
- SHA-512/256: SHA-512 family design with a 256-bit output and its own initialization value
SHA-256 is currently considered secure with no known practical attacks. It's used in:
- TLS 1.3 and HTTPS certificates
- Bitcoin and most cryptocurrency blockchains
- AWS, Google Cloud, and Azure API request signing
- Code signing (APK, .exe signing)
- JSON Web Tokens (RS256, ES256 use SHA-256 internally)
Throughput depends on the runtime, hardware, input size, and available CPU instructions; benchmark in the target environment instead of relying on a universal speed figure.
SHA-3 and BLAKE3: Modern Alternatives
SHA-3 (Keccak) was selected by NIST in 2012 after a public competition. Unlike SHA-1/2, SHA-3 uses a sponge construction and does not have the classic length-extension property of Merkle–Damgård hashes. For message authentication, use HMAC or another defined keyed construction rather than inventing a secret-prefix hash. Performance varies by platform and implementation.
BLAKE3 is a more recent hash function (2020) with remarkable properties:
- Designed for high throughput in software, with performance depending on implementation and hardware
- Designed for parallelism — exploits multiple CPU cores
- No practical cryptanalytic break is currently known; this does not mean the algorithm is risk-free
- Used by Bao for verifiable streaming; check the target ecosystem for library and interoperability support
// BLAKE3 in Node.js (using the blake3 package):
import { hash } from 'blake3';
const digest = hash("hello world");
// Produces a 256-bit hash by defaultBLAKE3 is a fast general-purpose hash, not a password-hashing algorithm. For security-sensitive use, consider ecosystem support, protocol compatibility, and review requirements before choosing it.
Password Hashing: Never Use SHA-256 Directly
This is the most important section of this guide. Do not store passwords as SHA-256 hashes. Here's why:
SHA-256 is designed to be fast. That makes it unsuitable for password storage: attackers can test guesses rapidly, and a general-purpose hash does not provide the per-password work factor and memory cost of an adaptive password-hashing scheme.
Use password-specific hashing algorithms:
bcrypt: An adaptive password-hashing function with a configurable work factor. Choose parameters by benchmarking on the production environment and re-evaluate them over time. Implementations commonly limit input to 72 bytes:
import bcrypt from 'bcrypt';
const hash = await bcrypt.hash(password, 12); // cost factor 12
const valid = await bcrypt.compare(input, hash);Argon2id is the current OWASP-preferred choice for most new password-storage systems. OWASP lists a baseline of 19 MiB memory, 2 iterations, and parallelism 1; tune upward where the production environment allows:
import argon2 from 'argon2';
const hash = await argon2.hash(password, { type: argon2.argon2id });
const valid = await argon2.verify(hash, password);scrypt: Memory-hard, available natively in Node.js:
import { scrypt } from 'crypto';PBKDF2: A password-based key derivation function available in many validated/FIPS-oriented environments. OWASP currently specifies at least 600,000 iterations for PBKDF2-HMAC-SHA-256 in its guidance; follow the approved profile required by your environment.
import { pbkdf2 } from 'crypto';Hash Functions in Practice: Use Cases
File integrity verification: Compute the downloaded file’s SHA-256 and compare it with a checksum obtained through an independently trusted channel. A checksum hosted beside the file can detect accidental corruption, but by itself it does not authenticate the publisher if both downloads can be replaced:
sha256sum ubuntu-24.04.iso
# Compare with a checksum obtained through a trusted, independent channelContent-addressable storage: Git, npm, Docker layers, and IPFS use hash digests as unique identifiers for content. A matching digest is strong evidence that contents match, but a hash is not a mathematical proof of identity; collision resistance is a security property, not a guarantee.
HMAC (Hash-based Message Authentication Code): Combines a secret key with a hash to prove both message integrity and authenticity:
import { createHmac } from 'crypto';
const mac = createHmac('sha256', secretKey).update(message).digest('hex');HMAC-SHA256 is used in JWT signatures (HS256), AWS request signatures, and webhook verification.
Certificate fingerprints: A fingerprint is a digest of the encoded certificate that can be compared out of band. It is not the same thing as Certificate Transparency (CT), which is a separate logging and auditing system.
Choosing the Right Hash Algorithm
| Use Case | Recommended | Avoid |
|----------|------------|-------|
| Password storage | Argon2id, bcrypt, scrypt | MD5, SHA-1, SHA-256 (plain) |
| File checksums / integrity | SHA-256, SHA-3 | MD5 |
| Digital signatures | SHA-256, SHA-384, SHA-512 | MD5, SHA-1 |
| HMAC / API authentication | HMAC-SHA256 | MD5, SHA-1 |
| Blockchain / Merkle trees | SHA-256, SHA-3, BLAKE3 | MD5 |
| Deduplication (non-security, trusted inputs) | MD5, SHA-1, BLAKE3 | MD5/SHA-1 for attacker-controlled inputs |
| High-performance hashing | BLAKE3 | MD5 |
| FIPS/compliance environments | SHA-256, SHA-384, SHA-3 | MD5, SHA-1 |