CRC (Cyclic Redundancy Check) Library
The `crc` library provides pure Python implementations for calculating, verifying, and configuring Cyclic Redundancy Check (CRC) checksums. It supports various predefined CRC configurations (e.g., CRC8, CRC16, CRC32, CRC64) and allows for custom configurations. Currently at version 7.1.0, the library is actively maintained with regular releases, often including new CRC configurations and bug fixes, and requires Python >=3.8 and <4.0.
Warnings
- breaking The `Crc16.CCITT` configuration was renamed to `Crc16.XMODEM`. The new `Crc16.KERMIT` configuration now provides the 'official' CCITT parameters.
- breaking Python 3.7 support has been removed.
- breaking The `SAEJ1850` configuration was adjusted to match the official specification. For users relying on the previously misconfigured values, a new configuration named `SAE_J1850_ZERO` was added.
- breaking The library transitioned from a single-file module (`crc.py`) to an src-based package layout. Direct imports that assumed a single `crc.py` file will no longer work.
- gotcha The `digest` function had unstable return values, particularly in configurations that required reverse output and when the CRC register was manually manipulated. Standard `Calculator` usage was generally unaffected.
Install
-
pip install crc
Imports
- Calculator
from crc import Calculator
- Crc8
from crc import Crc8
- Crc16
from crc import Crc16
- Configuration
from crc import Configuration
Quickstart
from crc import Calculator, Crc8
data = bytes([0, 1, 2, 3, 4, 5])
# Create a calculator with a predefined configuration (e.g., CRC-8 CCITT)
calculator = Calculator(Crc8.CCITT)
# Calculate the checksum
checksum = calculator.checksum(data)
print(f"CRC-8 CCITT checksum: {checksum:#04x}")
# Verify the checksum
assert calculator.verify(data, 0xBC)
print("Checksum verified successfully!")