MD5 vs SHA-256 for File Checksums

Updated

Use SHA-256 for file checksums. MD5 still detects accidental corruption, but practical collision attacks mean it cannot show that a file is the one its publisher intended; RFC 6151 states that MD5 is no longer acceptable where collision resistance is required. SHA-256 has no known practical collisions and is supported by the same everyday tools.

The longer answer depends on what you are protecting against: random errors, or a person who wants you to accept a different file.

MD5 vs SHA-256 for file checksums MD5 gives a 128-bit digest of 32 hex characters; collisions are practical, so it only detects accidental corruption. SHA-256 gives a 256-bit digest of 64 hex characters with no known practical collisions, so it also protects against deliberate tampering when the expected hash comes from a trusted source. MD5 vs SHA-256 for file checksums MD5 Digest: 128 bits b1946ac92492d2347c6235b4d2611184 ✓ Catches accidental corruption ✗ Collisions are practical (RFC 6151) ✗ Not for signatures or trusted downloads Use for: error checks, dedup of your own files SHA-256 Digest: 256 bits 5891b5b522d5df086d0ff0b110fbd9d2 1bb4fc7163af34d08286a2e846f6be03 ✓ Catches accidental corruption ✓ No known practical collisions ✓ Downloads, packages, signatures, SRI Recommended default Both digests: the same 6-byte file "hello\n". A match only helps if the expected hash comes from a trusted source.
MD5 and SHA-256 compared by digest size, collision status and recommended use

What a checksum does

A cryptographic hash function turns any amount of data into a fixed-size digest. Change one bit of the input and the digest changes completely. Publishing the digest next to a download lets you recompute it after the transfer and compare:

$ printf 'hello\n' > file.txt
$ md5sum file.txt
b1946ac92492d2347c6235b4d2611184  file.txt
$ sha256sum file.txt
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03  file.txt

Two different questions can be answered this way:

  1. Integrity against accidents. Did the bytes arrive unchanged after a network or disk error? Almost any hash, even a simple CRC, catches this.
  2. Integrity against attackers. Is this the exact file the publisher released, and not a different file crafted to have the same digest? That requires collision resistance, and it is where MD5 fails.

MD5 vs SHA-256 at a glance

MD5 SHA-256
Specification RFC 1321 (1992) SHA-2 family; RFC 6234 includes reference code
Digest size 128 bits 256 bits
Hex length 32 characters 64 characters
Base64 length 24 characters 44 characters
Collision resistance Broken: collisions are practical No known practical collisions
Accidental corruption Detected Detected
Acceptable for signatures No (RFC 6151) Yes
Typical tools md5sum, md5 sha256sum, shasum -a 256, Get-FileHash

What RFC 6151 says about MD5

RFC 6151 is the IETF's updated security guidance for MD5. Its key points:

  • Published attacks show that "it is not prudent to use MD5 when collision resistance is required". The RFC cites work that finds an MD5 collision in about one minute on a standard notebook PC.
  • MD5 must not be used for digital signatures, and such attacks have been demonstrated against X.509 certificates.
  • An MD5 checksum included only to detect errors "is still an acceptable use".
  • The best known pre-image attack is still far from practical, so finding a file that matches an existing MD5 digest you did not choose remains hard.

The last point is why MD5 checksums are not useless. The danger is a chosen collision: someone who controls the content can prepare two files, a harmless one and a malicious one, with the same MD5 digest, publish the hash of the harmless one, and later swap in the other.

When MD5 is still fine

  • Detecting accidental changes in your own backups or file transfers.
  • De-duplicating files you control, where nobody benefits from forging a match.
  • Cache keys and ETags that are not security boundaries.
  • Compatibility with an old system that only publishes MD5, as long as you treat the result as an error check, not a security check.

When you need SHA-256 (or stronger)

  • Verifying downloaded software, firmware, disk images or packages.
  • Any checksum that is published so that others can trust a file.
  • Digital signatures, certificates and code signing.
  • Integrity values for third-party resources, such as Subresource Integrity in HTML, which accepts SHA-256, SHA-384 and SHA-512 but not MD5.

Speed is rarely a good reason to keep MD5. Results depend on the CPU, and many current processors include hardware instructions for SHA-256, so measure before assuming MD5 is faster on your machines.

What about SHA-1 and SHA-512?

Hash Digest Hex length Status for checksums
SHA-1 160 bits 40 characters Avoid for security; MDN's Web Crypto documentation calls SHA-1 vulnerable for cryptographic use
SHA-256 256 bits 64 characters Recommended default
SHA-512 512 bits 128 characters Also fine; longer digest, same family (SHA-2)

SHA-1 sits between MD5 and SHA-256: it is longer than MD5 but no longer trusted where collision resistance matters. If a project publishes both SHA-1 and SHA-256 sums, check the SHA-256 one. SHA-512 is as safe as SHA-256 for this purpose; pick whichever the publisher provides, and use SHA-256 when you publish your own, since it is the one most tools and users expect.

How to check a SHA-256 checksum

Linux

sha256sum ubuntu.iso                # print the digest
sha256sum -c SHA256SUMS             # check every file listed in a checksum file

A checksum file has one line per file: the digest, two spaces, the file name. sha256sum -c prints OK for each match and FAILED plus a warning for any mismatch.

macOS

shasum -a 256 file.dmg

Windows

Get-FileHash .\setup.exe -Algorithm SHA256
certutil -hashfile setup.exe SHA256

Browser JavaScript

async function sha256Hex(file) {
  const buf = await file.arrayBuffer();
  const hash = await crypto.subtle.digest("SHA-256", buf);
  return [...new Uint8Array(hash)].map(b => b.toString(16).padStart(2, "0")).join("");
}

The Web Crypto digest() method supports SHA-1, SHA-256, SHA-384 and SHA-512, but not MD5.

Compare the whole digest, not just the first few characters, and compare it with a value from a source you trust. Hex digests are case-insensitive, so 5891B5… and 5891b5… are the same.

A matching hash is only as good as its source

If the checksum sits on the same server as the file, an attacker who can replace the file can usually replace the checksum too. Stronger setups publish checksums over a separate channel, sign the checksum file with a key you already trust, or rely on package managers that verify signatures automatically. SHA-256 removes the collision problem; it does not remove the need to trust where the expected value came from.

Text hashes on this site

The universal encoder decoder computes MD5, SHA-1, SHA-256 and SHA-512 of text you type, which is handy for comparing short strings and test vectors. Note that hashing text is sensitive to every byte: hello and hello followed by a line break give different digests, which is why the printf 'hello\n' example above does not match the SHA-256 of hello alone (2cf24dba…9824). Digests are usually shown in hex; the Base64 vs hex guide explains why some tools print them in Base64 instead, and why Base64 makes data larger covers the size difference. HMAC signatures in tokens are built on the same hash functions; see how JWTs are decoded and verified.

Frequently asked questions

Is MD5 still OK for checksums?

For catching accidental corruption, such as a broken download or a bad disk copy, MD5 still works; RFC 6151 says an MD5 checksum used only to detect errors is still an acceptable use. It is not acceptable when you need to know that nobody replaced the file on purpose, because MD5 collisions can be produced in practice.

Why is a SHA-256 checksum longer than an MD5 checksum?

MD5 produces a 128-bit digest, shown as 32 hex characters. SHA-256 produces a 256-bit digest, shown as 64 hex characters. In Base64 they would be 24 and 44 characters.

Does a matching checksum prove a download is safe?

Only if you got the checksum from a source you trust more than the download itself, such as a signed release page. If an attacker can change both the file and the published hash, a match proves nothing. Signed checksums or signed packages close that gap.

Encode or decode text now with the free Universal Encoder Decoder: Base64, URL, HTML entities, hex, binary and hashes.

Open the encoder

Sources