LearnastraTHE DESIGN COURSE

Understand the building blocks / CHAPTER 22

Checksums and data integrity

Detect changed bytes with checksums while separating accidental corruption from authenticity.

Intermediate1h of deliberate study10 questions

Builds on System design building blocks, Redundancy and replication.

By the end, you can
  • 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?

  1. The sender computes a compact value from the intended bytes using a defined algorithm.
  2. 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.
  3. The receiver computes the value again from the downloaded bytes.
  4. 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:

  1. 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.
  2. With a digital signature, the sender signs using a private key. The receiver verifies using the corresponding public key and the chosen standard scheme.
  3. 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:

  1. 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.
  2. Download a chunk and compute its digest using the specified algorithm.
  3. Compare it with the digest assigned to that position, and verify the expected length.
  4. If chunk 37 mismatches, reject or quarantine those bytes. Request a replacement from a source whose bytes can pass the same trusted check.
  5. Place every verified chunk in its assigned position.
  6. 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

Verification needs a trusted manifest and a recovery pathGet 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. Numbered arrows correspond to the connection key below. All connections are directional.Trusted manifestPosition, length,digest for each chunkDownloaded chunk37Bytes may havechangedRecompute digestSame definedalgorithmCompare withreferenceMismatch: do notacceptVerifiedalternativeRetry chunk 37 onlyAssemble in orderCheck complete-filedigest123456Verification needs a trusted manifest and a recovery pathGet 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. Numbered arrows correspond to the connection key below. All connections are directional.Trusted manifestPosition, length,digest for each chunkDownloaded chunk37Bytes may havechangedRecompute digestSame definedalgorithmCompare withreferenceMismatch: do notacceptVerifiedalternativeRetry chunk 37 onlyAssemble in orderCheck complete-filedigest123456Verification needs a trusted manifest and a recovery pathGet 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. Numbered arrows correspond to the connection key below. All connections are directional.Trusted manifestPosition, length,digest for each chunkDownloaded chunk37Bytes may havechangedRecompute digestSame definedalgorithmCompare withreferenceMismatch: do notacceptVerifiedalternativeRetry chunk 37 onlyAssemble in orderCheck complete-filedigest123456

Follow the numbered arrows

  1. Downloaded chunk 37 → Recompute digesthash received bytes
  2. Recompute digest → Compare with referencecurrent digest
  3. Trusted manifest → Compare with referenceexpected digest for 37
  4. Compare with reference → Verified alternativerecover on mismatch
  5. Verified alternative → Assemble in orderverified bytes in bound position
  6. Trusted manifest → Assemble in orderdefines structure
The trusted manifest states the expected bytes and position of chunk 37. A mismatch triggers replacement checked against that same reference. The receiver then places verified chunks in order and checks the complete file.
Trace the diagram in words
  1. Get the expected digest, length, and position from the trusted manifest.
  2. Calculate the digest of the downloaded chunk and compare it.
  3. Replace a mismatching chunk only with bytes that pass the trusted check.
  4. 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.

  1. Calculate the replacement transfer in the simple retry model.
  2. Explain how to choose and verify replacement bytes.
  3. Explain how to validate the complete assembled report.
Your self-check
Compare the worked solution
  1. Two chunks at 2 MB require 2 × 2 = 4 MB of replacement transfer, rather than another 200 MB, assuming the other chunks remain verified.
  2. Keep the authenticated manifest unchanged. Reject the bad chunks and fetch replacements whose digests and lengths match that same trusted reference.
  3. Put each verified replacement in its assigned position, assemble all chunks in the specified order, and check the whole-file digest when provided.
  4. Another replica is a candidate source, not automatic proof of correctness. Accept its bytes only if they pass the trusted check.
  5. 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?
  1. Obtain the expected value through a trusted path.
  2. Recompute it from received bytes.
  3. 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?
  1. Reject the mismatching bytes and retain the trusted expected digest.
  2. Fetch a replacement and verify it against that reference.
  3. Check its length and position, then verify the assembled file.
  4. Report failure when no verified replacement exists.
Practise this topic in the revision deck ↗

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.

1 / 10Chunk 37 mismatches its trusted digest. What should happen first?
2 / 10All 100 chunks verify individually but are assembled in reverse order. What is missing?
3 / 10An attacker replaces both a file and its plain published hash. What can happen?
4 / 10Only one 1 MB chunk of a 100 MB file is damaged. What can per-chunk checks save under the simple retry model?
5 / 10What is a CRC commonly chosen for?
6 / 10What does HMAC require that a public plain digest does not?
7 / 10What must be trusted when verifying a digital signature?
8 / 10Does a content-addressed identifier grant read permission?
9 / 10Why run background scrubbing?
10 / 10A checksum matches. What is the careful conclusion?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source