crccheck package

Submodules

crccheck.base module

Base class for CRC and checksum classes.

License:

Copyright (C) 2015-2016 by Martin Scharrer <martin@scharrer-online.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
crccheck.base.reflectbitorder(width, value)[source]

Reflects the bit order of the given value according to the given bit width.

Parameters:
  • width (int) – bitwidth
  • value (int) – value to reflect
exception crccheck.base.CrccheckError[source]

Bases: Exception

General checksum error exception

class crccheck.base.CrccheckBase(initvalue=None)[source]

Bases: object

Abstract base class for checksumming classes.

Parameters:initvalue (int) – Initial value. If None then the default value for the class is used.
reset(value=None)[source]

Reset instance.

Resets the instance state to the initial value. This is not required for a just created instance.

Parameters:value (int) – Set internal value. If None then the default initial value for the class is used.
Returns:self
process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
final()[source]

Return final check value. The internal state is not modified by this so further data can be processed afterwards.

Returns:final value
Return type:int
finalhex(byteorder='big')[source]

Return final checksum value as hexadecimal string (without leading “0x”). The hex value is zero padded to bitwidth/8. The internal state is not modified by this so further data can be processed afterwards.

Returns:final value as hex string without leading ‘0x’.
Return type:str
finalbytes(byteorder='big')[source]

Return final checksum value as bytes. The internal state is not modified by this so further data can be processed afterwards.

Returns:final value as bytes
Return type:bytes
value()[source]

Returns current intermediate value. Note that in general final() must be used to get the a final value.

Returns:current value
Return type:int
classmethod calc(data, initvalue=None, **kwargs)[source]

Fully calculate CRC/checksum over given data.

Parameters:
  • data (bytes, bytearray or list of ints [0-255]) – input data to process.
  • initvalue (int) – Initial value. If None then the default value for the class is used.
Returns:

final value

Return type:

int

classmethod calchex(data, initvalue=None, byteorder='big', **kwargs)[source]

Fully calculate checksum over given data. Return result as hex string.

Parameters:
  • data (bytes, bytearray or list of ints [0-255]) – input data to process.
  • initvalue (int) – Initial value. If None then the default value for the class is used.
  • byteorder ('big' or 'little') – order (endianness) of returned bytes.
Returns:

final value as hex string without leading ‘0x’.

Return type:

str

classmethod calcbytes(data, initvalue=None, byteorder='big', **kwargs)[source]

Fully calculate checksum over given data. Return result as bytearray.

Parameters:
  • data (bytes, bytearray or list of ints [0-255]) – input data to process.
  • initvalue (int) – Initial value. If None then the default value for the class is used.
  • byteorder ('big' or 'little') – order (endianness) of returned bytes.
Returns:

final value as bytes

Return type:

bytes

classmethod selftest(data=None, expectedresult=None, **kwargs)[source]

Selftest method for automated tests.

Parameters:
  • data (bytes, bytearray or list of int [0-255]) – data to process
  • expectedresult (int) – expected result
Raises:

CrccheckError – if result is not as expected

crccheck.checksum module

Classes to calculated additive and XOR checksums.

License:

Copyright (C) 2015-2016 by Martin Scharrer <martin@scharrer-online.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
class crccheck.checksum.ChecksumBase(initvalue=0, byteorder='big')[source]

Bases: crccheck.base.CrccheckBase

Base class for all checksum classes.

Parameters:
  • initvalue (int) – Initial value. If None then the default value for the class is used.
  • byteorder ('big' or 'little') – byte order (endianness) used when reading the input bytes.
process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
classmethod selftest(data=None, expectedresult=None, byteorder='big')[source]

Selftest method for automated tests.

Parameters:
  • data (bytes, bytearray or list of int [0-255]) – data to process
  • expectedresult (int) – expected result
  • byteorder ('big' or 'little') – byte order (endianness) used when reading the input bytes.
Raises:

CrccheckError – if result is not as expected

class crccheck.checksum.Checksum32(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumBase

32-bit checksum.

Calculates 32-bit checksum by adding the input bytes in groups of four. Input data length must be a multiple of four, otherwise the last bytes are not used.

class crccheck.checksum.Checksum16(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumBase

16-bit checksum.

Calculates 16-bit checksum by adding the input bytes in groups of two. Input data length must be a multiple of two, otherwise the last byte is not used.

class crccheck.checksum.Checksum8(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumBase

8-bit checksum.

Calculates 8-bit checksum by adding the input bytes.

class crccheck.checksum.ChecksumXorBase(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumBase

Base class for all XOR checksum classes.

process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
class crccheck.checksum.ChecksumXor32(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumXorBase

32-bit XOR checksum.

Calculates 32-bit checksum by XOR-ing the input bytes in groups of four. Input data length must be a multiple of four, otherwise the last bytes are not used.

class crccheck.checksum.ChecksumXor16(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumXorBase

16-bit XOR checksum.

Calculates 16-bit checksum by XOR-ing the input bytes in groups of two. Input data length must be a multiple of two, otherwise the last byte is not used.

class crccheck.checksum.ChecksumXor8(initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumXorBase

8-bit XOR checksum.

Calculates 8-bit checksum by XOR-ing the input bytes.

class crccheck.checksum.Checksum(width, initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumBase

General additive checksum.

Parameters:
  • width (int) – bit width of checksum. Must be positive and a multiple of 8.
  • initvalue (int) – Initial value. If None then the default value for the class is used.
  • byteorder ('big' or 'little') – byte order (endianness) used when reading the input bytes.
class crccheck.checksum.ChecksumXor(width, initvalue=0, byteorder='big')[source]

Bases: crccheck.checksum.ChecksumXorBase

General XOR checksum.

Parameters:
  • width (int) – bit width of checksum. Must be positive and a multiple of 8.
  • initvalue (int) – Initial value. If None then the default value for the class is used.
  • byteorder ('big' or 'little') – byte order (endianness) used when reading the input bytes.

crccheck.crc module

Classes to calculate CRCs (Cyclic Redundancy Check).

License:

Copyright (C) 2015-2016 by Martin Scharrer <martin@scharrer-online.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
class crccheck.crc.CrcBase(initvalue=None)[source]

Bases: crccheck.base.CrccheckBase

Abstract base class for all Cyclic Redundancy Checks (CRC) checksums

process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
final()[source]

Return final CRC value.

Returns:final CRC value
Return type:int
class crccheck.crc.Crc(width, poly, initvalue=0, reflect_input=False, reflect_output=False, xor_output=0, check_result=0)[source]

Bases: crccheck.crc.CrcBase

Creates a new general (user-defined) CRC calculator instance.

Parameters:
  • width (int) – bit width of CRC.
  • poly (int) – polynomial of CRC with the top bit omitted.
  • initvalue (int) – initial value of internal running CRC value. Usually either 0 or (1<<width)-1, i.e. “all-1s”.
  • reflect_input (bool) – If true the bit order of the input bytes are reflected first. This is to calculate the CRC like least-significant bit first systems will do it.
  • reflect_output (bool) – If true the bit order of the calculation result will be reflected before the XOR output stage.
  • xor_output (int) – The result is bit-wise XOR-ed with this value. Usually 0 (value stays the same) or (1<<width)-1, i.e. “all-1s” (invert value).
  • check_result (int) – The expected result for the check input “123456789” (= [0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39]). This value is used for the selftest() method to verify proper operation.
class crccheck.crc.Crc8(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-8. Has optimised code for 8-bit CRCs and is used as base class for all other CRC with this width.

process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
class crccheck.crc.Crc16(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-16. Has optimised code for 16-bit CRCs and is used as base class for all other CRC with this width.

process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
class crccheck.crc.Crc32(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-32. Has optimised code for 32-bit CRCs and is used as base class for all other CRC with this width.

process(data)[source]

Process given data.

Parameters:data (bytes, bytearray or list of ints [0-255]) – input data to process.
Returns:self
class crccheck.crc.Crc3Rohc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-3/ROHC

class crccheck.crc.Crc4Itu(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-4/ITU

class crccheck.crc.Crc5Epc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-5/EPC

class crccheck.crc.Crc5Itu(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-5/ITU

class crccheck.crc.Crc5Usb(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-5/USB

class crccheck.crc.Crc6Cdma2000A(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-6/CDMA2000-A

class crccheck.crc.Crc6Cdma2000B(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-6/CDMA2000-B

class crccheck.crc.Crc6Darc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-6/DARC

class crccheck.crc.Crc6Itu(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-6/ITU

class crccheck.crc.Crc7(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-7

class crccheck.crc.Crc7Rohc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-7/ROHC

class crccheck.crc.Crc8Cdma2000(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/CDMA2000

class crccheck.crc.Crc8Darc(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/DARC

class crccheck.crc.Crc8DvbS2(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/DVB-S2

class crccheck.crc.Crc8Ebu(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/EBU

class crccheck.crc.Crc8ICode(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/I-CODE

class crccheck.crc.Crc8Itu(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/ITU

class crccheck.crc.Crc8Maxim(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/MAXIM

class crccheck.crc.Crc8Rohc(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/ROHC

class crccheck.crc.Crc8Wcdma(initvalue=None)[source]

Bases: crccheck.crc.Crc8

CRC-8/WCDMA

class crccheck.crc.Crc10(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-10

class crccheck.crc.Crc10Cdma2000(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-10/CDMA2000

class crccheck.crc.Crc11(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-11

class crccheck.crc.Crc123Gpp(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-12/3GPP

class crccheck.crc.Crc12Cdma2000(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-12/CDMA2000

class crccheck.crc.Crc12Dect(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-12/DECT

class crccheck.crc.Crc13Bbc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-13/BBC

class crccheck.crc.Crc14Darc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-14/DARC

class crccheck.crc.Crc15(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-15

class crccheck.crc.Crc15Mpt1327(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-15/MPT1327

class crccheck.crc.CrcArc(initvalue=None)[source]

Bases: crccheck.crc.Crc16

ARC

class crccheck.crc.Crc16AugCcitt(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/AUG-CCITT

class crccheck.crc.Crc16Buypass(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/BUYPASS

class crccheck.crc.Crc16CcittFalse(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/CCITT-FALSE

class crccheck.crc.Crc16Cdma2000(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/CDMA2000

class crccheck.crc.Crc16Dds110(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/DDS-110

class crccheck.crc.Crc16DectR(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/DECT-R

class crccheck.crc.Crc16DectX(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/DECT-X

class crccheck.crc.Crc16Dnp(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/DNP

class crccheck.crc.Crc16En13757(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/EN-13757

class crccheck.crc.Crc16Genibus(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/GENIBUS

class crccheck.crc.Crc16Maxim(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/MAXIM

class crccheck.crc.Crcc16Mcrf4xx(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/MCRF4XX

class crccheck.crc.Crc16Riello(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/RIELLO

class crccheck.crc.Crc16T10Dif(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/T10-DIF

class crccheck.crc.Crc16Teledisk(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/TELEDISK

class crccheck.crc.Crc16Tms37157(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/TMS37157

class crccheck.crc.Crc16Usb(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-16/USB

class crccheck.crc.CrcA(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC-A

class crccheck.crc.Crc16Ccitt(initvalue=None)[source]

Bases: crccheck.crc.Crc16

CRC16 CCITT, aka KERMIT

crccheck.crc.CrcKermit

alias of Crc16Ccitt

class crccheck.crc.CrcModbus(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

MODBUS

class crccheck.crc.CrcX25(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

X-25

class crccheck.crc.CrcXmodem(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

XMODEM

class crccheck.crc.Crc24(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-24

class crccheck.crc.Crc24FlexrayA(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-24/FLEXRAY-A

class crccheck.crc.Crc24FlexrayB(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-24/FLEXRAY-B

class crccheck.crc.Crc31Philips(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-31/PHILIPS

class crccheck.crc.Crc32Bzip2(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32/BZIP2

class crccheck.crc.Crc32c(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32C

class crccheck.crc.Crc32d(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32D

class crccheck.crc.Crc32Mpeg2(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32/MPEG-2

class crccheck.crc.Crc32Posix(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32/POSIX

class crccheck.crc.Crc32q(initvalue=None)[source]

Bases: crccheck.crc.Crc32

CRC-32Q

class crccheck.crc.CrcJamcrc(initvalue=None)[source]

Bases: crccheck.crc.Crc32

JAMCRC

class crccheck.crc.CrcXfer(initvalue=None)[source]

Bases: crccheck.crc.Crc32

XFER

class crccheck.crc.Crc40Gsm(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-40/GSM

class crccheck.crc.Crc64(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-64

class crccheck.crc.Crc64We(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-64/WE

class crccheck.crc.Crc64Xz(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-64/XZ

class crccheck.crc.Crc82Darc(initvalue=None)[source]

Bases: crccheck.crc.CrcBase

CRC-82/DARC

Module contents

Classes to calculate CRCs and checksums from binary data

The crccheck.crc module implements all CRCs listed in the Catalogue of parametrised CRC algorithms:

CRC-3/ROHC, CRC-4/ITU, CRC-5/EPC, CRC-5/ITU, CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/ITU, CRC-7, CRC-7/ROHC, CRC-8, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/EBU, CRC-8/I-CODE, CRC-8/ITU, CRC-8/MAXIM, CRC-8/ROHC, CRC-8/WCDMA, CRC-10, CRC-10/CDMA2000, CRC-11, CRC-12/3GPP, CRC-12/CDMA2000, CRC-12/DECT, CRC-13/BBC, CRC-14/DARC, CRC-15, CRC-15/MPT1327, CRC-16, ARC, CRC-16/AUG-CCITT, CRC-16/BUYPASS, CRC-16/CCITT-FALSE, CRC-16/CDMA2000, CRC-16/DDS-110, CRC-16/DECT-R, CRC-16/DECT-X, CRC-16/DNP, CRC-16/EN-13757, CRC-16/GENIBUS, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/RIELLO, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/USB, CRC-A, CRC-16 CCITT, KERMIT, MODBUS, X-25, XMODEM, CRC-24, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-31/PHILIPS, CRC-32, CRC-32/BZIP2, CRC-32C, CRC-32D, CRC-32/MPEG-2, CRC-32/POSIX, CRC-32Q, JAMCRC, XFER, CRC-40/GSM, CRC-64, CRC-64/WE, CRC-64/XZ, CRC-82/DARC.

For the class names simply remove all dashes and slashes from the above names and apply CamelCase, e.g. “CRC-32/MPEG-2” is implemented by Crc32Mpeg2. Other CRC can be calculated by using the general class crccheck.crc.Crc by providing all required CRC parameters.

The crccheck.checksum module implements additive and XOR checksums with 8, 16 and 32 bit: Checksum8, Checksum16, Checksum32 and ChecksumXor8, ChecksumXor16, ChecksumXor32.

Usage example:

from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32

# Quick calculation
data = bytearray.fromhex("DEADBEEF")
crc = Crc32.calc(data)
checksum = Checksum32.calc(data)

# Procsss multiple data buffers
data1 = b"Binary string"  # or use .encode(..) on normal sring - Python 3 only
data2 = bytes.fromhex("1234567890")  # Python 3 only, use bytearray for older versions
data3 = (0x0, 255, 12, 99)  # Iterable which returns ints in byte range (0..255)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])
crcbytes = crcinst.finalbytes()
crchex = crcinst.finalhex()
crcint = crcinst.final()

License:

Copyright (C) 2015-2016 by Martin Scharrer <martin@scharrer-online.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.