+
Skip to content

tipabu/anycrc

 
 

Repository files navigation

This is a Cython module with bindings to the crcany library. It supports calculating CRC hashes of arbitary sizes as well as updating a CRC hash over time.

Installation

pip install anycrc

Usage

Use an existing model:

>>> import anycrc
>>> crc32 = anycrc.Model('CRC32-MPEG-2')
>>> crc32.calc(b'Hello World!')
2498069329

Create a CRC with specific parameters:

>>> crc32 = anycrc.CRC(width=32, poly=0x04c11db7, init=0xffffffff, refin=False, refout=False, xorout=0x00000000)
>>> crc32.calc('Hello World!')
2498069329

Read the data in chunks:

>>> value = crc32.calc(b'Hello ')
>>> crc32.calc(b'World!', value)
2498069329

The length of the data can be specified in bits by calling calc_bits and passing a bitarray object:

>>> from bitarray import bitarray
>>> bits = bitarray()
>>> bits.frombytes(b'Hello World!')
>>> value = crc32.calc_bits(bits[:50])
>>> crc32.calc_bits(bits[50:], value)
2498069329

To use bit lengths with reflected CRCs, create a little endian bitarray object: bitarray(endian='little')

To combine two CRCs, provide the CRC values along with the length of the second CRC's message in bytes:

>>> value = crc32.calc(b'Hello ')
>>> value2 = crc32.calc(b'World!')
>>> crc32.combine(value, value2, len(b'World!'))
2498069329

There is also a combine_bits method where the length argument is expected to be in bits.

For a list of pre-built models, check models.py. To get a list of the models at any time, use the following command:

python -m anycrc models

The maximum supported CRC width is 64 bits.

Benchmark

Module Speed (MB/s) Relative Notes
pycrc32 22549.44 x0.11 CRC32 only
crc32c 20990.32 x0.12 CRC32C only
google-crc32c 17046.81 x0.15 CRC32C only
anycrc 2591.08 x1.00
zlib 2028.04 x1.28 CRC32 only
fastcrc 652.01 x3.97
crcmod-plus 647.36 x4.00
crc-ct 570.82 x4.54
libscrc 149.21 x17.37
crcengine 7.44 x348.07
pycrc 3.63 x714.01
crccheck 1.06 x2449.24
crc 0.34 x7583.33

Tested on a 10th generation Intel i7 processor.

About

The fastest Python CRC library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 54.9%
  • C 37.1%
  • Cython 8.0%
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载