CRC32C Checksum Library
The `crc32c` Python package provides an implementation of the CRC32C checksum algorithm, intelligently leveraging hardware acceleration (Intel SSE 4.2, ARMv8 crc32 instructions) when available, and falling back to an optimized software implementation otherwise. As of version 2.8, it offers robust data integrity verification across various platforms. The library aims for regular updates.
Warnings
- deprecated The `crc32c.crc32` function is deprecated and will be removed in future versions. Users should switch to `crc32c.crc32c` for all checksum calculations.
- gotcha The `auto` value for the `CRC32C_SW_MODE` environment variable, which controls software fallback behavior, is planned for discontinuation. It's recommended to explicitly use `force` or `none` if specific fallback control is needed.
- breaking In versions up to 2.6, an `ImportError` was raised if no hardware support was found and software fallback was not enabled. As of version 2.7, this behavior has changed: `CRC32C_SW_MODE='none'` will now issue a `RuntimeWarning` on import and a `RuntimeError` on execution if hardware support is missing.
- gotcha The project uses specific `gcc`/`clang` compiler extensions. If installing from source (e.g., when no pre-built wheel is available for your platform/Python version), older compiler versions might fail to build the package.
Install
-
pip install crc32c
Imports
- crc32c
import crc32c
- crc32c
crc32c.crc32c(data)
- CRC32CHash
from crc32c import CRC32CHash
Quickstart
import crc32c
# Calculate CRC32C for a byte string
data = b"hello world"
checksum = crc32c.crc32c(data)
print(f"CRC32C checksum of '{data.decode()}': {checksum}")
# Incremental calculation (similar to binascii.crc32 with 'value' parameter)
crc_part1 = crc32c.crc32c(b"hello")
crc_total = crc32c.crc32c(b" world", value=crc_part1)
print(f"Incremental CRC32C: {crc_total}")
# Using the hash-like object
hash_obj = crc32c.CRC32CHash()
hash_obj.update(b"hello")
hash_obj.update(b" world")
print(f"CRC32CHash object checksum: {hash_obj.checksum}")
# Check if hardware acceleration is in use
print(f"Hardware acceleration in use: {crc32c.hardware_based}")