CRC
Calculate CRC checksums, verify CRC checksum, predefined CRC configurations, custom CRC configurations
- Documentation: https://nicoretti.github.io/crc
- Source Code: https://github.com/Nicoretti/crc
Available CRC Configurations
The library includes a variety of common CRC configurations for convenience. To explore the full range of available CRC configurations, please checkout the configurations section of the documentation. If you need a new configuration to be readily available, consider submitting a PR or raising an issue.
Custom Configurations
If you want to create a custom configuration, you should have the following information available:
- 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 might help.
Note
This library currently only supports bit widths of full bytes 8, 16, 24, 32, ...
Requirements
Installation
pip install crc
Examples
Create a Calculator
from crc import Calculator, Crc8
calculator = Calculator(Crc8.CCITT)
from crc import Calculator, Configuration
config = Configuration(
width=8,
poly=0x07,
init_value=0x00,
final_xor_value=0x00,
reverse_input=False,
reverse_output=False,
)
calculator = Calculator(config)
Calculate a checksum
from crc import Calculator, Crc8
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT)
assert expected == calculator.checksum(data)
from crc import Calculator, Crc8
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT, optimized=True)
assert expected == calculator.checksum(data)
Verify a checksum
from crc import Calculator, Crc8
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT)
assert calculator.verify(data, expected)
from crc import Calculator, Crc8
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
calculator = Calculator(Crc8.CCITT, optimized=True)
assert calculator.verify(data, expected)
Supported data types
from crc import Calculator, Crc8
expected = 0x20
data = 97
calculator = Calculator(Crc8.CCITT, optimized=True)
assert calculator.checksum(data) == expected
from crc import Calculator, Crc8
expected = 0xF4
data = b"123456789"
calculator = Calculator(Crc8.CCITT, optimized=True)
assert calculator.checksum(data) == expected
from crc import Calculator, Crc8
expected = 0xF4
data = bytearray(b"123456789")
calculator = Calculator(Crc8.CCITT, optimized=True)
assert calculator.checksum(data) == expected
from crc import Calculator, Crc8
expected = 0xF4
calculator = Calculator(Crc8.CCITT, optimized=True)
with open("afile.txt", "rb") as f:
assert calculator.checksum(f) == expected
import io
from crc import Calculator, Crc8
expected = 0xF4
data = io.ByteIo(b"123456789")
calculator = Calculator(Crc8.CCITT, optimized=True)
assert calculator.checksum(data) == expected
from crc import Calculator, Crc8
expected = 0xF4
calculator = Calculator(Crc8.CCITT, optimized=True)
data = (data for data in [b"12", b"34", b"56", b"78", b"9"])
assert calculator.checksum(data) == expected
from crc import Calculator, Crc8
class ByteConvertible:
def __init__(self, data):
self._data = data
def __bytes__(self):
return self._data.encode("utf-8")
expected = 0xF4
calculator = Calculator(Crc8.CCITT, optimized=True)
data = ByteConvertible("123456789")
assert calculator.checksum(bytes(data)) == expected
Calculate a checksum with raw registers
from crc import Crc8, Register
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
register = Register(Crc8.CCITT)
register.init()
register.update(data)
assert expected == register.digest()
from crc import Crc8, TableBasedRegister
expected = 0xBC
data = bytes([0, 1, 2, 3, 4, 5])
register = TableBasedRegister(Crc8.CCITT)
register.init()
register.update(data)
assert expected == register.digest()