harmmy

File hashes: SHA-256, SHA-1 and checks that fail

SHA-1, SHA-256 and SHA-512 answer different questions. Where WebCrypto stops short of MD5, why hashes are not encryption, and whitespace that breaks a check.

2026-12-02

Four SHA-2 variants and one legacy name

Hash lengths are fixed. MD5 produces 128 bits (32 hex characters), SHA-1 160 (40), SHA-256 256 (64), SHA-384 384 (96) and SHA-512 512 (128). Longer is not automatically safer; SHA-256 and SHA-512 are both fine for integrity, and the choice usually follows what the other side publishes. A 4 GB ISO has the same digest length as a one-line text file.

SHA-1 is deprecated for signatures and certificates because collisions are practical: the SHAttered attack in 2017 produced two different PDFs with the same SHA-1. It still appears in Git object names and old checksum files, where it catches accidental corruption but should not be trusted against a deliberate forgery. If a vendor only publishes MD5 or SHA-1, treat it as a corruption check and look for a second source.

Why browsers do not give you MD5

WebCrypto's SubtleCrypto.digest supports SHA-1, SHA-256, SHA-384 and SHA-512, not MD5. The reason is policy, not difficulty: MD5 has had chosen-prefix collisions since 2004, and exposing it invites building signatures and password schemes on a broken primitive. Node also omits it in FIPS mode.

Legacy systems still hand out MD5 checksums, so a tool that cannot compute MD5 is useless for verifying an old CD image. The workaround is a small JavaScript or WebAssembly implementation, which is slower and must be fed bytes rather than text. Do not use that MD5 for passwords or signatures; it is only for matching a published checksum.

A hash is not encryption and does not prove origin

A hash is one-way: there is no key and no decryption. Anyone can compute the SHA-256 of a file, so a matching value proves the bytes you have equal the bytes someone described, not who produced them. Encryption is reversible with a key; a hash is a fingerprint.

Proving origin needs a signature (PGP, code signing, Sigstore) or a shared secret (HMAC). A vendor that publishes a SHA-256 value on the same HTTPS page as the download protects against a corrupted mirror, not against a compromised website. For real supply-chain checks, compare the hash through a second channel or verify a signature.

Case, whitespace and line endings break comparisons

Hex output is case-insensitive in meaning but not in string comparison: A1B2 and a1b2 are the same hash, and a strict equality check fails. Copying from a PDF or a web page can bring a non-breaking space or a trailing newline; trim and lowercase both sides before comparing.

Text files are worse. Hashing hello followed by LF and hello followed by CRLF gives different values, and a UTF-8 file saved as UTF-16 with a BOM hashes differently again. If a checksum is meant for text, the publisher must state the encoding and line endings, or publish the hash of a canonical form. For binary files none of this applies; hash the bytes exactly as downloaded.

Checking a download and the metadata trap

On Windows, certutil -hashfile file.zip SHA256 prints the digest; on macOS and Linux, shasum -a 256 or sha256sum does the same. Compare the full string, not the first eight characters; a truncated prefix is fine for a quick glance and useless for a security check. Renaming a file does not change its hash, but editing one byte does.

Images and PDFs carry metadata inside the file: EXIF, XMP, document info, sometimes an embedded thumbnail. Two visually identical JPEGs with different EXIF have different hashes, and stripping metadata changes the hash even though the pixels are untouched. That is expected: the hash covers the container, not the picture. If you need a stable identifier for content, hash the decoded pixels or the extracted text, and say so explicitly.

Related tools

File hashes: SHA-256, SHA-1 and checks that fail | harmmy