MD5 has been declared dead more times than I can count, yet it's still sitting in production code all over the place. Checksums, cache keys, ETags, dedup logic — it keeps turning up. The trick is knowing which of those uses are perfectly fine and which will get you into trouble.

What MD5 actually is

MD5 (Message-Digest Algorithm 5) is a cryptographic hash function that takes any input and produces a fixed 128-bit output, usually written as a 32-character hexadecimal string. Feed it the same bytes and you get the same digest every time. Change a single bit and the output looks completely different.

md5("hello")  -> 5d41402abc4b2a76b9719d911017c592
md5("Hello")  -> 8b1a9953c4611296a827abf8c47804d7

Notice how flipping one character from lowercase to uppercase gives a totally unrelated hash. That property — called the avalanche effect — is exactly why hashes are useful for comparing content without comparing it byte by byte.

Where MD5 is still fine

Despite its reputation, MD5 is a reasonable tool when you're not relying on it for security. The reasons it's broken (more on that below) don't matter for a lot of everyday engineering tasks.

  • Checksums and integrity checks — verifying a file downloaded correctly and wasn't corrupted in transit. Here you're guarding against accidental damage, not a malicious attacker.
  • Cache keys — hashing a long URL or a set of query params into a short, consistent key.
  • Deduplication — spotting identical files or records by comparing hashes before doing an expensive full comparison.
  • ETags and change detection — generating a compact fingerprint of a response body.

In all of these, MD5's speed is an advantage and its cryptographic weaknesses are irrelevant.

Where MD5 will absolutely bite you

MD5 is cryptographically broken. Researchers can generate collisions — two different inputs that produce the same hash — quickly and reliably. That makes it unsafe for anything where an adversary might try to forge data.

Never use MD5 for passwords

This is the big one. Storing password hashes with raw MD5 is a serious mistake. It's fast, which is great for cache keys and terrible for passwords, because an attacker with your database can brute-force billions of guesses per second on commodity GPUs. Use a purpose-built password hash like bcrypt, scrypt, or argon2 instead. They're deliberately slow and salted.

Never use MD5 for digital signatures or certificates

Because collisions are practical, MD5 can't guarantee that a signed document hasn't been swapped for a malicious one with the same hash. SHA-256 is the sensible default for signing and verification today.

Watch out for encoding assumptions

A hash is computed over bytes, not characters. If one system hashes a string as UTF-8 and another as UTF-16, you'll get different digests for what looks like identical text. Line endings are another classic trap — a file with Windows \r\n endings hashes differently from the same file with Unix \n endings, even though they look the same in your editor.

A short worked example

Say you want a stable cache key for a set of report parameters. Serialise them consistently, then hash. 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} produce the same hash
  const sorted = Object.keys(params)
    .sort()
    .reduce((acc, k) => { acc[k] = params[k]; return acc; }, {});

  const json = JSON.stringify(sorted);
  return crypto.createHash('md5').update(json, 'utf8').digest('hex');
}

cacheKey({ userId: 42, month: '2024-01' });
// -> something like '3b8e...e1a9' — stable every run

The sorting step matters. Without it, {userId: 42, month: '2024-01'} and {month: '2024-01', userId: 42} serialise to different JSON and give you two cache entries for the same logical request. Small detail, real bug.

In Python the equivalent is just as short:

import hashlib, json

def cache_key(params):
    data = json.dumps(params, sort_keys=True).encode('utf-8')
    return hashlib.md5(data).hexdigest()

Verifying by hand

When you're debugging a mismatch — your server computes one hash and a client computes another — it helps to have a neutral reference point. Rather than wiring up a script, I'll often paste the exact string into our MD5 Hash Generator and compare. If the tool's output matches one side but not the other, you've narrowed the problem down to an encoding or whitespace difference in the mismatched system, not a logic bug in both.

Quick gotcha checklist

  • Trailing whitespace and newlines — trim deliberately, don't trim by accident.
  • Encoding — always hash bytes with an explicit, agreed charset (UTF-8 is the safe default).
  • Case sensitivity of the output — most libraries return lowercase hex, but not all. Normalise before comparing strings.
  • Don't compare hashes for secret values in a loop that leaks timing — though if you're worried about timing attacks, you shouldn't be using MD5 anyway.
  • Salt won't save MD5 for passwords — it slows down precomputed rainbow tables but does nothing about raw brute-force speed.

The short version

MD5 is a fast, deterministic fingerprint. Treat it as a convenience for non-adversarial work: cache keys, dedup, integrity checks against accidental corruption. The moment an attacker enters the picture — passwords, signatures, anything where forging a collision would be valuable — reach for SHA-256 or a proper password hashing function.

Know which bucket your use case falls into and MD5 stays a useful, boring tool instead of a security incident waiting to happen.

Photo by Chris Ried on Unsplash