Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nicoretti/crc
Pure Python CRC library
https://github.com/nicoretti/crc
crc crc-calculation crc16 crc32 crc64 crc8 hacktoberfest python
Last synced: 3 days ago
JSON representation
Pure Python CRC library
- Host: GitHub
- URL: https://github.com/nicoretti/crc
- Owner: Nicoretti
- License: bsd-2-clause
- Created: 2016-03-21T19:33:31.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2025-01-01T12:49:12.000Z (17 days ago)
- Last Synced: 2025-01-07T23:08:49.937Z (10 days ago)
- Topics: crc, crc-calculation, crc16, crc32, crc64, crc8, hacktoberfest, python
- Language: Python
- Homepage: https://nicoretti.github.io/crc/
- Size: 2.73 MB
- Stars: 55
- Watchers: 6
- Forks: 13
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
CRC
Calculate CRC checksums, verify CRC checksum, predefined CRC configurations, custom CRC configurations
---
* Documentation: [https://nicoretti.github.io/crc](https://nicoretti.github.io/crc)
* Source Code: [https://github.com/Nicoretti/crc](https://github.com/Nicoretti/crc)
---## Available CRC Configurations
For convenience various frequently used crc configurations ship with the library out of the box.| CRC8 | CRC16 | CRC32 | CRC64 |
|---------------|----------|---------|-------|
| CCITT | XMODEM | CRC32 | CRC64 |
| AUTOSAR | GSM | AUTOSAR | |
| SAEJ1850 | PROFIBUS | BZIP2 | |
| SAEJ1850_ZERO | MODBUS | POSIX | |
| BLUETOOTH | IBM-3740 | | |
| MAXIM-DOW | KERMIT | | |If you find yourself in the position, where having a new configuration available out of the
box would be desirable, feel free to create a [PR](https://github.com/Nicoretti/crc/pulls) or file an [issue](https://github.com/Nicoretti/crc/issues).## Custom Configurations
If you want to create a custom configuration, you should have the following information available:
🗒 Note:
This library currently only supports bit widths of full bytes 8, 16, 24, 32, ...
* **width**
* **polynom**
* **init value**
* **final xor value**
* **reversed input**
* **reversed output**In case you only have a name of a specific crc configuration/algorithm and you are unsure what are the specific parameters
of it, a look into this [crc-catalogue](http://reveng.sourceforge.net/crc-catalogue/all.htm) might help.## Requirements
* [\>= Python 3.8](https://www.python.org)## Installation
```shell
pip install crc
```## Examples
### Create a Calculator
#### Pre defined configuration
```python
from crc import Calculator, Crc8calculator = Calculator(Crc8.CCITT)
```
#### Custom configuration```python
from crc import Calculator, Configurationconfig = Configuration(
width=8,
polynomial=0x07,
init_value=0x00,
final_xor_value=0x00,
reverse_input=False,
reverse_output=False,
)calculator = Calculator(config)
```### Calculate a checksum
#### Standard
```python
from crc import Calculator, Crc8expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT)assert expected == calculator.checksum(data)
```#### Optimized for speed
```python
from crc import Calculator, Crc8expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT, optimized=True)assert expected == calculator.checksum(data)
```### Verify a checksum
#### Standard
```python
from crc import Calculator, Crc8expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT)assert calculator.verify(data, expected)
```#### Optimized for speed
```python
from crc import Calculator, Crc8expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT, optimized=True)assert calculator.verify(data, expected)
```### Calculate a checksum with raw registers
#### Register
```python
from crc import Crc8, Registerexpected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
register = Register(Crc8.CCITT)register.init()
register.update(data)
assert expected == register.digest()
```
#### TableBasedRegister```python
from crc import Crc8, TableBasedRegisterexpected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
register = TableBasedRegister(Crc8.CCITT)register.init()
register.update(data)
assert expected == register.digest()
```References & Resources
-----------------------
* [A Painless guide to crc error detection algorithms](http://www.zlib.net/crc_v3.txt)
* [CRC-Catalogue](http://reveng.sourceforge.net/crc-catalogue/all.htm)