Sub topic 11
Verifying a download with a checksum
This chapter assumes you can open Terminal, run a command, and tell an app, a disk image (.dmg) and an installer package (.pkg) apart. It covers one check: confirming that the file you downloaded is the file the developer published.
What a checksum tells you
A checksum is a short fingerprint calculated from a file's bytes. SHA-256 is the common algorithm. Change one byte of the file and the fingerprint changes completely. If your fingerprint matches the one the developer publishes, the file is identical to theirs.
A checksum catches two problems:
- A download that was damaged or cut short.
- A file that was swapped or altered after the developer published it, for example on a mirror server.
It does not prove the developer is trustworthy. If an attacker controls the page that lists the checksum, they can publish a matching fingerprint for a malicious file. Take the checksum from the developer's official site, ideally over HTTPS from a different page or channel than the download itself.
Calculating the hash
Use shasum with the -a 256 option and the path to the file:
shasum -a 256 ~/Downloads/Example.dmgThe output is a 64-character hexadecimal string followed by the file name. Compare it with the published value. Hex digits can be upper or lower case, so ignore case when you compare.
Comparing without eyeballing
Comparing 64 characters by eye is error-prone. Let the shell do it. Paste the published value into a variable and compare:
expected="paste-the-published-hash-here"
actual=$(shasum -a 256 ~/Downloads/Example.dmg | awk '{print $1}')
[ "$actual" = "$expected" ] && echo "MATCH" || echo "MISMATCH"If the developer publishes a file in the standard hash filename format, shasum -c does the comparison for you.
cd ~/Downloads
shasum -a 256 -c Example.dmg.sha256The -c option reads the listed hashes and prints OK or FAILED for each file.
The flow
Common errors
- Hashing the wrong file. If you open a disk image and copy the app out, the hash of the app does not match the hash published for the
.dmg. Hash the file exactly as downloaded. - Different algorithms. A 32-character value is MD5 and a 40-character value is SHA-1. Use
shasum -a 1ormd5only if that is what the developer published. SHA-256 is preferred. - Mismatch after a retry. Download again once. If the hash still differs, stop and do not open the file. The published value may be out of date, or the file may be tampered with.
Exercise
You download Example.dmg from a third-party mirror. The developer's official page lists a SHA-256 value. Your shasum -a 256 output differs from it by a few characters. What should you do, and why can't you just open the file to see if it works?
Show answer
Delete the file and download again, preferably from the official site. If the hash still differs, do not open it. Any difference means the file is not the one the developer published. It could be corrupted or altered, and a tampered file can look like it works while doing harm.