Calculating checksums¶
The Calculator combines a configuration with a register implementation
and exposes two methods: checksum and verify.
Create a calculator¶
Calculate a checksum¶
from crc import Calculator, Crc8
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT, optimized=True) # (1)!
assert expected == calculator.checksum(data)
- Builds a lookup table upfront, which trades a bit of memory and setup time for significantly faster checksum calculation.
Verify a checksum¶
Standard vs. optimized¶
| Standard | Optimized | |
|---|---|---|
| Created with | Calculator(config) |
Calculator(config, optimized=True) |
| Register used | Register |
TableBasedRegister |
| Setup cost | None | Builds a lookup table |
| Calculation speed | Slower | Significantly faster |
| Best for | One off checksums | Lots of data, repeated use |
Create it once, reuse it
Creating an optimized calculator builds a lookup table. Create it once and reuse it, rather than creating a new one for every checksum.
Supported input types¶
Both checksum and verify accept a wide range of input types, see
InputType for the formal definition.
from crc import Calculator, Crc8
expected = 0xF4
calculator = Calculator(Crc8.CCITT, optimized=True)
with open("afile.txt", "rb") as f: # (1)!
assert calculator.checksum(f) == expected
- Make sure to open the file in binary mode (
"rb"), files are read in chunks, so even large files do not need to fit into memory.
from crc import Calculator, Crc8
class ByteConvertible:
def __init__(self, data):
self._data = data
def __bytes__(self):
return self._data.encode("utf-8")
expected = 0xF4
calculator = Calculator(Crc8.CCITT, optimized=True)
data = ByteConvertible("123456789")
assert calculator.checksum(bytes(data)) == expected
Next steps¶
- Configurations — pick or define the CRC algorithm
- Raw registers — drive the calculation yourself
CalculatorAPI reference