Sub topic 11
Verifying a Download with a Checksum
This chapter assumes you can open Terminal and run a command, and that you know the difference between an app, a disk image (.dmg) and an installer package (.pkg). It covers what a checksum proves, how to compute one on macOS, how to compare it correctly, and where the method stops being useful.
What a checksum is
A checksum, in this chapter, means a cryptographic hash. A hash function reads every byte of a file and produces a short fixed-length string called a digest. SHA-256, the usual choice today, produces 64 hexadecimal characters.
Two properties matter here:
- The same file always gives the same digest.
- Changing even one byte gives a completely different digest. Nobody can practically craft a different file that produces the same digest.
So if the digest of your downloaded file matches the digest the developer published, your file is byte-for-byte identical to the file the developer meant you to get.
What a checksum proves, and what it does not
A checksum answers one question: is this the same file as the reference? It can catch:
- A download that was cut short or corrupted by a network fault.
- A file swapped or altered on a mirror, a CDN, or in transit.
- A file you got from an unofficial copy that differs from the original.
It cannot tell you:
- Whether the developer's software is safe. A malicious developer publishes a correct hash for a malicious file.
- Whether the reference value is genuine. If an attacker controls the page that hosts both the file and the hash, they can change both.
That second point drives how you should use checksums. The reference value must come from a channel the attacker does not control, or at least a different one from the download.
Computing a SHA-256 checksum on macOS
macOS includes shasum, which handles several algorithms. The -a 256 option selects SHA-256.
shasum -a 256 ~/Downloads/Example.dmgThe output has the digest, two spaces, then the file path:
3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea /Users/sam/Downloads/Example.dmgThe command reads the whole file, so large downloads take a few seconds. Nothing is printed while it works.
macOS also ships openssl, which gives the same digest in a different format:
openssl dfgst -sha256 ~/Downloads/Example.dmgIf you have a publisher's .sha256 file next to your download, shasum can check it for you:
cd ~/Downloads
shasum -a 256 -c Example.dmg.sha256The check file must contain lines in the form digest filename. The command prints Example.dmg: OK on a match and FAILED on a mismatch.
Comparing the values correctly
Do not compare 64 characters by eye. People skim the start and end and miss a difference in the middle. Let the shell compare them.
expected="3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea"
actual=$(shasum -a 256 ~/Downloads/Example.dmg | awk '{print $1}')
if [ "$expected" = "$actual" ]; then
echo "Checksum matches"
else
echo "MISMATCH: do not open this file"
fiPaste the expected value from the developer's page into expected. The script takes the first field of the shasum output, which is the digest, and compares the two strings exactly.
Case matters to the string comparison. Some sites publish uppercase hex. Convert both sides first if you see that:
expected=$(echo "$expected" | tr 'A-F' 'a-f')The workflow
A match is a gate, not the finish. After it, you still rely on code signing, notarization and Gatekeeper, covered in other chapters, to judge the app itself.
What a mismatch means
Work through the likely causes in order of how common they are:
- Incomplete download. The browser was interrupted. Delete the file and download again.
- Wrong file or version. You compared against the hash for a different release, or for the Intel build when you have the Apple silicon build. Check the version and architecture on the page.
- Wrong algorithm. The page lists SHA-512 or SHA-1 and you computed SHA-256. Use
shasum -a 512orshasum -a 1to match. - Hash of a different artifact. Some projects publish the hash of the
.appcontents or of a.zip, not of the.dmgyou downloaded. - Tampering. Rare, but this is what the check exists for. If a fresh download from the official site still mismatches, do not install it, and consider reporting it to the developer.
Edge cases and limits
- The file changes after you check it. A checksum covers the file at the moment you ran it. If you open a
.dmgand then drag the app out, the app is no longer the same file. Verify the.dmgbefore you mount it, not afterward. - Hashing a
.appfolder. A.appis a directory, andshasumworks on files only. To hash a bundle, you would compress it into a single archive first, and the digest would then depend on how it was compressed. Checksums fit disk images, packages and archives. For an installed app, usecodesign, covered in a later chapter. - Quarantine does not affect hashing. The
com.apple.quarantineattribute is stored outside the file's data, so the digest is the same whether or not it is present. - Weak algorithms. MD5 and SHA-1 have known collision attacks. They still detect accidental corruption, but they give weak protection against a deliberate swap. If a developer offers SHA-256, use it.
- Many developers publish nothing. A checksum is a bonus when it exists. When it does not, rely on the other signals in this tutorial: the source, the signature and notarization.
- Package managers. Homebrew casks record a SHA-256 for each download and check it automatically, which is one reason they are a reasonable alternative source.
Checksums compared with signatures
| Property | Checksum | Code signature |
|---|---|---|
| Detects corruption | Yes | Yes |
| Detects tampering | Only if the reference is trusted | Yes, against the signer's certificate |
| Identifies the publisher | No | Yes (certificate and Team ID) |
| Checked automatically by macOS | No | Yes, by Gatekeeper |
| Works on any file | Yes | Only on signed code and packages |
The two tools complement each other. A checksum tells you the file is the one the developer posted. A signature tells you who built it and that it was not altered after signing.
Practice
Exercise
You download Example.dmg. The developer's site shows a SHA-256 value, and you compute a different one with shasum -a 256. Name two harmless explanations and one action to take before deciding the file is malicious.
Show answer
Harmless explanations include an interrupted or corrupted download, and comparing against the hash for a different version or architecture (or a different algorithm such as SHA-512). Before deciding it is malicious, delete the file and download it again from the official site, then recompute the hash. If it still mismatches, do not install it and contact the developer.
Exercise
A download page hosts Example.dmg and, right beside it, a SHA-256 value. Why is this a weak check on its own, and what would make it stronger?
Show answer
An attacker who can replace the file on that page can replace the hash too, so the two will still match. The check is stronger if the expected hash comes from a separate, trusted channel such as the developer's main site over HTTPS, a signed release announcement, or a source you obtained independently of the download.
Exercise
Why should you verify the checksum of a .dmg before mounting it?
Show answer
The checksum covers the exact file you downloaded. Once you mount the image and copy the app out, you have a different object, a .app folder, that shasum cannot hash directly. Checking the .dmg first confirms the file is intact before anything from it runs.