Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

How hashing and checksums work

After this lesson: I can use a hash to check that data has not changed.

55 min lesson8 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Has this download been changed?

A hash is a short fingerprint of some data, worked out by a hash function. It lets you check that a file has not changed, without comparing every byte.

A software site publishes the fingerprint of its installer. You work out the fingerprint of the file you downloaded. If the two match, nothing was altered on the way.

By the end of this lesson

Note

I can use a hash to check that data has not changed.

Key words

WordWhat it means
Hasha short fixed-size fingerprint of some data, also called a digest
Hash functiona rule that turns any data into a hash
Checksuma hash used to spot a file that has changed
Integritydata is exactly as it was meant to be
Avalanche effecta tiny change in the data changes the whole hash
Collisiontwo different inputs that give the same hash

The cells students fill in are left blank in this preview.

A hash function gives data a fixed-size fingerprint

A hash function takes any amount of data and gives back a hash of one fixed size. SHA-256 always gives 256 bits, which is 64 hexadecimal characters.

The same input always gives the same hash. You cannot work backwards from a hash to the input. Run the next two and compare the answers.

Hash the word hello

Command
printf 'hello' | sha256sum

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Hash one letter different

Command
printf 'hellp' | sha256sum

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: one letter changes everything

InputWhat sha256sum prints before the two spaces and the dash
hello2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
hellpfdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a hash locks data away, like encryption. It does not. A hash has no key and nothing to unlock. You use it to check data, not to hide it. A short or guessable input can still be found by trying guesses.

A checksum file shows whether a download changed

A checksum file lists a hash for each file. To check a download, you hash your copy and compare. sha256sum -c does the comparing for you.

Run these five in order. They copy a report, record its hash, check it, then add a line and check again.

Copy the lab's report

Command
cp /srv/lab-web/report.csv report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Record its hash in a list

Command
sha256sum report.csv > sums.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check the file against the list

Command
sha256sum -c sums.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Add one line to the report

Command
echo 'Saturday,300' >> report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check the file again

Command
sha256sum -c sums.txt

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Use SHA-256, not MD5 or SHA-1

A collision is two different inputs with one hash. A good hash makes a collision impossible to find on purpose. MD5 and SHA-1 are broken: attackers can make two different files with the same hash.

Use SHA-256, SHA-512 or SHA-3. Remember that a hash shows the data is unchanged. It does not show who made it.

Worked example: which hash to use

HashSize in bitsHex charactersUse it to check data?
MD512832No, collisions can be made
SHA-116040No, collisions can be made
SHA-25625664Yes
SHA-512512128Yes
SHA3-25625664Yes

The cells students fill in are left blank in this preview.

The length of a hash

Code: python
import hashlib
text = "hello"
digest = hashlib.sha256(text.encode()).hexdigest()
# Change text to "hello world", then press Run.
# The length stays the same. Then change sha256 to md5.
print(len(digest))

Hash hello with MD5, to compare lengths

Command
printf 'hello' | md5sum

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.