1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
This is a Cython module with bindings to the [crcany](https://github.com/madler/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:
```python
>>> import anycrc
>>> crc32 = anycrc.Model('CRC32-MPEG-2')
>>> crc32.calc(b'Hello World!')
2498069329
```
Create a CRC with specific parameters:
```python
>>> 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:
```python
>>> 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](https://github.com/ilanschnell/bitarray) object:
```python
>>> 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:
```python
>>> 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](https://github.com/marzooqy/anycrc/blob/main/src/anycrc/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 |
|---|:-:|:-:|
| anycrc | 2834.54 | x1.00 |
| fastcrc | 656.97 | x4.31 |
| crcmod-plus | 654.04 | x4.33 |
| crc-ct | 646.11 | x4.39 |
| libscrc | 215.91 | x13.13 |
| crcengine | 9.47 | x299.29 |
| pycrc | 7.55 | x375.39 |
| crccheck | 1.48 | x1921.18 |
| crc | 0.42 | x6670.14 |
Tested on a 12th generation Intel i7 processor.
|