liveBook · Manning

Checksum

As explained in Rust in Action: Systems programming concepts and techniques

A checksum is a value computed from data and stored or transmitted with it so later recomputation can detect changes or corruption.

A checksum is a value computed from data and stored or transmitted alongside it. Recomputing the value later and comparing the results can reveal interrupted writes, transmission errors, or corrupted data. section 7.7.4

Use a checksum by following this cycle:

  • Before writing data, apply a checking function to its bytes and store the result beside the original data. section 7.7.4
  • When reading, calculate a fresh checksum from the data and compare it with the saved value. section 7.7.4
  • Treat a mismatch as corruption and report an error rather than trusting the data. section 7.7.4

Parity checking

A parity bit counts the 1 bits in a byte stream and records whether their total is even or odd. For the bytes representing “abc”, the counts are 3, 3, and 4, producing 00000001; adding “d” changes the result to 00000000. A Rust implementation loops over a byte slice, uses count_ones(), totals the counts, and returns whether the total is even. section 7.7.4

Figure 7.3 A single record in the Bitcask file format. To parse a record, read the header information, then use that information to read the body. Lastly, verify the body’s contents with the checksum provided in the header.

A Bitcask record places the checksum in a header with key and value lengths: read the header, read the body, then verify the body against the stored checksum. section 7.7.2

Checksum choices trade simplicity, speed, and reliability. Parity bits are fast and easy but prone to errors; CRC32 is more complex and more trustworthy; cryptographic hash functions are slower but provide stronger assurance. section 7.7.4

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains the save-and-compare procedure, compares checksum families, and implements parity checking in Rust.
Examples
Uses Bitcask record headers, byte streams such as “abc” and “abcd”, and a Rust parity_bit function.
What it assumes
Assumes familiarity with bytes and file I/O while teaching checksum computation and comparison directly.
What you can do after
Build it from scratch by implementing parity checks and applying saved checksums during record parsing.
How it connects
Connects checksums with parity bits, CRC32, cryptographic hash functions, file formats, and serialization.

Also explained in

Compare how 5 books explain checksum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.