crccheck library
crccheck is a Python library (currently v1.3.1) for calculating various Cyclic Redundancy Checks (CRCs) and checksums from binary data. It includes over 170 CRC algorithms and simple additive/XOR checksums. The library maintains an active, though somewhat irregular, release schedule, with recent updates adding new CRC algorithms.
Warnings
- breaking Older versions (prior to v0.3) had a different package name (not specified but taken on PyPI) and used `bigendian=True/False` arguments. These were changed to `crccheck` and `byteorder='big'/'little'` respectively.
- gotcha Input data for CRC and checksum calculations must be binary (bytes or bytearray) in Python 3. Passing a regular string will result in a TypeError or incorrect results, as the library operates on byte sequences.
- gotcha The `calc()` class method is suitable for quick calculations on a single complete data block. For calculations involving multiple sequential data blocks, you must instantiate the CRC/checksum class, feed data using `process()`, and retrieve the final result with `final()`.
Install
-
pip install crccheck
Imports
- Crc32
from crccheck.crc import Crc32
- Checksum8
from crccheck.checksum import Checksum8
- Crc
from crccheck.crc import Crc
Quickstart
from crccheck.crc import Crc32
data_bytes = bytearray.fromhex("DEADBEEF01020304")
# Quick calculation for a single data block
calculated_crc = Crc32.calc(data_bytes)
print(f"CRC-32 for {data_bytes.hex()}: {calculated_crc:08X}")
# Calculation over multiple data blocks
multi_block_data1 = b"Hello"
multi_block_data2 = b"World"
crc_instance = Crc32()
crc_instance.process(multi_block_data1)
crc_instance.process(multi_block_data2)
final_crc = crc_instance.final()
print(f"CRC-32 for multiple blocks: {final_crc:08X}")