Skip to content

MD5 vs SHA-1 vs SHA-256 vs SHA-512: which hash should you use?

· 9 min read

Open a hash generator and you are asked to choose: MD5, SHA-1, SHA-256, SHA-384, SHA-512. They all turn input into a string of hexadecimal, they all look equally official, and the advice online ranges from "MD5 is fine" to "MD5 is a security incident". Both are true — for different jobs. The right algorithm depends far less on the algorithm than on what you are about to do with the result.

What a hash function promises

A cryptographic hash takes input of any size and produces a fixed-size digest. The same input always gives the same digest, and changing a single bit changes roughly half the bits of the output. Security rests on three separate promises, and they fail separately:

  • Preimage resistance — given a digest, you cannot find any input that produces it.
  • Second-preimage resistance — given one input, you cannot find a different input with the same digest.
  • Collision resistance — you cannot find any two inputs with the same digest, even when you get to choose both.

Collision resistance is the hardest to keep and the first to go. When people say MD5 or SHA-1 is "broken", they mean collisions: an attacker can craft two different files that hash identically. Nobody has a practical way to reverse MD5 or to match a hash you chose in advance. That distinction decides almost every question below.

The five algorithms side by side

AlgorithmDigestHex lengthStatus
MD5128 bits32Collisions in seconds on a laptop
SHA-1160 bits40Practical collisions since 2017
SHA-256256 bits64No known practical attack
SHA-384384 bits96No known practical attack
SHA-512512 bits128No known practical attack

The history is worth knowing because it shows how "theoretical" weaknesses turn real. MD5 collisions were demonstrated in 2004. By 2008 researchers used one to forge a certificate authority certificate that browsers trusted, and in 2012 the Flame malware used an MD5 collision to pass as signed Microsoft code. SHA-1 fell in 2017, when the SHAttered project published two different PDFs with the same SHA-1 hash; by 2020 a chosen-prefix collision cost a few tens of thousands of dollars of cloud computing. SHA-256, SHA-384 and SHA-512 all belong to the SHA-2 family, which has held up for over two decades.

Which one, for which job

Checking that a download is intact

Use SHA-256, or whatever the publisher printed — you can only compare against the algorithm they used. Against accidental corruption, even MD5 works perfectly: no flaky mirror or half-finished download produces a collision by chance. Against a deliberate attacker who could swap the file, SHA-256 is the one that counts. There is a full walkthrough in how to verify a file checksum.

Deduplication, cache keys, change detection

When nobody is trying to fool you — spotting duplicate photos, naming cache entries, noticing that a file changed — any of the five works, MD5 included. The only argument against MD5 here is consistency: if a system might one day face untrusted input, starting with SHA-256 saves a migration. On modern processors with SHA instructions, SHA-256 is fast enough that speed is rarely a reason to pick MD5 any more.

Signatures and certificates

SHA-256 or stronger, without exception. This is precisely the job collisions break: an attacker gets an innocent document signed, then swaps in a malicious one with the same hash, and the signature still verifies. Browsers stopped accepting SHA-1 certificates in 2017 for exactly this reason.

Storing passwords: none of them

This is the most common mistake, and switching from MD5 to SHA-512 does not fix it. Password storage needs a function that is deliberately slow and salted, so that each guess costs an attacker real time. MD5 and the SHA-2 family are designed to be fast — a single graphics card computes billions of SHA-256 hashes per second, which is wonderful for checksums and catastrophic for passwords. Use Argon2id, scrypt or bcrypt, the password hashing functions OWASP recommends. And the password itself should be long and random; a password generator handles that part.

Proving a message came from someone holding a key

Use HMAC-SHA256, not sha256(secret + message). MD5, SHA-1, SHA-256 and SHA-512 are all vulnerable to length extension: knowing the hash of secret + message, an attacker can compute a valid hash for secret + message + more without ever learning the secret. HMAC is built to close that hole, and it is what webhook signatures and HS256-signed JWTs use.

SHA-256 or SHA-512?

Both are secure; the choice is about size and speed. SHA-512 works on 64-bit words, so on a 64-bit CPU without dedicated hash instructions it often processes data faster than SHA-256, despite producing a digest twice as long. Many modern Intel, AMD and ARM chips do accelerate SHA-256 in hardware, though, and there it usually wins. For most purposes SHA-256 is the sensible default: it is what Git's newer object format, TLS certificates and most package managers use, and its 64-character digest is easier to paste around. SHA-384 is SHA-512 cut short, which incidentally makes it immune to length extension.

SHA-3 and BLAKE2/BLAKE3 are excellent newer designs, but browsers' built-in Web Crypto API offers only SHA-1 and the SHA-2 family, so browser tools — including ours — stick to those, plus MD5 for comparing against older published checksums.

Why the same text gives two different hashes

If your hash does not match someone else's for what looks like identical input, the algorithm is almost never at fault. The bytes differ:

  • A trailing newline. echo "hello" | shasum hashes hello\n. Use echo -n or printf to hash exactly what you typed.
  • Line endings. The same text file saved on Windows (CRLF) and on macOS or Linux (LF) hashes differently.
  • Character encoding. "café" is five bytes in UTF-8 and eight in UTF-16. Web tools nearly always hash UTF-8.
  • Output format. The same digest written as hex and as Base64 looks nothing alike. Upper- and lower-case hex are the same value.
  • Invisible characters. A byte-order mark, a non-breaking space, or a trailing space copied from a web page all change the result.

How to compute one

WhereCommand
macOSshasum -a 256 file · md5 file
Linuxsha256sum file · md5sum file
Windows (PowerShell)Get-FileHash file -Algorithm SHA256
Anywhere with OpenSSLopenssl dgst -sha256 file

Or use the hash generator, which computes all five digests of a text or a file at once — useful when you don't know which algorithm a project published. It runs entirely in your browser: SHA via the built-in Web Crypto API and MD5 via a small local library, so a file being hashed never leaves your device, and the page keeps working with the network off.

The short version

  • Default: SHA-256.
  • Matching a published checksum: whatever was published; prefer SHA-256 when offered.
  • Non-adversarial fingerprints: anything, MD5 included.
  • Signatures, certificates, anything an attacker might target: SHA-256 or stronger, never MD5 or SHA-1.
  • Passwords: none of these — Argon2id, scrypt or bcrypt.
  • Keyed authentication: HMAC-SHA256.