Computing Library › Number Systems & Information
Number Systems & Information

Checksums

A checksum is a small value computed from data to detect accidental corruption during storage or transfer.

The idea

A checksum condenses a block of data into a short value using a fixed function. The sender transmits it alongside the data; the receiver recomputes it and flags a mismatch as corruption.

Simple sums

Kronos motion — data assimilation

The most basic checksum adds up the data bytes or words modulo some value. The Internet checksum sums 16-bit words with end-around carry using one's complement arithmetic, cheap enough to compute on every packet.

Strengths and weaknesses

Additive checksums are fast but weak: reordering bytes or two offsetting errors can leave the sum unchanged. They catch common random noise well but are not designed to resist deliberate tampering.

Beyond simple sums

Stronger detection uses cyclic redundancy checks, which treat data as polynomial coefficients. For integrity against adversaries, cryptographic hashes are required; ordinary checksums assume only accidental errors.

Where checksums appear

Error detection versus correction

A checksum only detects that something changed; it cannot say what or repair it. Recovery requires either retransmission or an error-correcting code with enough redundancy to reconstruct the original.

python
def internet_checksum(data):
    s = 0
    for i in range(0, len(data), 2):
        w = data[i] << 8 | (data[i+1] if i+1 < len(data) else 0)
        s += w
        s = (s & 0xFFFF) + (s >> 16)  # end-around carry
    return ~s & 0xFFFF