CRCmod-plus
crcmod-plus is a modernized Python package for generating objects that compute the Cyclic Redundancy Check (CRC). It is a drop-in replacement for the original `crcmod` 1.7 package, having dropped Python 2 support and updated the build pipeline for modern Python environments. It enables the use of any 8, 16, 24, 32, or 64-bit CRC, allowing users to specify custom polynomials or utilize a variety of predefined CRC algorithms. The library maintains a moderate release cadence, primarily focusing on Python compatibility and minor enhancements.
Warnings
- breaking crcmod-plus dropped support for Python 2.x. If migrating from the original `crcmod` 1.7, ensure your environment is Python 3.9 or newer.
- gotcha CRC functions and classes in `crcmod-plus` (and `crcmod`) expect byte strings (`bytes`) as input for Python 3.x. Passing Unicode strings (`str`) will result in a TypeError.
- gotcha While `crcmod-plus` is a drop-in replacement for `crcmod` 1.7, it is a distinct package. Users should explicitly install `crcmod-plus` for the modernized, Python 3-compatible version, as the original `crcmod` on PyPI is old and unmaintained.
- gotcha The `crcmod.predefined` module, which contains standard CRC algorithms, needs to be explicitly imported (e.g., `import crcmod.predefined`). Symbols like `mkPredefinedCrcFun` are not directly available under `import crcmod`.
Install
-
pip install crcmod-plus
Imports
- mkCrcFun
import crcmod crc_func = crcmod.mkCrcFun(...)
- Crc
import crcmod crc_obj = crcmod.Crc(...)
- mkPredefinedCrcFun
import crcmod.predefined predefined_crc_func = crcmod.predefined.mkPredefinedCrcFun('crc-32') - PredefinedCrc
import crcmod.predefined predefined_crc_obj = crcmod.predefined.PredefinedCrc('crc-32')
Quickstart
import crcmod
import crcmod.predefined
# --- Custom CRC-32 calculation ---
# Create a CRC function using a custom polynomial (e.g., CRC-32-ISO-HDLC)
crc32_func_custom = crcmod.mkCrcFun(0x104C11DB7, initCrc=0, xorOut=0xFFFFFFFF)
data_bytes = b"123456789"
crc_value_custom = crc32_func_custom(data_bytes)
print(f"Custom CRC-32 for '123456789': {hex(crc_value_custom)}")
# Alternatively, use the Crc class for a stateful calculator
crc32_obj_custom = crcmod.Crc(0x104C11DB7, initCrc=0, xorOut=0xFFFFFFFF)
crc32_obj_custom.update(b"123")
crc32_obj_custom.update(b"456789")
print(f"Custom CRC-32 (Crc class) for '123456789': {hex(crc32_obj_custom.crcValue)}")
# --- Predefined CRC-16 (XMODEM) calculation ---
# Create a CRC function using a predefined algorithm name
crc16_func_xmodem = crcmod.predefined.mkCrcFun('xmodem')
crc_value_xmodem = crc16_func_xmodem(data_bytes)
print(f"Predefined CRC-16 (XMODEM) for '123456789': {hex(crc_value_xmodem)}")
# Alternatively, use the PredefinedCrc class
crc16_obj_xmodem = crcmod.predefined.PredefinedCrc('xmodem')
crc16_obj_xmodem.update(data_bytes)
print(f"Predefined CRC-16 (XMODEM, PredefinedCrc class) for '123456789': {hex(crc16_obj_xmodem.crcValue)}")