crcengine

raw JSON →
0.4.0.post1 verified Sat May 09 auth: no python

A library for CRC calculation and code generation in Python. Current version 0.4.0.post1. Supports many CRC algorithms and can generate CRC code. Release cadence is sporadic.

pip install crcengine
error AttributeError: module 'crcengine' has no attribute 'CrcParams'
cause Using crcengine version <0.4.0 which does not have CrcParams.
fix
Upgrade crcengine: pip install --upgrade crcengine
error TypeError: new() got an unexpected keyword argument 'polynomial'
cause Passing separate polynomial/seed arguments is deprecated in v0.4.0+ and removed in some versions? Actually it's deprecated but still works in 0.4.0. Might be removed later.
fix
Use CrcParams object: from crcengine import CrcParams; crcengine.new(CrcParams(...))
deprecated Passing separate polynomial and seed arguments to crcengine.new() is deprecated since v0.4.0. Use CrcParams instead.
fix Use from crcengine import CrcParams; params = CrcParams(...); crc = crcengine.new(params)
gotcha CRC polynomials under 8 bits wide caused an invalid shift and Exception before v0.4.0. Fixed in v0.4.0.
fix Upgrade to v0.4.0+ or avoid polynomials <8 bits width.
gotcha Function crcengine.new() returns a callable that takes bytes, not a class instance. Do not attempt to create instances.
fix Call the returned function directly: crc = crcengine.new('crc32'); crc(b'data')

Compute CRC using predefined or custom parameters.

import crcengine

# Predefined algorithms available
crc = crcengine.new('crc32c')
data = b'hello world'
checksum = crc(data)
print(hex(checksum))  # e.g. 0x...

# Using CrcParams for custom CRC (v0.4.0+)
from crcengine import CrcParams
params = CrcParams(width=16, polynomial=0x8005, seed=0x0000, reflect_input=False, reflect_output=False, xor_out=0x0000)
crc16 = crcengine.new(params)
print(hex(crc16(b'test')))