Lesson 8 of 8
Checksums
- 15 min
- 5 checks
- 3 hints
- Go to task
Goal
A number that exposes damaged bytes
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.
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- The byte sum wraps modulo 256
- The sum works on other inputs
- verify accepts intact data
- verify rejects corrupted data
- Computed with zlib.crc32