AnyCRC Library

raw JSON →
2.0.0 verified Fri Apr 17 auth: no python

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.

pip install anycrc
error 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.
fix
Ensure you are importing crc as from anycrc import crc and then instantiating the specific CRC algorithm, e.g., c = crc.crc32_mpeg2().
error 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.
fix
First create an instance of the CRC algorithm: c = crc.crc32_mpeg2(), then call calc() on that instance: checksum = c.calc(data).
error 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.
fix
Convert your string to bytes using .encode() or prefix it with b (e.g., b"my string") before passing it to calc().
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.
fix Update your code to instantiate CRC algorithms as objects (e.g., `c = crc.crc32_mpeg2()`) and then call the `calc()` method on the instance (e.g., `c.calc(data)`). Refer to the official documentation or GitHub README for updated API usage.
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.
fix Upgrade your Python environment to version 3.8 or newer to use current `anycrc` releases.
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.
fix No action is strictly required as the library gracefully falls back. However, for maximum performance, ensure the application runs on hardware with the specified CRC acceleration instructions. No user-level configuration is available to force hardware acceleration.

Initialize a CRC algorithm object and then use its `calc()` method to compute the checksum for byte-like data.

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