CRCmod-plus

2.3.1 · active · verified Mon Apr 13

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

Install

Imports

Quickstart

This quickstart demonstrates how to calculate CRCs using both custom polynomials via `crcmod.mkCrcFun` and `crcmod.Crc` class, and predefined algorithms via `crcmod.predefined.mkCrcFun` and `crcmod.predefined.PredefinedCrc` class.

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)}")

view raw JSON →