Sub topic 13
Using SHA-256 to check integrity
This chapter assumes you know what a hash function is, why collisions exist, and the three cryptographic resistance properties: preimage, second-preimage, and collision resistance. You should also be comfortable reading Python and hexadecimal.
The integrity-check pattern
An integrity check answers one question: is this the same data I expected? SHA-256 is a cryptographic hash from the SHA-2 family. It turns any input into a 256-bit digest, usually shown as 64 hex characters.
The pattern has three steps:
- The publisher computes the digest of a file and publishes it through a trusted channel.
- You download the file and compute its digest yourself.
- You compare the two digests. If they match, the file is the one the publisher hashed.
This works because of the properties from the previous chapters. Changing one input bit changes about half of the output bits (avalanche). Finding a different file with the same digest is computationally infeasible (second-preimage resistance).
Computing a digest
This example hashes a small message and prints the 64-character result.
import hashlib
data = b"release-1.0.tar.gz contents"
print(hashlib.sha256(data).hexdigest())The same input always gives the same digest, on any machine. Change one byte and the digest looks unrelated.
Hashing large files
Real files may not fit in memory. The hashlib object accepts data in pieces through update, so you can read a file in chunks. This function runs as shown.
import hashlib
import hmac
def sha256_file(path: str, chunk_size: int = 65536) -> str:
h = hashlib.sha256()
with open(path, "rb") as f:
while chunk := f.read(chunk_size):
h.update(chunk)
return h.hexdigest()
def verify(path: str, expected_hex: str) -> bool:
actual = sha256_file(path)
return hmac.compare_digest(actual, expected_hex.lower())Chunked hashing gives the same digest as hashing the whole file at once. Memory use stays constant at one chunk.
What an integrity check does not prove
The check is only as trustworthy as the channel that delivered the expected digest.
- If the file and its digest sit on the same compromised server, an attacker replaces both. The match proves nothing.
- A plain hash has no secret. Anyone can compute a valid digest for any data, so it detects accidental corruption and tampering only when the digest comes from a source the attacker cannot change.
- To prove who produced the data, you need a keyed construction (HMAC) or a digital signature. The next chapter covers HMAC.
Common mistakes
- Comparing hex strings with different letter case. Normalize with
.lower()before comparing. - Hashing text instead of bytes. Encode text explicitly, for example with UTF-8, or you may hash a different byte sequence than the publisher did.
- Opening a file in text mode. Newline translation can change the bytes. Always open with
"rb". - Using MD5 or SHA-1 for new checks. Both have practical collision attacks, so an attacker can craft two different files with one digest.
Exercise
A download page lists a SHA-256 digest right next to the download link, both served from the same web server. Why is this weak protection against a malicious attacker, and what does it still protect against?
Show answer
An attacker who controls the server can replace the file and the digest together, so the match proves nothing about authenticity. It still detects accidental corruption, such as a truncated or damaged download, because random damage will not produce a matching digest.