← Full reference

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

OptionWhat it does
--tagBSD format: SHA256 (a.txt) = … — the algorithm is visible right away
-b / -tbinary / text mode; on GNU/Linux the result is the same, only the * in the output changes
-zend lines with a NUL byte instead of \n

Options when verifying (-c)

OptionWhat it does
-c listfor each line, recompute the file's hash and compare: name: OK or name: FAILED
--quietdon't print OK, only errors
--statusprint nothing; the result is only the exit code
--ignore-missingsilently skip files that aren't on disk
--strictnon-zero exit code if the list has malformed lines
-wwarn about each malformed line with its number

-c exit code: 0 — everything matched, 1 — there are mismatches, missing files or read errors.

Algorithms

CommandHash lengthGood for
md5sum32 hex charactersa quick check against accidental corruption; not for security
sha1sum40outdated, collisions have been found; still seen in old lists and in git
sha256sum64the default standard for verifying downloads
sha512sum128the same SHA-2 family, longer
b2sum128BLAKE2b: 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. sha256sum is 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 -c from the same directory, otherwise you get FAILED open or read.
  • Malformed lines are ignored. A line not in hash name format is skipped with a warning, and the check may still return 0. Add --strict.
  • A newline changes the hash. echo 'hello' | sha256sum and printf 'hello' | sha256sum give different results. Windows \r\n and a trailing space have the same effect.
  • The case of hex characters doesn't matter when verifying, but the algorithm does: a list from md5sum fed to sha256sum -c gives no properly formatted checksum lines found.
  • Names containing a newline or a backslash are escaped, and the output line starts with \. That's normal; -c understands such lines.

See also

openssl · find · sort · uniq · base64