Lesson 8 of 8

Checksums

Goal

A number that exposes damaged bytes

01

Explanation

When you read a file format, you'll often find a number computed from the data bytes themselves. Its job is to catch corruption: if a single byte changes along the way, the number no longer matches. That number is called a checksum.

The simplest one adds up the bytes and takes the remainder modulo 256 so it fits in a single byte, which is sum(data) & 0xFF. But it's weak: if two bytes swap places, the sum stays the same.

That's why real formats use CRC32, a 32-bit number that changes with almost any small corruption, even two swapped bytes. In PNG, for example, every chunk ends with four bytes holding the CRC32 of the chunk's type and data, and in ZIP every file has its own CRC32. In Python, the zlib.crc32 function computes it:

import zlib
a, b = b"ab", b"ba"
sum(a) == sum(b)                # True
zlib.crc32(a) == zlib.crc32(b)  # False

In analysis you need it both ways: to confirm that the file you're reading is intact, and to recompute it when you modify a file of your own, because the program that reads the format rejects a chunk whose checksum doesn't match.

02

Your task

Write the function checksum8(data) that returns the sum of the bytes modulo 256, and the function verify(data, expected) that returns True if the CRC32 of the data equals expected and False otherwise. Compute it with zlib.crc32.

Checks

5 checks
  1. The byte sum wraps modulo 256
  2. The sum works on other inputs
  3. verify accepts intact data
  4. verify rejects corrupted data
  5. Computed with zlib.crc32
03

Hints

0/3
Track lessons0 of 8 done

Read assembly, calling conventions and control flow, then build a disassembler and an emulator for a tiny machine, patch its bytes and check their integrity.

Intermediate level, 8 lessons, 2 hours in total.

main.py
OutputPython (Pyodide)

Loading Python. This only happens once. Your browser keeps it after that.

–/5

Checks start once the environment is ready.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Read assembly, calling conventions and control flow, then build a disassembler and an emulator for a tiny machine, patch its bytes and check their integrity.

Intermediate level, 8 lessons, 2 hours in total.