Hash Functions Explained — SHA-256, MD5, and When to Use Each
Hash functions are everywhere in software — from verifying file downloads to storing passwords. Understanding how they work and which to choose matters more than most developers realize.
What Is a Hash Function?
A hash function takes an input of any size and produces a fixed-length output (the “digest”). The key properties are:
- One-way — you cannot reverse a hash to recover the original input
- Deterministic — the same input always produces the same hash
- Avalanche effect — changing a single bit in the input drastically changes the output
- Collision-resistant — it should be computationally infeasible to find two inputs that produce the same hash
Common Algorithms Compared
MD5 produces a 128-bit (32 hex character) digest. It’s fast but cryptographically broken — researchers demonstrated practical collision attacks in 2004. Never use MD5 for security. It’s still acceptable for non-security checksums, like verifying a file transfer didn’t corrupt data.
SHA-256 (part of the SHA-2 family) produces a 256-bit digest. It’s the current workhorse of cryptographic hashing — used in TLS certificates, Bitcoin, and most integrity-verification systems. No practical attacks exist against it.
SHA-3 (Keccak) was standardized in 2015 as a backup to SHA-2. It uses a completely different internal structure (sponge construction vs. Merkle–Damgård). SHA-3 is not faster than SHA-2 in software, but it provides a hedge if SHA-2 is ever compromised.
Choosing the Right Hash
- File integrity checks — SHA-256 is the standard. MD5 is fine if you only care about accidental corruption, not tampering.
- Password storage — do not use any of the above directly. Use a purpose-built password hash like bcrypt, scrypt, or Argon2. These are intentionally slow to resist brute-force attacks.
- Digital signatures & certificates — SHA-256 or SHA-3. MD5 and SHA-1 are deprecated for this use.
- Data deduplication — SHA-256 gives strong collision resistance for content-addressable storage.
- Hash tables / non-security — faster non-cryptographic hashes like xxHash or MurmurHash are better choices.
Quick Example
# Command line
echo -n "hello" | shasum -a 256
# 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
echo -n "hello" | md5
# 5d41402abc4b2a76b9719d911017c592Notice both outputs are deterministic and fixed-length, regardless of input size. But SHA-256’s 64-character hex output gives you 2256 possible values — making collisions effectively impossible.
The Bottom Line
Default to SHA-256 for any new project that needs hashing. Reach for SHA-3 if compliance or defense-in-depth demands it. Avoid MD5 and SHA-1 for anything security-related. And always use a specialized password hashing algorithm for credentials.
Related Tools
- Hash Generator — generate MD5, SHA-256, and other hashes instantly
- Password Generator — create strong, cryptographically secure passwords
Try it yourself
Use our free Hash Generator — no signup, no ads interrupting your workflow.
Open Hash Generator