AnyCRC Library
anycrc is a Python library providing extremely fast CRC (Cyclic Redundancy Check) calculations. It leverages hardware acceleration on Intel (SSE4.2, AVX512) and Arm (ARMv8-CRC) processors for significant speedups, claiming up to 10x faster than previous versions. The current version is 2.0.0, maintaining an active development pace with notable updates for performance and Python version compatibility.
Common errors
-
AttributeError: module 'anycrc.crc' has no attribute 'crc32_mpeg2'
cause Attempting to access a CRC algorithm directly from the `anycrc` module or as a static method, when it's an attribute of the `crc` submodule and requires instantiation.fixEnsure you are importing `crc` as `from anycrc import crc` and then instantiating the specific CRC algorithm, e.g., `c = crc.crc32_mpeg2()`. -
TypeError: calc() missing 1 required positional argument: 'data'
cause This error occurs if you try to call the `calc()` method on the CRC algorithm class itself (e.g., `crc.crc32_mpeg2.calc(data)`) instead of on an instantiated object of that class.fixFirst create an instance of the CRC algorithm: `c = crc.crc32_mpeg2()`, then call `calc()` on that instance: `checksum = c.calc(data)`. -
TypeError: argument must be a bytes-like object, not str
cause The `calc()` method expects binary data (bytes or bytearray) for CRC calculation, but a string was provided.fixConvert your string to bytes using `.encode()` or prefix it with `b` (e.g., `b"my string"`) before passing it to `calc()`.
Warnings
- breaking Version 2.0.0 switched the underlying CRC implementation from `crcany` to a custom library and introduced significant API changes due to a new object caching mechanism (introduced in 1.3.0). Direct calls to global functions like `anycrc.crc32()` are no longer supported.
- deprecated Python 3.7 support was officially dropped in version 1.3.5. Users running Python 3.7 will not be able to install or use anycrc versions 1.3.5 or newer.
- gotcha While anycrc provides significant speedups through hardware acceleration on compatible Intel and Arm CPUs, performance will degrade to a software implementation if the necessary CPU features (e.g., SSE4.2, AVX512 for Intel; ARMv8-CRC for Arm) are not present.
Install
-
pip install anycrc
Imports
- crc
from anycrc import crc
Quickstart
from anycrc import crc
# Calculate CRC32 MPEG2
c32 = crc.crc32_mpeg2()
checksum_mpeg2 = c32.calc(b"Hello, World!")
print(f"CRC32 MPEG2 of 'Hello, World!': {checksum_mpeg2:08x}")
# Calculate CRC16 CCITT
c16 = crc.crc16_ccitt()
checksum_ccitt = c16.calc(b"Another string")
print(f"CRC16 CCITT of 'Another string': {checksum_ccitt:04x}")