File Integrity Checks: From Hash Comparison to Safe Downloads

You downloaded an installer from the official site and the release page lists SHA-256: f584a70d…. That digest answers exactly one question: do the bytes you hold match the bytes the publisher hashed? It says nothing about whether the publisher is trustworthy or the file is safe, but it stops mirror tampering, truncated downloads, and disk corruption — the failures that actually happen.

This article covers the workflow only. Why CRC32, MD5, and SHA-256 differ in adversarial strength, and why hash(secret + data) can be forged, belong to the security boundaries of checksums.

Why It Works: Determinism Makes "Same Digest" ≈ "Same File"

A hash is a deterministic function, and three properties together make digest comparison meaningful:

  • Determinism: the same bytes always produce the same digest, across machines, tools, and time;
  • Avalanche: one changed byte flips roughly half the output bits, so there is no "slightly changed input, slightly changed digest";
  • Fixed length: SHA-256 always emits 64 hex characters, whether the file is 1 KB or 10 GB.

The reverse does not hold: a different digest does not prove the file is bad. Most often you hashed the wrong object — wrong release, wrong path, or a different algorithm on each side.

One Command Per Environment

Environment Command Note
Linux sha256sum file Lowercase hex output
macOS shasum -a 256 file Or openssl dgst -sha256 file
PowerShell Get-FileHash .\file -Algorithm SHA256 Output is uppercase
cmd certutil -hashfile file SHA256 Output contains spaces; strip them
No terminal The text hash tool here Pick a file to hash SHA-256 / SHA-1 / MD5 and paste the official digest to compare locally

Hex comparison is case-insensitive: Get-FileHash prints uppercase and sha256sum prints lowercase, yet the numbers are identical. Normalise to lowercase before a character-by-character check.

The Standard Workflow, and How to Use a .sha256 File

  1. Obtain the digest from the vendor's HTTPS page, release notes, or a signed file — never from a forum, a file-sharing link, or a third-party blog.
  2. Match the algorithm: if the page says SHA-256, comparing against a local MD5 tells you nothing except that you used the wrong algorithm.
  3. Hash locally with any command above.
  4. Compare character by character, or paste the official value into the tool.
  5. Re-download on mismatch: if a second download still mismatches, stop and verify through official channels instead of "installing it anyway".

When the vendor publishes a .sha256 or SHA256SUMS file, check in bulk:

sha256sum -c SHA256SUMS
# ubuntu-24.04.iso: OK

Each line is <digest> <filename>; binary-mode files add a * before the filename, which -c understands. The filename must match yours exactly, extension and version included — a mismatch makes -c fail, which is itself a safeguard.

PowerShell has no -c equivalent: hash with Get-FileHash and compare yourself, or paste the digest into the tool here, which computes several algorithms and reports the match.

When It Mismatches: Four Things That Look Identical

Symptom More likely cause How to verify
Different digest, same file size Wrong release (.zip vs .tar.gz, x64 vs arm64) Check the filename and version against the official page
Text file mismatch An editor added a BOM or converted LF to CRLF Compare against the original with cmp, or download again
Large download stops at 99% Truncated transfer Compare byte count with the published size
Two runs, two different digests Wrong object hashed (a directory, several files, glob expansion) Confirm the command targets exactly one file

There is also a false match: the digest and the file come from the same mirror. An attacker able to replace both makes a successful comparison meaningless. That is not an algorithm failure but a channel failure — the digest must come from a different, trusted source.

One Byte Changes Everything

printf 'oltool' | sha256sum
# f584a70d13b8d82609042540654a08ff02254268e11625383e72730c06c6f189

printf 'oltool\n' | sha256sum
# a84d2a66f90db7dc8e1d29de5d070f25b31faece1d2379fdcc9df1e9d4e18bc2

A single trailing newline and not one of the 64 characters matches. That is exactly the most common text-file mismatch: a Windows editor saved CRLF or added a BOM, so the content "looks unchanged" while the bytes changed.

The same bytes must produce the same digest across tools, which you can cross-check:

echo -n "hello world" | sha256sum
python -c "import hashlib; print(hashlib.sha256(b'hello world').hexdigest())"
# both print b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Mind echo -n: without it a newline is appended and the digest becomes a different value — the most common self-inflicted trap when verifying a command by hand.

Boundaries: Checksums Catch Accidents, Not Substitution

SHA-256 stops transfer corruption and a mirror "adjusting" a package, but it cannot stop a malicious publisher, a hijacked page that serves both file and digest, or the constructible collisions of MD5 and SHA-1. To prove who published a file, you need code signing and signature verification, not a hash.

The full adversarial discussion — error-detecting codes versus cryptographic hashes, the real threat model of MD5 collisions, length extension, and the KDF boundary of password storage — is in the security boundaries of checksums.

Frequently Asked Questions

What is a checksum, and how is it different from a password hash?

A checksum is a fixed-length, irreversible digest of the file's bytes, e.g. SHA-256 emits 64 hex chars. It neither encrypts nor reconstructs—it exists for comparison: identical content always yields the same digest, and flipping one bit changes it entirely. The difference from password hashing is in purpose and input model: file checksums usually aren't salted and often cover public data—they answer 'did the transfer/storage stay intact'; password hashes must survive digest disclosure without revealing the plaintext, so they add salt and use deliberately slow algorithms to resist cracking.

Why does the same small file give a different MD5 every time?

It almost always reduces to 'the bytes aren't actually identical': ①different newlines/encoding (CRLF vs LF, BOM vs no BOM) change the byte stream; ②downloads from different sources carry different versions; ③the file was opened and auto-resaved by some viewer/editor (rewrites bytes/ mtime even without visible changes); ④you hashed the wrong thing (a directory vs a single file). To tell: compare the two files' first/last bytes with a hex/binary diff tool, or just re-run the hash once. MD5 itself is deterministic—same input, same output, always.

Can verifying a published SHA-256 sum really prevent tampering?

It detects accidental corruption reliably, but only partially defends against targeted tampering. Against corruption: bit flips in transit, partial loss and silent server damage all surface immediately on comparison. The limitation: if an attacker can also replace the published checksum (swap both the download and its sha256 for a malicious pair), the comparison 'fails consistently' toward the wrong answer. So against targeted attacks you still need a trusted channel: compare against the official HTTPS domain, PGP signatures, or a trusted package manager's signatures. Conclusion: a checksum is a cheap, high-value integrity line but not key-level forge-proofing.

Should my project verify sha256 on every dependency download? Is it too slow?

Worth doing, and usually fast: SHA-256 on a 256 MB file takes roughly a second or two (modern CPUs have hardware acceleration). For security-critical dependencies (binaries, artifacts crossing a trust boundary) write the expected digest into a lockfile/script and enforce it; for dev-cache assets change-detection is enough. For speed plus robustness use a faster checksum (BLAKE2/xxhash); for tamper-resistance use a lockfile plus official signatures rather than laziness. Note: 'large file size as checksum' carries zero integrity meaning—it's practically equivalent to nothing.

← Back to Blog