What Is a Checksum? A Small Number That Catches Errors in a Large One
By the BrainSnail editorial team. How these articles are written and checked, and how to tell us when one is wrong.
Appending a few extra digits computed from the data lets a receiver detect that something got corrupted. The technique is in every bank account number, every barcode and every network packet, and it fails in specific knowable ways.
The basic idea
A checksum is a short value calculated from a larger body of data by a fixed procedure and transmitted or stored alongside it. The receiver applies the same procedure to the data they received and compares the result with the value supplied, and a mismatch proves that something changed. The technique detects errors without needing a second copy of the data, which is the point, since transmitting everything twice would be expensive and would still leave the question of which copy is right. What a checksum cannot do is prove that the data is correct, since different data can produce the same value, and it cannot generally say where the error is or repair it, which requires a more elaborate scheme. It answers one question, which is whether corruption is detectable, and answers it cheaply.
Where they appear
The technique is embedded in systems most people use daily without noticing:
- •The last digit of a bank card number, computed from the others to catch mistyping
- •The final digit of a book identifier and of most barcodes
- •International bank account numbers, which carry two check characters
- •Network packets, which carry checksums at several layers so corruption is caught in transit
- •Downloaded files published with a hash so the recipient can confirm nothing was altered
- •Memory in servers, where extra bits both detect and correct single-bit errors
- •Vehicle identification numbers, national identity numbers and many similar codes
What they catch and what they miss
Different schemes are designed against different error patterns and the design choices are deliberate. A simple sum of the digits catches any single wrong digit but misses two digits being swapped, since addition does not care about order, which is a serious gap because transposition is one of the commonest human errors. Weighting each position differently before summing fixes that, which is why the standard schemes multiply alternate digits by different factors. Adding digits in a way that discards carries misses certain paired errors that a different arithmetic catches. A single parity bit catches any odd number of flipped bits and misses every even number. Longer and more elaborate functions catch bursts of consecutive errors, which is the characteristic failure of a physical transmission line, and the polynomial schemes used in networking are chosen specifically for that property.
How the card number one works
The scheme protecting bank card numbers is simple enough to follow fully and illustrates the design choices. Starting from the right, every second digit is doubled, and where doubling gives a two-digit result the digits of that result are added together, which keeps everything single-digit. All the digits are then summed, and the number is valid if that total is a multiple of ten. The final digit of the card number is chosen when the card is issued precisely to make the total come out right. The doubling of alternate positions is what catches transposed neighbouring digits, since swapping two adjacent digits moves one of them between a doubled and an undoubled position and changes the total. The scheme was patented in the 1950s, is in the public domain, catches every single-digit error and nearly every transposition, and takes a few lines to implement.
Detection, correction and tampering
Three different goals are easily confused. Detection tells you something is wrong and requires only a small addition to the data. Correction lets the receiver repair the error without asking for a retransmission, which requires considerably more redundancy and is used where asking again is impossible or expensive, including deep space communication, optical discs that must tolerate scratches, and memory in systems that cannot afford a fault. Protection against deliberate tampering is a different problem again, since an attacker who changes the data can simply recompute the checksum, so the schemes used for accidental errors provide no security at all, and resisting tampering requires a cryptographic function combined with a secret or a separately trusted copy of the expected value. Publishing a hash beside a download only helps if the hash itself cannot be replaced by whoever replaced the file.
The takeaway
A short value computed from the data by a fixed procedure lets a receiver detect corruption without a second copy, and a mismatch proves something changed while a match proves nothing. Weighting positions before summing catches transposed digits, which a plain sum misses. Detecting errors, correcting them and resisting deliberate tampering are three different problems needing different schemes.