MD5 is one of those algorithms that refuses to die. It was declared cryptographically broken years ago, and yet you'll still find it in config files, cache keys, and legacy APIs all over the industry. That's not necessarily a mistake. The trick is knowing exactly what MD5 is safe to do and what it absolutely cannot do.
What MD5 actually is
MD5 (Message-Digest Algorithm 5) is a hash function that takes any input — a string, a file, a blob of bytes — and produces a fixed 128-bit output, usually written as a 32-character hexadecimal string. The same input always produces the same digest, and even a one-character change produces a completely different result.
Here's what that looks like in practice:
md5("hello") = 5d41402abc4b2a76b9719d911017c592
md5("hello!") = a647c8eb1e2dd555cba9c32dc3177ab0
md5("Hello") = 8b1a9953c4611296a827abf8c47804d7Notice how a single capital letter changes the whole digest. That property — tiny input change, totally different output — is called the avalanche effect, and it's what makes hashes useful for detecting changes.
When MD5 is a fine choice
The one thing to remember: MD5 is acceptable for integrity and identity checks where an attacker isn't trying to fool you. If there's no adversary deliberately crafting malicious input, MD5 is fast, compact, and perfectly serviceable.
- Checksums for accidental corruption. Verifying a file downloaded cleanly, or that a byte stream wasn't mangled in transit. MD5 catches accidental bit flips just fine.
- Cache keys. Hashing a complex query or a set of parameters into a short, stable key is a classic use. The 32-char output is a tidy, deterministic identifier.
- Deduplication. Spotting duplicate files or records by comparing digests instead of full contents.
- Deterministic bucketing / sharding. Turning an arbitrary string into a number you can modulo to pick a shard.
In all of these cases, nobody gains anything by forging a collision, so MD5's weaknesses don't matter.
When MD5 will absolutely burn you
The headline: never use MD5 for security. Specifically:
Don't hash passwords with it
MD5 is fast, which is the opposite of what you want for password storage. GPUs can compute billions of MD5 hashes per second, so a leaked database of MD5 password hashes gets cracked almost instantly. Use a dedicated password hashing function — bcrypt, scrypt, or Argon2 — all of which are deliberately slow and salted.
Don't rely on it against a motivated attacker
MD5 is vulnerable to collision attacks: it's computationally feasible to generate two different inputs that produce the same digest. This has been used to forge digital certificates in the real world. If your check needs to withstand someone actively trying to defeat it — signatures, integrity of security-critical downloads, tamper detection — use SHA-256 or better.
Don't use it for HMAC on new systems
While HMAC-MD5 is more resilient than raw MD5, there's no reason to pick it for anything new. Reach for HMAC-SHA256.
Common gotchas
Even in the legitimate use cases, a few things trip people up:
- Encoding matters. MD5 hashes bytes, not characters. "café" hashed as UTF-8 gives a different digest than the same string in Latin-1. Always be explicit about your encoding before hashing.
- Trailing whitespace and newlines. A string with a trailing
\nhashes completely differently. This bites people comparing a file's digest against a value someone pasted into a text field. - Case of the hex output. Some tools emit uppercase hex, others lowercase. They represent the same digest, but a naive string comparison will say they don't match. Normalise before comparing.
- Don't confuse hashing with encryption. MD5 is one-way. You can't "decrypt" an MD5 hash back to the original. What people mean by "cracking" MD5 is looking the value up in a precomputed table or brute-forcing inputs — which works precisely because MD5 is fast and unsalted.
A quick worked example
Say you're building a simple cache and want a stable key from a request's query parameters. Here's a Node.js version:
const crypto = require('crypto');
function cacheKey(params) {
// Sort keys so {a:1,b:2} and {b:2,a:1} map to the same key
const normalized = JSON.stringify(
Object.keys(params).sort().reduce((acc, k) => {
acc[k] = params[k];
return acc;
}, {})
);
return crypto
.createHash('md5')
.update(normalized, 'utf8') // be explicit about encoding
.digest('hex');
}
cacheKey({ user: 42, page: 2 });
// => '1b9f...' (always the same for the same params)The same idea in Python:
import hashlib, json
def cache_key(params):
normalized = json.dumps(params, sort_keys=True)
return hashlib.md5(normalized.encode('utf-8')).hexdigest()Note the deliberate normalisation. Without sorting the keys, two logically identical requests could produce different cache keys and you'd get cache misses you didn't expect.
When you just want to sanity-check a value by hand — confirm a digest matches, or generate one quickly while debugging — a browser tool is faster than writing a script. I keep this MD5 Hash Generator bookmarked for exactly that: paste a string, get the digest, compare, done.
The bottom line
MD5 is a good, fast, compact hash for non-adversarial integrity and identity work: cache keys, dedup, corruption checks, sharding. It is a terrible choice for anything a determined attacker might try to break: passwords, signatures, security-critical verification. Those cases want SHA-256, or a proper password KDF like Argon2.
Know which bucket your use case falls in, mind your encoding and whitespace, and MD5 will keep quietly doing its job the way it has for decades.
Photo by George Prentzas on Unsplash