Checksums and Hashing: sha256sum, b3sum and What Verification Proves

Checksums and Hashing: sha256sum, b3sum and What Verification Proves

Everyone has typed sha256sum and compared the first few characters by eye. That catches corruption, which is worth doing. It does not catch an attacker, and the difference matters more than the command does.

The commands

# compute
sha256sum file.iso
sha512sum file.iso
md5sum file.iso
b3sum file.iso           # BLAKE3, if installed

# a whole directory
find . -type f -exec sha256sum {} + > SHA256SUMS

# verify against a file of checksums
sha256sum -c SHA256SUMS
a1b2c3...  file.iso: OK
d4e5f6...  other.iso: FAILED
sha256sum: WARNING: 1 computed checksum did NOT match

Read the summary line. -c exits non-zero on any failure, which is what a script should check, and a human skimming the output can miss one FAILED among fifty OKs.

# in a script
if sha256sum -c SHA256SUMS --quiet; then
    echo "all good"
else
    echo "verification failed" >&2
    exit 1
fi

--quiet prints only failures, which is the right mode for automation. Our exit codes guide covers checking the status properly.

# useful options
sha256sum -c SHA256SUMS --ignore-missing   # file lists more than you have
sha256sum -c SHA256SUMS --strict           # fail on malformed lines
sha256sum --tag file.iso                   # BSD-style output

--ignore-missing is the one you want when a project publishes one checksum file covering every architecture and you downloaded one image.

Which algorithm

HashSpeedCollision resistantUse
md5FastestNo, brokenCorruption detection only
sha1FastNo, brokenLegacy, git internals
sha256GoodYesThe default choice
sha512Often faster than sha256 on 64-bitYesEqually fine
blake3Fastest of the secure onesYesLarge files, if available

md5 and sha1 are broken for adversarial use. Practical collision attacks exist, meaning someone can construct two different files with the same hash. For catching a corrupted download they remain adequate, and since sha256 is fast enough there is rarely a reason to choose them for new work.

# how much does it actually cost
time sha256sum big.iso
time sha512sum big.iso
time md5sum big.iso
time b3sum big.iso

On most 64-bit hardware sha512 beats sha256, counterintuitively, because it operates on 64-bit words. And modern CPUs have SHA extensions that make sha256 very fast indeed:

grep -o 'sha_ni' /proc/cpuinfo | head -1

b3sum parallelises across cores, which makes it dramatically faster on large files:

sudo apt install b3sum       # Debian and Ubuntu
cargo install b3sum          # anywhere with Rust

BLAKE3 is sound and less widely deployed, so you will still meet sha256 everywhere.

What verification actually proves

The part worth getting right.

# downloaded from example.com
wget https://example.com/app.tar.gz
wget https://example.com/SHA256SUMS
sha256sum -c SHA256SUMS
# app.tar.gz: OK

That proves almost nothing about authenticity. Anyone who could replace app.tar.gz on that server could replace SHA256SUMS too. You have verified a claim against itself.

What it does prove: the file arrived intact. A truncated download, a corrupted mirror, a bad disk, a flaky network. Those are real and worth catching.

Signatures are the other half

wget https://example.com/SHA256SUMS
wget https://example.com/SHA256SUMS.asc

# get the key from somewhere else
gpg --keyserver keys.openpgp.org --recv-keys 0xDEADBEEF

gpg --verify SHA256SUMS.asc SHA256SUMS
sha256sum -c SHA256SUMS --ignore-missing

Two steps, in that order:

  1. Verify the signature on the checksum file, which proves the project produced it
  2. Verify the file against the checksum, which proves the file matches
# check the fingerprint against what the project publishes
gpg --fingerprint 0xDEADBEEF

Get the key from somewhere other than the download page. A keyserver, the project’s source repository, a distribution package, or a release you already trusted. Our gpg guide covers the mechanics and our supply chain guide covers why this distinction decides whether verification means anything.

A signature with the right fingerprint is still only as good as your confidence that the fingerprint belongs to who you think. That is the part no command solves.

Where it already happens for you

# APT verifies repository metadata signatures automatically
ls /etc/apt/keyrings/ /etc/apt/trusted.gpg.d/

# RPM the same
rpm -qa gpg-pubkey* --qf '%{summary}\n'

# verify installed files against the package
debsums -c                   # Debian and Ubuntu
rpm -Va                      # Fedora and RHEL

Installing from your distribution’s repositories gets you signature verification without thinking about it, which our packages and repositories guide covers. Fedora 45 now enforces RPM signature checking by default, closing a configuration that previously allowed bypassing it.

debsums -c and rpm -Va are worth knowing: they compare installed files against the package’s recorded hashes, which is how you find a file that has been modified since installation.

# rpm -Va output, the columns that matter
# 5 means the hash differs, T the mtime, S the size
rpm -Va | grep '^..5'

Verifying a large transfer

# compute at both ends, compare
sha256sum bigfile.tar
ssh remote 'sha256sum /path/bigfile.tar'

# or in one go
ssh remote 'sha256sum /path/bigfile.tar' | diff - <(sha256sum bigfile.tar | sed 's|.*/||')
# rsync verifies as it goes, which is usually enough
rsync -a --checksum source/ dest/

# force a full verification pass
rsync -a --checksum --dry-run --itemize-changes source/ dest/

rsync without --checksum compares size and mtime, which is fast and misses a file whose content changed while keeping the same size and timestamp. That is rare and not impossible. --checksum reads everything, which is slow and definitive.

Our rsync guide covers the trade-off, and our Btrfs replication guide covers the case where the filesystem knows what changed and no comparison is needed.

Finding duplicates

# by hash, which catches identical content with different names
find . -type f -exec sha256sum {} + | sort | uniq -w64 -d

# faster: compare sizes first, hash only the candidates
find . -type f -printf '%s\n' | sort -n | uniq -d | while read -r size; do
    find . -type f -size "${size}c" -exec sha256sum {} +
done | sort | uniq -w64 --all-repeated=separate

Check sizes before hashing. Files of different sizes cannot be identical, so hashing everything is wasted I/O on a large tree. uniq -w64 compares only the first 64 characters, which is the sha256 hex digest.

Our find guide and uniq guide cover the pieces. For anything beyond occasional use, rdfind and jdupes do this properly and faster.

When two identical files hash differently

# they are not identical, so find out where
ls -l file1 file2
cmp file1 file2
cmp -l file1 file2 | head

Line endings are the usual answer. A file transferred in text mode, or edited on Windows, has \r\n where the original had \n:

file file1 file2
# file1: ASCII text
# file2: ASCII text, with CRLF line terminators

# normalise and compare
dos2unix < file2 | sha256sum

A trailing newline is the other one. Many editors add one; many generators do not.

tail -c1 file1 | xxd
# 0a means it ends with a newline

Our cmp guide covers finding the first differing byte and our diff guide covers seeing what changed.

Hashes elsewhere

# git uses sha1, moving to sha256
git hash-object file.txt
git rev-parse HEAD

# container images are identified by digest
podman image inspect myimage --format '{{.Digest}}'
docker pull myimage@sha256:abc123...

# password hashing is a different problem entirely
mkpasswd -m sha512crypt

Password hashing is not this. A fast hash is exactly wrong for passwords, because the attacker benefits from speed. Those use deliberately slow algorithms with salts and work factors: yescrypt, bcrypt, argon2. Our user accounts guide covers what /etc/shadow holds.

Container digests are the reason to pin by digest rather than tag, as our supply chain guide argues: a tag is a mutable pointer and a digest is the artifact.

A habit worth adopting

# for anything downloaded outside your package manager
curl -LO https://example.com/tool.tar.gz
curl -LO https://example.com/tool.tar.gz.asc
gpg --verify tool.tar.gz.asc tool.tar.gz || { echo "REFUSING"; exit 1; }
tar -tzf tool.tar.gz | head        # look before extracting
tar -xzf tool.tar.gz

tar -t before tar -x is the other half: see what the archive contains before letting it write into your filesystem. An archive with absolute paths or ../ components is a problem, and modern tar refuses those by default, which is worth not relying on. Our tar guide covers it.

And the thing worth repeating from our supply chain post: curl | sudo bash gives you nothing to verify and no record of what ran. Download, verify, read, then run.

Frequently Asked Questions

What is the difference between a checksum and a signature?

A checksum proves a file has not changed since the checksum was computed, which catches corruption. A signature proves who produced it, which catches substitution. A checksum hosted on the same server as the download proves nothing about origin, because anyone who can replace the file can replace the checksum.

Is md5 still safe to use for anything?

For detecting accidental corruption, yes, and it is faster than the alternatives. For anything where someone might deliberately produce a collision it is broken, and has been for years. Since sha256 is fast enough on modern hardware, there is rarely a reason to choose md5 for new work.

What is b3sum and why is it faster?

BLAKE3 is a newer hash designed to parallelise across CPU cores and use SIMD instructions, so it can be several times faster than sha256 on large files. It is cryptographically sound and not yet as widely deployed, so you will still meet sha256 far more often.

How do I verify a file against a checksum file?

Use the -c option, which reads the checksum file and reports OK or FAILED per entry. Combine it with —ignore-missing when the file lists more entries than you downloaded, and read the summary line rather than assuming silence means success.

Why do two files with identical content have different checksums?

They do not, if the content is genuinely identical. A difference means the bytes differ somewhere, which is usually a line ending conversion, a trailing newline, or a transfer in text mode. Compare sizes first, then use cmp to find the first differing byte.

Where should I get a signing key from?

Anywhere other than the page hosting the download. A keyserver, the project source repository, a distribution package, or a release you already trusted. A key obtained from the same place as the file verifies that file against its own claim, which establishes nothing.