Understand the building blocks / CHAPTER 22
Checksums and data integrity
Detect changed bytes with checksums while separating accidental corruption from authenticity.
Builds on System design building blocks, Redundancy and replication.
- Explain checksum generation and verification.
- Distinguish non-cryptographic checksums, cryptographic hashes, and authenticated verification.
- Design recovery after a mismatch.
01Keep a reference to what the bytes should be
A file service sends a 100 MB report in 100 chunks of 1 MB. How can the receiver detect a changed chunk rather than merely accept whatever bytes arrive?
- The sender computes a compact value from the intended bytes using a defined algorithm.
- The receiver gets the expected value from the intended file service over a TLS connection that verifies that service’s identity, rather than accepting a replacement value bundled with an untrusted download.
- The receiver computes the value again from the downloaded bytes.
- It compares the values. A mismatch means the bytes and reference disagree.
That compact value is a checksum, or a digest when discussing a hash calculation. A digest has a fixed output size even when the file is large. A mismatch does not reveal the cause and cannot reconstruct the original bytes.
For the report, obtain a trusted manifest from that service: a list of chunk positions, expected lengths, and expected digests, plus the named digest algorithm and complete-file digest. The positions matter because correct chunks in the wrong order can still form the wrong report.
Choose the method according to the threat:
- A cyclic redundancy check, or CRC, efficiently detects common accidental changes under its error model.
- A cryptographic hash is designed to make deliberate collision-finding difficult under its security assumptions.
- A collision means different byte sequences produce the same output. Finite outputs mean collisions remain possible; a matching digest is evidence under the chosen method, not a mathematical unique-file guarantee.
02Separate corruption detection from authenticity
An attacker replaces a file and also replaces the plain hash published beside it. The receiver recomputes the replacement’s hash and sees a match. The comparison worked, but its reference was untrusted.
Separate two questions:
- Integrity asks whether the bytes agree with the expected reference.
- Authenticity asks whether the message or reference came from the intended source.
Use a trusted reference or an established authentication method:
- With HMAC, sender and verifier share a secret key. The sender uses the standard algorithm to produce a message authentication code; the verifier uses the shared secret to check it. Someone without the key should not be able to create a valid code for altered data.
- With a digital signature, the sender signs using a private key. The receiver verifies using the corresponding public key and the chosen standard scheme.
- The receiver must know that the public key belongs to the intended signer. Accepting an attacker’s replacement file, signature, and replacement key does not establish the original source.
Use established schemes and key-management rules. Inventing a hash-plus-secret formula is not a substitute for them.
A digest can also name stored content. This is content-addressed storage:
- Recomputing a trusted content identifier helps detect unexpected returned bytes.
- Matching content identifiers can help identify duplicate files and reuse stored bytes.
- The identifier does not decide who may read the file, whether the content is safe, or whether the uploader owns it.
Byte checking, source authentication, and access permission remain separate decisions.
03Detect the problem, then recover safely
Verify and repair the chunked report in order:
- Obtain the intended manifest from the authenticated file-service connection, or verify it using the HMAC/signature method described above. Do not trust a new manifest merely because it arrived beside the suspect bytes.
- Download a chunk and compute its digest using the specified algorithm.
- Compare it with the digest assigned to that position, and verify the expected length.
- If chunk 37 mismatches, reject or quarantine those bytes. Request a replacement from a source whose bytes can pass the same trusted check.
- Place every verified chunk in its assigned position.
- Check the assembled file against its complete-file digest when provided.
In the simple retry model, replacing one bad 1 MB chunk avoids retransmitting the other 99 MB. Checking the entire assembly additionally helps detect wrong ordering or assembly mistakes.
Handle failure honestly:
- Do not replace the expected digest with the newly calculated one just to silence an error. That declares the suspect bytes correct without evidence.
- Another replica is useful only when its bytes can be verified against the trusted reference. “Different machine” alone does not establish correctness.
- If no verified replacement exists, report the failure rather than accepting unknown bytes.
Periodically reading stored data and comparing its expected digest is called background scrubbing. It can detect damage in a report nobody has opened recently. Repair still needs verified replacement bytes.
Monitor mismatches by storage location and record repair failures. The complete design includes an expected reference, a comparison, and a recovery action; calculating a hash supplies only part of that design.
Verification needs a trusted manifest and a recovery path
Follow the numbered arrows
- Downloaded chunk 37 → Recompute digesthash received bytes
- Recompute digest → Compare with referencecurrent digest
- Trusted manifest → Compare with referenceexpected digest for 37
- Compare with reference → Verified alternativerecover on mismatch
- Verified alternative → Assemble in orderverified bytes in bound position
- Trusted manifest → Assemble in orderdefines structure
Trace the diagram in words
- Get the expected digest, length, and position from the trusted manifest.
- Calculate the digest of the downloaded chunk and compare it.
- Replace a mismatching chunk only with bytes that pass the trusted check.
- Assemble chunks in their assigned positions and verify the complete report.
DRAW. CALCULATE. EXPLAIN.
Repair a chunked report
20 minutes · guided practice
A 200 MB report contains 100 ordered chunks of 2 MB. Chunks 12 and 37 disagree with an authenticated manifest.
- Calculate the replacement transfer in the simple retry model.
- Explain how to choose and verify replacement bytes.
- Explain how to validate the complete assembled report.
Compare the worked solution
- Two chunks at 2 MB require
2 × 2 = 4 MBof replacement transfer, rather than another 200 MB, assuming the other chunks remain verified. - Keep the authenticated manifest unchanged. Reject the bad chunks and fetch replacements whose digests and lengths match that same trusted reference.
- Put each verified replacement in its assigned position, assemble all chunks in the specified order, and check the whole-file digest when provided.
- Another replica is a candidate source, not automatic proof of correctness. Accept its bytes only if they pass the trusted check.
- If no source passes, report or quarantine the failure. Replacing the expected digest would remove the evidence rather than repair the report.
The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.
RETRIEVE BEFORE YOU REVEAL
Keep the important ideas.
Say the answer aloud, then turn the card over.
CARD 01What does a checksum comparison establish?
- Obtain the expected value through a trusted path.
- Recompute it from received bytes.
- Compare the two values.
A mismatch says the bytes and reference disagree. It does not identify the cause or provide correct replacement bytes.
CARD 02How does byte integrity differ from source authenticity?
- Integrity compares bytes with a trusted expected reference.
- Authenticity establishes the intended source.
HMAC uses a shared secret. A signature uses a private signing key and a trusted public verification key. Neither grants access permission.
CARD 03How do I recover after a mismatch?
- Reject the mismatching bytes and retain the trusted expected digest.
- Fetch a replacement and verify it against that reference.
- Check its length and position, then verify the assembled file.
- Report failure when no verified replacement exists.
CHECK THE REASONING
Can you explain the choice?
Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.
CLOSE THE LOOP
Explain it without the page.
Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.