Registry / serialization / crcmod

crcmod

JSON →
library1.7pypypi✓ verified 26d ago

CRCmod is a Python module for calculating Cyclic Redundancy Checks (CRCs). It provides functionality to generate custom CRC algorithms and access a wide range of predefined CRC algorithms, such as CRC-CCITT and CRC-32. The current version is 1.7. The library has a slow release cadence, with updates primarily focused on bug fixes and stability.

pip install crcmod
INSTALL
IMPORT
SIG · CRCMOD
C
crcmod
serializationpythonv1.7
Install
2.5s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.5s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

crcmod
import crcmod
Crc
from crcmod.predefined import Crc
from crcmod import Crc
While `crcmod.Crc` exists, `crcmod.predefined.Crc` is commonly used for standard CRC algorithms.
mkCrcFun
from crcmod import mkCrcFun
crcmod.mkCrcFun()
`mkCrcFun` is a function that *returns* a CRC calculation function, it's not a method to be called directly on the module.

This quickstart demonstrates how to calculate CRCs using both predefined algorithms and custom parameters. It shows both the object-oriented approach (`Crc` objects) and the functional approach (`mkCrcFun` functions) for flexibility.

import crcmod import crcmod.predefined # 1. Using a predefined CRC algorithm (object-oriented approach) # Get a CRC-16-CCITT-FALSE calculator crc16_ccitt = crcmod.predefined.Crc('crc-ccitt-false') data_bytes = b'Hello, world!' crc16_ccitt.update(data_bytes) print(f"CRC-16-CCITT-FALSE for '{data_bytes.decode()}': {hex(crc16_ccitt.crcValue)}") # You can reset and reuse the object crc16_ccitt.reset() crc16_ccitt.update(b'Another test') print(f"CRC-16-CCITT-FALSE for 'Another test': {hex(crc16_ccitt.crcValue)}") # 2. Creating a custom CRC function (functional approach) # Example: CRC-32 (Ethernet) - polynomial 0x104C11DB7, initial 0xFFFFFFFF, xorOut 0xFFFFFFFF, revIn/Out True crc32_func = crcmod.mkCrcFun(0x104C11DB7, initCrc=0xFFFFFFFF, xorOut=0xFFFFFFFF, rev=True) data_bytes_32 = b'123456789' result_32 = crc32_func(data_bytes_32) print(f"CRC-32 for '{data_bytes_32.decode()}': {hex(result_32)}") # 3. Using a predefined CRC algorithm (functional approach) crc32_predef_func = crcmod.predefined.mkCrcFun('crc-32') result_predef_32 = crc32_predef_func(b'123456789') print(f"CRC-32 (predefined func) for '123456789': {hex(result_predef_32)}")
Debug
Known issues
gotchaInput data to CRC functions (e.g., `update`, `mkCrcFun`) MUST be bytes (`bytes`), not strings (`str`). Passing a Python string will result in a `TypeError`.
fix
Always encode strings to bytes before passing them to crcmod functions, e.g., `my_string.encode('utf-8')`.
affects: All versions
gotchaThere are different ways to use crcmod: `crcmod.predefined.Crc` (object-oriented for standard CRCs), `crcmod.mkCrcFun` (functional for custom CRCs), and `crcmod.predefined.mkCrcFun` (functional for standard CRCs). Confusing these can lead to errors.
fix
Use `crcmod.predefined.Crc('crc-name')` for an object to update incrementally with a standard CRC. Use `crcmod.mkCrcFun(...)` or `crcmod.predefined.mkCrcFun('crc-name')` if you need a simple function that calculates the CRC of an entire byte string in one call.
affects: All versions
gotcha`crcmod.predefined.Crc` objects, used for incremental CRC calculations, do not provide a `reset()` method. Attempting to call `reset()` will result in an `AttributeError`.
fix
If you need to restart a CRC calculation from its initial state, you must create a new `crcmod.predefined.Crc` object. For example, instead of `my_crc.reset()`, use `my_crc = crcmod.predefined.Crc('crc-name')` to get a fresh instance.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'crcmod'
The `crcmod` library is not installed in the active Python environment or there is a conflict between Python 2 and Python 3 installations.
fix
Install the library using pip, specifying the Python version if necessary: `pip install crcmod` or `pip3 install crcmod`. Ensure your virtual environment is activated if applicable.
TypeError: Unicode-objects must be encoded before calculating a CRC
In Python 3, `crcmod` functions expect byte strings (e.g., `b'data'`) as input, but a standard Unicode string (e.g., `'data'`) was provided.
fix
Encode the Unicode string to bytes before passing it to the `crcmod` function, for example: `crc_function('your_string'.encode('utf-8'))`.
CommandException: Downloading this composite object requires integrity checking with CRC32c, but your crcmod installation isn't using the module's C extension, so the hash computation will likely throttle download performance.
The C extension for `crcmod` failed to compile during installation because necessary development tools (like a C compiler and Python development headers) were missing, causing `crcmod` to fall back to its slower pure-Python implementation.
fix
Ensure a C compiler (e.g., `gcc`) and Python development headers (e.g., `python-dev` or `python3-devel` depending on your Python version and OS) are installed on your system, then reinstall `crcmod` using `pip install --force-reinstall crcmod`.
ValueError: The only polynomials allowed are those that generate 8, 16, 24, 32, or 64 bit CRCs.
The custom polynomial provided to `crcmod.mkCrcFun` does not correspond to a valid CRC width (8, 16, 24, 32, or 64 bits), or its hexadecimal representation is incorrect for the desired bit length.
fix
Either use a predefined CRC polynomial from `crcmod.predefined` or ensure your custom polynomial correctly represents a generator for an 8, 16, 24, 32, or 64-bit CRC (e.g., an 8-bit polynomial should be between `0x100` and `0x1FF` inclusive).
Upgrade
Version history
1.7latest on PyPI · released Jun 27, 2010
Audit
Dependencies

No dependency data recorded yet.

Agent activity
26 hits · last 30 days
node
24
Resources
crcmod — pip install crcmod · libregistry