Checksums
sha256sum, md5sum and friends — compute a hash, verify files against a list, why MD5 is not for security
A checksum (hash) is a short string that changes if even one byte of the file changes. It's used to verify that a file downloaded completely and wasn't tampered with, and to find files that changed. coreutils has a separate command for each algorithm, all with the same options: sha256sum, sha512sum, sha1sum, md5sum, b2sum.
Syntax
sha256sum [options] [file…] # compute
sha256sum -c [options] list # verify against a list
Without a file (or with -) it reads stdin.
Output format
5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 a.txt
The hash in hex, two spaces, the file name. If there's a * instead of the second space (… *a.txt), the file was read in "binary mode"; on Linux there's no difference. Such lines saved to a file are exactly the list for -c. For stdin - is printed instead of a name.
Options when computing
| Option | What it does |
|---|---|
--tag | BSD format: SHA256 (a.txt) = … — the algorithm is visible right away |
-b / -t | binary / text mode; on GNU/Linux the result is the same, only the * in the output changes |
-z | end lines with a NUL byte instead of \n |
Options when verifying (-c)
| Option | What it does |
|---|---|
-c list | for each line, recompute the file's hash and compare: name: OK or name: FAILED |
--quiet | don't print OK, only errors |
--status | print nothing; the result is only the exit code |
--ignore-missing | silently skip files that aren't on disk |
--strict | non-zero exit code if the list has malformed lines |
-w | warn about each malformed line with its number |
-c exit code: 0 — everything matched, 1 — there are mismatches, missing files or read errors.
Algorithms
| Command | Hash length | Good for |
|---|---|---|
md5sum | 32 hex characters | a quick check against accidental corruption; not for security |
sha1sum | 40 | outdated, collisions have been found; still seen in old lists and in git |
sha256sum | 64 | the default standard for verifying downloads |
sha512sum | 128 | the same SHA-2 family, longer |
b2sum | 128 | BLAKE2b: fast and strong |
Examples
# hash of a file
sha256sum archive.tar.gz
# just the hash, no name
sha256sum archive.tar.gz | cut -d' ' -f1
# hash of a string: echo without -n adds a newline and the hash will differ
printf '%s' 'hello' | sha256sum
# save the sums of a set of files
sha256sum *.txt > SHA256SUMS
# verify: are all files present and unchanged
sha256sum -c SHA256SUMS
# in a script — exit code only
sha256sum -c --status SHA256SUMS && echo 'all intact'
# check a file against a published hash (two spaces between hash and name)
echo '5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03 a.txt' | sha256sum -c
# sums for a whole directory tree, sorted by name
find data -type f -exec sha256sum {} + | sort -k2 > data.sums
# find identical files: same hash = same content
find . -type f -exec md5sum {} + | sort | uniq -w32 -D
Pitfalls
- MD5 and SHA-1 are not for security. Collisions can be crafted for them: two different files with the same hash. MD5 protects against accidental corruption during copying, not against deliberate substitution. To verify downloads use SHA-256 and compare against a hash from a trusted source, not from the same server as the file.
- Passwords aren't stored as plain hashes.
sha256sumis too fast — brute force takes minutes. For passwords there are bcrypt, scrypt, Argon2. - Paths in the list are relative — as they were written. Run
sha256sum -cfrom the same directory, otherwise you getFAILED open or read. - Malformed lines are ignored. A line not in
hash nameformat is skipped with a warning, and the check may still return0. Add--strict. - A newline changes the hash.
echo 'hello' | sha256sumandprintf 'hello' | sha256sumgive different results. Windows\r\nand a trailing space have the same effect. - The case of hex characters doesn't matter when verifying, but the algorithm does: a list from
md5sumfed tosha256sum -cgivesno properly formatted checksum lines found. - Names containing a newline or a backslash are escaped, and the output line starts with
\. That's normal;-cunderstands such lines.