A Python package implementing the CRC32C checksum algorithm in hardware and software
npx @tessl/cli install tessl/pypi-crc32c@2.7.0A Python package implementing the CRC32C checksum algorithm in hardware and software. It automatically chooses between hardware-accelerated implementations (using Intel SSE 4.2 CRC32C instructions on x86 CPUs, and crc32* instructions on ARMv8 CPUs) and optimized software fallbacks when hardware support is unavailable.
pip install crc32cimport crc32cimport crc32c
# Basic checksum calculation
checksum = crc32c.crc32c(b'hello world')
print(checksum) # 3381945770
# Incremental checksum calculation
crc = crc32c.crc32c(b'hello')
final_crc = crc32c.crc32c(b' world', value=crc)
print(final_crc) # 3381945770
# Using hash object interface
hasher = crc32c.CRC32CHash()
hasher.update(b'hello')
hasher.update(b' world')
print(hasher.checksum) # 3381945770
print(hasher.digest()) # b'\xc9\x94e\xaa'
print(hasher.hexdigest()) # c99465aaCalculate CRC32C checksum incrementally with configurable GIL release behavior.
def crc32c(data, value=0, gil_release_mode=-1):
"""
Calculate CRC32C checksum incrementally.
Parameters:
- data: Buffer (bytes-like object) to checksum
- value: int, initial checksum value (default: 0)
- gil_release_mode: int, GIL release behavior:
* Negative: Only release GIL when data >= 32KiB (default: -1)
* 0: Never release the GIL
* Positive: Always release the GIL
Returns:
int: The calculated CRC32C checksum
"""Legacy function name for CRC32C calculation. Use crc32c instead as this will be removed in future versions.
def crc32(data, value=0, gil_release_mode=-1):
"""
Calculate CRC32C checksum incrementally (deprecated).
Deprecated: Use crc32c instead. Will be removed in future versions.
Parameters:
- data: Buffer (bytes-like object) to checksum
- value: int, initial checksum value (default: 0)
- gil_release_mode: int, GIL release behavior (same as crc32c)
Returns:
int: The calculated CRC32C checksum
"""Object-oriented interface modeled after Python's hashlib for CRC32C computation.
class CRC32CHash:
"""
Hash object class for CRC32C computation following hashlib interface.
"""
def __init__(self, data=b"", gil_release_mode=-1):
"""
Initialize hash object with optional initial data.
Parameters:
- data: bytes-like object, initial data to hash (default: empty bytes)
- gil_release_mode: int, GIL release behavior for checksum calculations (default: -1)
"""
def update(self, data):
"""
Update hash object with bytes-like object.
Parameters:
- data: bytes-like object to add to hash
Returns:
None
"""
def digest(self):
"""
Return digest as bytes object.
Returns:
bytes: 4-byte big-endian digest
"""
def hexdigest(self):
"""
Return digest as hexadecimal string.
Returns:
str: Hexadecimal representation of digest
"""
def copy(self):
"""
Return copy of hash object.
Returns:
CRC32CHash: Copy of current hash object state
"""
@property
def digest_size(self):
"""
Size of resulting hash in bytes.
Returns:
int: Always 4 for CRC32C
"""
@property
def block_size(self):
"""
Internal block size of hash algorithm in bytes.
Returns:
int: Always 1 for CRC32C
"""
@property
def name(self):
"""
Canonical name of hash algorithm.
Returns:
str: Always "crc32c"
"""
@property
def checksum(self):
"""
Current checksum value (non-standard hashlib extension).
Returns:
int: Current checksum as integer
"""Runtime information about implementation and platform characteristics.
hardware_based: bool
"""
Indicates whether hardware-based implementation is in use.
True if using hardware acceleration, False if using software fallback.
"""
big_endian: int
"""
Indicates platform endianness.
1 if platform is big-endian, 0 if little-endian.
"""Performance testing functionality available as a submodule.
import crc32c.benchmark
def run(size, iterations):
"""
Run benchmark with specified parameters.
Parameters:
- size: int, amount of bytes to checksum
- iterations: int, number of iterations to run
Returns:
tuple[float, int]: (duration_seconds, total_evaluations)
"""
def main():
"""
Command-line benchmark interface with argument parsing.
Returns:
None
"""The benchmark module can be executed directly:
python -m crc32c.benchmark --size 104857600 --iterations 10Package behavior can be controlled via environment variables:
Controls software implementation preference:
auto: Use software fallback if no hardware support (default behavior, will be discontinued)force: Force software implementation regardless of hardware supportnone: Raise RuntimeError if no hardware support foundFor testing purposes:
1: Skip hardware capability detectionfrom typing import Union
from typing_extensions import Buffer
Buffer = Union[bytes, bytearray, memoryview, ...]
"""
Type alias for bytes-like objects that support the buffer protocol.
Includes bytes, bytearray, memoryview, and other objects implementing __buffer__.
"""class RuntimeError(Exception):
"""
Raised when no hardware support is available and CRC32C_SW_MODE is set to "none".
"""
class RuntimeWarning(Warning):
"""
Warning issued when no hardware support is available and CRC32C_SW_MODE is set to "none".
"""
class DeprecationWarning(Warning):
"""
Warning issued when using the deprecated crc32 function.
"""