Using hashes to check data integrity

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Using hashes to check data integrity

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A student downloads a file and its hash matches the hash published by the website. What does this show?
A The file is unchanged, so its integrity is intact
B The file has been encrypted during the download
2. A file's hash before sending is different from its hash after it arrives. What is the most likely cause?
A The file was altered or corrupted in transfer
B The file arrived exactly as it was sent
3. Which step is needed to check a file's integrity using a hash?
A Hash the file before and after transfer, then compare the two
B Hash the file once and assume it is safe
4. What is a file checksum?
A A password that locks the file
B A hash value calculated from a file's contents
5. An attacker changes one number in a spreadsheet during transfer. What happens to its hash?
A It stays the same, so the tampering is hidden
B It changes, so the tampering can be detected

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1sha256(before) == sha256(after) ADifferent hashes mean the file has changed. It may have been tampered with or corrupted, so it should not be trusted.
2sha256(before) != sha256(after) Bchecksum = sha256(file)
3A file checksum is the hash of a file, published by the sender so the receiver can check the download CWith no hash from before the transfer there is nothing to compare against, so tampering cannot be detected. The file must be hashed before and after.
4After a transfer, the receiver hashes the received file and compares it with the sender's hash to detect tampering Dif sha256(received) == sentHash
5Only hash the file after transfer, then assume it is unchanged EThe two hashes match, so the file has not changed during transfer and its integrity is intact
6Hashes differ, so the file is unchanged and safe to use FThe hashes differ, so the file was altered, corrupted or tampered with and its integrity is lost

Definitions

hash comparison

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
hashBefore ← ____(file)
2.
IF hashBefore _ hashAfter THEN OUTPUT "Data unchanged" ENDIF
3.
IF hashBefore _ hashAfter THEN OUTPUT "File was tampered with" ENDIF
4.
received ← download(url) hashAfter ← hash(________)
5.
IF checksum = published THEN OUTPUT "Safe" ____ OUTPUT "Altered" ENDIF

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A before ← HASH(file)
B after ← HASH(before)
C IF before = after THEN
D OUTPUT 'Unchanged'
E ELSE OUTPUT 'Tampered'
Question 2
A sent ← SHA256(report)
B got ← MD5(download)
C IF sent = got THEN
D OUTPUT 'Integrity kept'
Question 3
A expected ← 'a94f3c'
B actual ← CHECKSUM(expected)
C IF actual = expected THEN
D OUTPUT 'File is intact'
Question 4
A h1 ← COUNT(contract)
B SEND contract TO client
C h2 ← HASH(received)
D IF h1 ≠ h2 THEN ALERT
Question 5
A before ← file
B UPLOAD file
C after ← HASH(file)
D IF before ≠ after THEN
E OUTPUT 'Altered'

Answer Key

Using hashes to check data integrity
Section A: Quick Questions
Q1
A — The file is unchanged, so its integrity is intact
Q2
A — The file was altered or corrupted in transfer
Q3
A — Hash the file before and after transfer, then compare the two
Q4
B — A hash value calculated from a file's contents
Q5
B — It changes, so the tampering can be detected
Section B: Match the Pairs
Q1
sha256(before) == sha256(after) → The two hashes match, so the file has not changed during transfer and its integrity is intact
Q2
sha256(before) != sha256(after) → The hashes differ, so the file was altered, corrupted or tampered with and its integrity is lost
Q3
A file checksum is the hash of a file, published by the sender so the receiver can check the download → checksum = sha256(file)
Q4
After a transfer, the receiver hashes the received file and compares it with the sender's hash to detect tampering → if sha256(received) == sentHash
Q5
Only hash the file after transfer, then assume it is unchanged → With no hash from before the transfer there is nothing to compare against, so tampering cannot be detected. The file must be hashed before and after.
Q6
Hashes differ, so the file is unchanged and safe to use → Different hashes mean the file has changed. It may have been tampered with or corrupted, so it should not be trusted.
Section C: Definitions
Q1
hash comparison — Hash the file before and after transfer. If both hashes (checksums) match, the data is unchanged. A different hash shows the file was altered or tampered with, so its integrity is lost.
Section D: Fill in the Blanks
Q1
hashBefore ← hash(file)
Q2
IF hashBefore = hashAfter THEN OUTPUT "Data unchanged" ENDIF
Q3
IF hashBefore ≠ hashAfter THEN OUTPUT "File was tampered with" ENDIF
Q4
received ← download(url) hashAfter ← hash(received)
Q5
IF checksum = published THEN OUTPUT "Safe" ELSE OUTPUT "Altered" ENDIF
Section E: Spot the Error
Q1
B — The hash after transfer must be calculated from the received copy of the file, not from the earlier hash. It should be after ← HASH(copy).
Q2
B — Both hashes must use the same algorithm. A different algorithm gives a different value even for an identical file. It should be got ← SHA256(download).
Q3
B — The checksum must be calculated from the downloaded file. Calculating it from the expected value tells us nothing about the file. It should be actual ← CHECKSUM(file).
Q4
A — A hash function is needed to create a fingerprint of the data. Counting does not detect small changes. It should be h1 ← HASH(contract).
Q5
A — The variable must store the hash of the file, not the file itself. Otherwise a hash is compared with a whole file and the check is meaningless. It should be before ← HASH(file).