Verify a file's integrity with its hash
A hash changes completely as soon as a single byte of the file changes. Here is how to use it to check a download or a file you send.
Hash functions in a nutshell
A cryptographic hash function such as SHA-256 turns any sequence of bytes (a word, a document, a disk image of several gigabytes) into a fixed-size hash: 256 bits, or 64 hexadecimal characters. Three properties make it a verification tool:
- it is deterministic: the same content always gives the same hash, on any system and with any software;
- it is one-way: the hash cannot be turned back into the content;
- it is collision-resistant: nobody knows how to craft two different contents that give the same SHA-256 hash.
On top of that comes the avalanche effect: flipping a single bit of the input changes on
average half the bits of the hash, with no visible link to the original. Between onlyU and
onlyu, a single letter and a single bit differ:
onlyU a52c6ffe21c22ddbc9cf397c58aac0b9df45556730e1681b42ea0caf2c28611b
onlyu 358743d05665653e153c613a73d8263ce6c82e590b014f7bc032ca1665027d2c
Two hashes that “look alike” therefore mean nothing: only an exact match counts, character for character, over the whole length. Leave that comparison to a tool rather than your eyes: a tool never skips a character.
What a matching hash proves
If your file's hash equals the reference hash, your file is exactly the one the reference was computed from, byte for byte. A truncated download, a damaged disk sector, a file swapped for another version: all of these show up immediately. That is the guarantee of integrity.
The hash attests that the file is identical to the reference, and that is all: it says nothing about who produced it or whether its content is safe. Its value depends entirely on the source of the checksum. If the same person can modify both the file and the page that publishes its hash, they will replace both, and the comparison will still match perfectly. A trustworthy checksum therefore comes through a channel the attacker does not control: the official site over HTTPS when the file comes from a mirror, a separate message when the file comes from a colleague.
For authenticity: signatures
Proving who published a file is the job of a digital signature, verified with the publisher's public key:
- GPG: many projects sign their checksum file. With Ubuntu, for example,
gpg --verify SHA256SUMS.gpg SHA256SUMSconfirms the list really comes from the publisher, thensha256sum -cconfirms your files match it; - minisign: a lightweight alternative to GPG, used by a number of open source projects;
- Authenticode: the signature on Windows executables and installers, shown in the file's
properties (“Digital Signatures” tab) or with
Get-AuthenticodeSignaturein PowerShell.
If the sender and the recipient already share a secret key, an HMAC ties the hash to that key: only someone who holds it can produce the right value.
Compute a SHA-256 checksum from the command line
Every operating system can compute a SHA-256 hash with nothing to install.
Windows
# PowerShell
Get-FileHash .\file -Algorithm SHA256
# Command Prompt (cmd)
certutil -hashfile file SHA256
Get-FileHash prints the hash in upper case and certutil in lower case: case makes no
difference to the value. In PowerShell, the comparison takes one line, since the -eq operator
ignores case:
(Get-FileHash .\file -Algorithm SHA256).Hash -eq "published hash"
macOS
shasum -a 256 file
Linux
sha256sum file
# Verify every file listed in SHA256SUMS
sha256sum -c SHA256SUMS
A SHA256SUMS file holds one line per file: the hash, two spaces, then the name.
sha256sum -c recomputes each hash and answers OK or FAILED for each
file. The --ignore-missing option limits the check to the files present in the folder, handy
when you downloaded only one image out of ten. On macOS, shasum -a 256 -c SHA256SUMS does the
same.
The kit's file hash calculator gives the same result in the
browser, with an automatic comparison and the sha256sum-style line. For files of several
gigabytes, your system's command is better suited, as it reads the file in chunks.
SHA-1 and MD5: fine for transmission errors only
MD5 and SHA-1 still detect a file damaged by accident very well. Against deliberate tampering, however, they no longer protect you: researchers have been able to craft MD5 collisions since 2004, and SHA-1 collisions since 2017, when they published two different PDF files with the same hash. An attacker can thus prepare a harmless version and a booby-trapped version that share the same hash. To verify software, use SHA-256 or SHA-512; when a publisher only lists MD5 or SHA-1, rely on its signature if there is one.
Send the hash through a different channel from the file
When you send an important file yourself (a contract, a delivery archive, an installer), give your recipient the reference hash so they can check what they receive. Sent in the same email or the same share as the file, it would prove little: whoever intercepts and modifies one modifies the other. Sent separately, it forces an attacker to compromise two channels at once.
That is what “Send with onlyU” does from the calculator: it passes on the hash along with the file name and size. The hash goes from the kit to onlyU tab to tab in your browser, without appearing in an address or going through the kit's server. onlyU encrypts it in your browser and creates a link that opens only once, valid for 24 hours, then disappears. It is free, with no sign-up. Your recipient computes the hash of the file they received, with their system's command or with the kit, and compares.