././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1600722827.4327836 crccheck-1.0/0000777000000000000000000000000000000000000011252 5ustar0000000000000000././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1600722827.4327836 crccheck-1.0/PKG-INFO0000666000000000000000000001301500000000000012347 0ustar0000000000000000Metadata-Version: 1.1 Name: crccheck Version: 1.0 Summary: Calculation library for CRCs and checksums Home-page: https://sourceforge.net/projects/crccheck/ Author: Martin Scharrer Author-email: martin@scharrer-online.de License: GPL v3+ Download-URL: https://sourceforge.net/projects/crccheck/files/ Description: crccheck - 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/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU, CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7, CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A, CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC, CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250, CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY, CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC, CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000, CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757, CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740, CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B, X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5, CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU, CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE, CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD, CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP, CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D, CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX, CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32, CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM, CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, 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() Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+) Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Topic :: Scientific/Engineering Classifier: Topic :: Software Development :: Embedded Systems Classifier: Topic :: Utilities ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/README.rst0000666000000000000000000000767600000000000012761 0ustar0000000000000000crccheck - 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/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU, CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7, CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A, CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC, CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250, CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY, CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC, CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000, CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757, CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740, CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B, X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5, CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU, CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE, CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD, CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP, CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D, CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX, CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32, CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM, CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, 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() ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1600722827.3887978 crccheck-1.0/crccheck/0000777000000000000000000000000000000000000013017 5ustar0000000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/crccheck/__init__.py0000666000000000000000000001173300000000000015135 0ustar0000000000000000""" Classes to calculate CRCs and checksums from binary data ======================================================== The :mod:`crccheck.crc` module implements all CRCs listed in the `Catalogue of parametrised CRC algorithms `_: CRC-3/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU, CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7, CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A, CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC, CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250, CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY, CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC, CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000, CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757, CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740, CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B, X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5, CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU, CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE, CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD, CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP, CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D, CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX, CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32, CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM, CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, 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 :class:`.Crc32Mpeg2`. Other CRC can be calculated by using the general class :class:`crccheck.crc.Crc` by providing all required CRC parameters. The :mod:`crccheck.checksum` module implements additive and XOR checksums with 8, 16 and 32 bit: :class:`.Checksum8`, :class:`.Checksum16`, :class:`.Checksum32` and :class:`.ChecksumXor8`, :class:`.ChecksumXor16`, :class:`.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-2020 by Martin Scharrer 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 . """ from crccheck import crc, checksum ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/crccheck/base.py0000666000000000000000000002066100000000000014310 0ustar0000000000000000""" Base class for CRC and checksum classes. License:: Copyright (C) 2015-2020 by Martin Scharrer 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 . """ import math REFLECT_BIT_ORDER_TABLE = ( 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, ) def reflectbitorder(width, value): """ Reflects the bit order of the given value according to the given bit width. Args: width (int): bitwidth value (int): value to reflect """ binstr = ("0"*width + bin(value)[2:])[-width:] return int(binstr[::-1], 2) class CrccheckError(Exception): """General checksum error exception""" pass class CrccheckBase(object): """ Abstract base class for checksumming classes. Args: initvalue (int): Initial value. If None then the default value for the class is used. """ _initvalue = 0x00 _check_result = None _check_data = None _file_chunksize = 512 _width = 0 def __init__(self, initvalue=None): if initvalue is None: self._value = self._initvalue else: self._value = initvalue def reset(self, value=None): """ Reset instance. Resets the instance state to the initial value. This is not required for a just created instance. Args: value (int): Set internal value. If None then the default initial value for the class is used. Returns: self """ if value is None: self._value = self._initvalue else: self._value = value return self def process(self, data): """ Process given data. Args: data (bytes, bytearray or list of ints [0-255]): input data to process. Returns: self """ raise NotImplementedError def final(self): """Return final check value. The internal state is not modified by this so further data can be processed afterwards. Return: int: final value """ return self._value def finalhex(self, byteorder='big'): """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. Return: str: final value as hex string without leading '0x'. """ asbytes = self.finalbytes(byteorder) try: return asbytes.hex() except AttributeError: return "".join(["{:02x}".format(b) for b in asbytes]) def finalbytes(self, byteorder='big'): """Return final checksum value as bytes. The internal state is not modified by this so further data can be processed afterwards. Return: bytes: final value as bytes """ bytelength = int(math.ceil(self._width / 8.0)) asint = self.final() try: return asint.to_bytes(bytelength, byteorder) except AttributeError: asbytes = bytearray(bytelength) for i in range(0, bytelength): asbytes[i] = asint & 0xFF asint >>= 8 if byteorder == 'big': asbytes.reverse() return asbytes def value(self): """Returns current intermediate value. Note that in general final() must be used to get the a final value. Return: int: current value """ return self._value @classmethod def calc(cls, data, initvalue=None, **kwargs): """ Fully calculate CRC/checksum over given data. Args: 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. Return: int: final value """ inst = cls(initvalue, **kwargs) inst.process(data) return inst.final() @classmethod def calchex(cls, data, initvalue=None, byteorder='big', **kwargs): """Fully calculate checksum over given data. Return result as hex string. Args: 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. Return: str: final value as hex string without leading '0x'. """ inst = cls(initvalue, **kwargs) inst.process(data) return inst.finalhex(byteorder) @classmethod def calcbytes(cls, data, initvalue=None, byteorder='big', **kwargs): """Fully calculate checksum over given data. Return result as bytearray. Args: 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. Return: bytes: final value as bytes """ inst = cls(initvalue, **kwargs) inst.process(data) return inst.finalbytes(byteorder) @classmethod def selftest(cls, data=None, expectedresult=None, **kwargs): """ Selftest method for automated tests. Args: data (bytes, bytearray or list of int [0-255]): data to process expectedresult (int): expected result Raises: CrccheckError: if result is not as expected """ if data is None: data = cls._check_data expectedresult = cls._check_result result = cls.calc(data, **kwargs) if result != expectedresult: raise CrccheckError("{:s}: expected {:s}, got {:s}".format(cls.__name__, hex(expectedresult), hex(result))) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/crccheck/checksum.py0000666000000000000000000001700500000000000015176 0ustar0000000000000000""" Classes to calculated additive and XOR checksums. License:: Copyright (C) 2015-2020 by Martin Scharrer 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 . """ from crccheck.base import CrccheckBase, CrccheckError class ChecksumBase(CrccheckBase): """ Base class for all checksum classes. Args: 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. """ _width = 0 _mask = 0 _check_data = (0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0x55, 0xC2, 0x8C) _check_result_littleendian = None def __init__(self, initvalue=0, byteorder='big'): super(ChecksumBase, self).__init__(initvalue) self._byteorder = byteorder def process(self, data): """ Process given data. Args: data (bytes, bytearray or list of ints [0-255]): input data to process. Returns: self """ dataword = 0 n = 0 bigendian = (self._byteorder == 'big') width = self._width mask = self._mask value = self._value for byte in data: if bigendian: dataword = (dataword << 8) | byte else: dataword |= (byte << n) n += 8 if n == width: value = mask & (value + dataword) dataword = 0 n = 0 self._value = value return self @classmethod def selftest(cls, data=None, expectedresult=None, byteorder='big'): """ Selftest method for automated tests. Args: 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 """ if data is None: data = cls._check_data if expectedresult is None: if byteorder == 'big': expectedresult = cls._check_result else: expectedresult = cls._check_result_littleendian result = cls.calc(data, byteorder=byteorder) if result != expectedresult: raise CrccheckError(hex(result)) class Checksum32(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. """ _width = 32 _mask = 0xFFffFFff _check_result = 0x8903817B _check_result_littleendian = 0x7C810388 class Checksum16(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. """ _width = 16 _mask = 0xFFff _check_result = 0x0A7D _check_result_littleendian = 0x8008 class Checksum8(ChecksumBase): """ 8-bit checksum. Calculates 8-bit checksum by adding the input bytes. """ _width = 8 _mask = 0xFF _check_result = 0x85 _check_result_littleendian = _check_result class ChecksumXorBase(ChecksumBase): """ Base class for all XOR checksum classes. """ def process(self, data): """ Process given data. Args: data (bytes, bytearray or list of ints [0-255]): input data to process. Returns: self """ dataword = 0 n = 0 bigendian = (self._byteorder == 'big') width = self._width mask = self._mask value = self._value for byte in data: if bigendian: dataword = (dataword << 8) | byte else: dataword |= (byte << n) n += 8 if n == width: value = mask & (value ^ dataword) dataword = 0 n = 0 self._value = value return self class ChecksumXor32(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. """ _width = 32 _mask = 0xFFffFFff _check_result = 0x74F87C63 _check_result_littleendian = 0x637CF874 class ChecksumXor16(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. """ _width = 16 _mask = 0xFFff _check_result = 0x089B _check_result_littleendian = 0x9B08 class ChecksumXor8(ChecksumXorBase): """ 8-bit XOR checksum. Calculates 8-bit checksum by XOR-ing the input bytes. """ _width = 8 _mask = 0xFF _check_result = 0x93 _check_result_littleendian = _check_result class Checksum(ChecksumBase): """ General additive checksum. Args: 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. """ _check_result = None _check_result_littleendian = None def __init__(self, width, initvalue=0, byteorder='big'): super(Checksum, self).__init__(initvalue, byteorder) width = int(width) if width <= 0 or width % 8 != 0: raise ValueError("width must be postive and a multiple of 8") self._width = width self._mask = (1 << width) - 1 class ChecksumXor(ChecksumXorBase): """ General XOR checksum. Args: 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. """ _check_result = None _check_result_littleendian = None def __init__(self, width, initvalue=0, byteorder='big'): super(ChecksumXor, self).__init__(initvalue, byteorder) width = int(width) if width <= 0 or width % 8 != 0: raise ValueError("width must be postive and a multiple of 8") self._width = width self._mask = (1 << width) - 1 ALLCHECKSUMCLASSES = ( Checksum8, Checksum16, Checksum32, ChecksumXor8, ChecksumXor16, ChecksumXor32, ) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/crccheck/crc.py0000666000000000000000000015146500000000000014154 0ustar0000000000000000""" Classes to calculate CRCs (Cyclic Redundancy Check). License:: Copyright (C) 2015-2020 by Martin Scharrer 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 . """ from crccheck.base import CrccheckBase, reflectbitorder, REFLECT_BIT_ORDER_TABLE, CrccheckError class CrcBase(CrccheckBase): """Abstract base class for all Cyclic Redundancy Checks (CRC) checksums""" _names = () _width = 0 _poly = 0x00 _initvalue = 0x00 _reflect_input = False _reflect_output = False _xor_output = 0x00 _check_result = None _check_data = bytearray(b"123456789") _residue = None def process(self, data): """ Process given data. Args: data (bytes, bytearray or list of ints [0-255]): input data to process. Returns: self """ crc = self._value highbit = 1 << (self._width - 1) mask = ((highbit - 1) << 1) | 0x1 # done this way to avoid overrun for 64-bit values poly = self._poly shift = self._width - 8 diff8 = -shift if diff8 > 0: # enlarge temporary to fit 8-bit mask = 0xFF crc <<= diff8 shift = 0 highbit = 0x80 poly = self._poly << diff8 reflect = self._reflect_input for byte in data: if reflect: byte = REFLECT_BIT_ORDER_TABLE[byte] crc ^= (byte << shift) for i in range(0, 8): if crc & highbit: crc = (crc << 1) ^ poly else: crc = (crc << 1) crc &= mask if diff8 > 0: crc >>= diff8 self._value = crc return self def final(self): """ Return final CRC value. Return: int: final CRC value """ crc = self._value if self._reflect_output: crc = reflectbitorder(self._width, crc) crc ^= self._xor_output return crc def __eq__(self, other): return self._width == other._width and \ self._poly == other._poly and \ self._initvalue == other._initvalue and \ self._reflect_input == other._reflect_input and \ self._reflect_output == other._reflect_output and \ self._xor_output == other._xor_output def __repr__(self): residue = hex(self._residue) if self._residue is not None else 'None' check_result = hex(self._check_result) if self._check_result is not None else 'None' return ("Crc(width={:d}, poly=0x{:x}, initvalue=0x{:X}, reflect_input={!s:s}, reflect_output={!s:s}, " + "xor_output=0x{:x}, check_result={}, residue={})").format( self._width, self._poly, self._initvalue, self._reflect_input, self._reflect_output, self._xor_output, check_result, residue) def find(classes=None, width=None, poly=None, initvalue=None, reflect_input=None, reflect_output=None, xor_output=None, check_result=None, residue=None): """Find CRC classes which the matching properties. Args: classes (None or list): List of classes to search in. If None the list ALLCRCCLASSES will be used. width (None or int): number of bits of the CRC classes to find poly (None or int): polygon to find initvalue (None or int): initvalue to find reflect_input (None or bool): reflect_input to find reflect_output (None or bool): reflect_output to find xor_output (None or int): xor_output to find check_result (None or int): check_result to find residue (None or int): residue to find Returns: List of CRC classes with the selected properties. Examples: Find all CRC16 classes: $ find(width=16) Find all CRC32 classes with all-1 init value and XOR output: $ find(width=32, initvalue=0xFFFF, xor_output=0xFFFF) """ found = list() if classes is None: classes = ALLCRCCLASSES for cls in classes: if width is not None and width != cls._width: continue if poly is not None and poly != cls._poly: continue if initvalue is not None and initvalue != cls._initvalue: continue if reflect_input is not None and reflect_input != cls._reflect_input: continue if reflect_output is not None and reflect_output != cls._reflect_output: continue if xor_output is not None and xor_output != cls._xor_output: continue if check_result is not None and check_result != cls._check_result: continue if residue is not None and residue != cls._residue: continue found.append(cls) return found def identify(data, crc, width=None, classes=None, one=True): """ Identify the used CRC algorithm which was used to calculate the CRC from some data. This function can be used to identify a suitable CRC class if the exact CRC algorithm/parameters are not known, but a CRC value is known from some data. Note that this function can be quite time consuming on large data, especially if the given width is not known. Args: data (bytes): Data to compare with the `crc`. crc (int): Known CRC of the given `data`. width (int or None): Known bit width of given `crc`. Providing the width will speed up the identification of the CRC algorithm. classes (iterable or None): Listing of classes to check. If None then ALLCRCCLASSES is used. one (bool): If True then only the first found CRC class is retunred. Otherwise a list of all suitable CRC classes. Returns: If `one` is True: CRC class which instances produce the given CRC from the given data. If no CRC class could be found `None` is returned. If `one` is False: List of CRC classes which instances produce the given CRC from the given data. The list may be empty. """ if classes is None: classes = ALLCRCCLASSES if width is not None: classes = (cls for cls in classes if cls._width == width) found = [] for cls in classes: if cls().calc(data) == crc: if one: return cls found.append(cls) if one: return None return found class Crc(CrcBase): """ Creates a new general (user-defined) CRC calculator instance. Arguments: 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< 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 . """ from crccheck.base import CrccheckBase, reflectbitorder, REFLECT_BIT_ORDER_TABLE class CrcBase(CrccheckBase): """Abstract base class for all Cyclic Redundancy Checks (CRC) checksums""" _width = 0 _poly = 0x00 _initvalue = 0x00 _reflect_input = False _reflect_output = False _xor_output = 0x00 _check_result = None _check_data = bytearray(b"123456789") def process(self, data): """ Process given data. Args: data (bytes, bytearray or list of ints [0-255]): input data to process. Returns: self """ crc = self._value highbit = 1 << (self._width - 1) mask = ((highbit - 1) << 1) | 0x1 # done this way to avoid overrun for 64-bit values poly = self._poly shift = self._width - 8 diff8 = -shift if diff8 > 0: # enlarge temporary to fit 8-bit mask = 0xFF crc <<= diff8 shift = 0 highbit = 0x80 poly = self._poly << diff8 reflect = self._reflect_input for byte in data: if reflect: byte = REFLECT_BIT_ORDER_TABLE[byte] crc ^= (byte << shift) for i in range(0, 8): if crc & highbit: crc = (crc << 1) ^ poly else: crc = (crc << 1) crc &= mask if diff8 > 0: crc >>= diff8 self._value = crc return self def final(self): """ Return final CRC value. Return: int: final CRC value """ crc = self._value if self._reflect_output: crc = reflectbitorder(self._width, crc) crc ^= self._xor_output return crc class Crc(CrcBase): """ Creates a new general (user-defined) CRC calculator instance. Arguments: 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<`_: CRC-3/GSM, CRC-3/ROHC, CRC-4/G-704, CRC-4/ITU, CRC-4/INTERLAKEN, CRC-5/EPC-C1G2, CRC-5/EPC, CRC-5/G-704, CRC-5/ITU, CRC-5/USB, CRC-6/CDMA2000-A, CRC-6/CDMA2000-B, CRC-6/DARC, CRC-6/G-704, CRC-6/ITU, CRC-6/GSM, CRC-7/MMC, CRC-7, CRC-7/ROHC, CRC-7/UMTS, CRC-8/AUTOSAR, CRC-8/BLUETOOTH, CRC-8/CDMA2000, CRC-8/DARC, CRC-8/DVB-S2, CRC-8/GSM-A, CRC-8/GSM-B, CRC-8/I-432-1, CRC-8/ITU, CRC-8/I-CODE, CRC-8/LTE, CRC-8/MAXIM-DOW, CRC-8/MAXIM, DOW-CRC, CRC-8/MIFARE-MAD, CRC-8/NRSC-5, CRC-8/OPENSAFETY, CRC-8/ROHC, CRC-8/SAE-J1850, CRC-8/SMBUS, CRC-8, CRC-8/TECH-3250, CRC-8/AES, CRC-8/EBU, CRC-8/WCDMA, CRC-10/ATM, CRC-10, CRC-10/I-610, CRC-10/CDMA2000, CRC-10/GSM, CRC-11/FLEXRAY, CRC-11, CRC-11/UMTS, CRC-12/CDMA2000, CRC-12/DECT, CRC-12-X, CRC-12/GSM, CRC-12/UMTS, CRC-12/3GPP, CRC-13/BBC, CRC-14/DARC, CRC-14/GSM, CRC-15/CAN, CRC-15, CRC-15/MPT1327, CRC-16/ARC, ARC, CRC-16/LHA, CRC-IBM, CRC-16/CDMA2000, CRC-16/CMS, CRC-16/DDS-110, CRC-16/DECT-R, R-CRC-16, CRC-16/DECT-X, X-CRC-16, CRC-16/DNP, CRC-16/EN-13757, CRC-16/GENIBUS, CRC-16/DARC, CRC-16/EPC, CRC-16/EPC-C1G2, CRC-16/I-CODE, CRC-16/GSM, CRC-16/IBM-3740, CRC-16/AUTOSAR, CRC-16/CCITT-FALSE, CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B, CRC-16/X-25, CRC-B, X-25, CRC-16/ISO-IEC-14443-3-A, CRC-A, CRC-16/KERMIT, CRC-16/CCITT, CRC-16/CCITT-TRUE, CRC-16/V-41-LSB, CRC-CCITT, KERMIT, CRC-16/LJ1200, CRC-16/MAXIM-DOW, CRC-16/MAXIM, CRC-16/MCRF4XX, CRC-16/MODBUS, MODBUS, CRC-16/NRSC-5, CRC-16/OPENSAFETY-A, CRC-16/OPENSAFETY-B, CRC-16/PROFIBUS, CRC-16/IEC-61158-2, CRC-16/RIELLO, CRC-16/SPI-FUJITSU, CRC-16/AUG-CCITT, CRC-16/T10-DIF, CRC-16/TELEDISK, CRC-16/TMS37157, CRC-16/UMTS, CRC-16/BUYPASS, CRC-16/VERIFONE, CRC-16/USB, CRC-16/XMODEM, CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM, CRC-17/CAN-FD, CRC-21/CAN-FD, CRC-24/BLE, CRC-24/FLEXRAY-A, CRC-24/FLEXRAY-B, CRC-24/INTERLAKEN, CRC-24/LTE-A, CRC-24/LTE-B, CRC-24/OPENPGP, CRC-24, CRC-24/OS-9, CRC-30/CDMA, CRC-31/PHILIPS, CRC-32/AIXM, CRC-32Q, CRC-32/AUTOSAR, CRC-32/BASE91-D, CRC-32D, CRC-32/BZIP2, CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32, CRC-32/CD-ROM-EDC, CRC-32/CKSUM, CKSUM, CRC-32/POSIX, CRC-32/ISCSI, CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN, CRC-32C, CRC-32/ISO-HDLC, CRC-32, CRC-32/ADCCP, CRC-32/V-42, CRC-32/XZ, PKZIP, CRC-32/JAMCRC, JAMCRC, CRC-32/MPEG-2, CRC-32/XFER, XFER, CRC-40/GSM, CRC-64/ECMA-182, CRC-64, CRC-64/GO-ISO, CRC-64/WE, CRC-64/XZ, CRC-64/GO-ECMA, 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() Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+) Classifier: Intended Audience :: Developers Classifier: Intended Audience :: Information Technology Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Topic :: Scientific/Engineering Classifier: Topic :: Software Development :: Embedded Systems Classifier: Topic :: Utilities ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722827.0 crccheck-1.0/crccheck.egg-info/SOURCES.txt0000666000000000000000000000053400000000000016377 0ustar0000000000000000README.rst setup.py crccheck/__init__.py crccheck/base.py crccheck/checksum.py crccheck/crc.py crccheck/crc06.py crccheck.egg-info/PKG-INFO crccheck.egg-info/SOURCES.txt crccheck.egg-info/dependency_links.txt crccheck.egg-info/top_level.txt tests/__init__.py tests/test_base.py tests/test_checksum.py tests/test_crc.py tests/test_reflectbitorder.py././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722827.0 crccheck-1.0/crccheck.egg-info/dependency_links.txt0000666000000000000000000000000100000000000020557 0ustar0000000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722827.0 crccheck-1.0/crccheck.egg-info/top_level.txt0000666000000000000000000000001700000000000017241 0ustar0000000000000000crccheck tests ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1600722827.4327836 crccheck-1.0/setup.cfg0000666000000000000000000000005200000000000013070 0ustar0000000000000000[egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/setup.py0000666000000000000000000000273200000000000012770 0ustar0000000000000000#!/usr/bin/python import os try: from setuptools import setup except ImportError: from distutils.core import setup def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup( name='crccheck', description='Calculation library for CRCs and checksums', long_description=read("README.rst"), author='Martin Scharrer', author_email='martin@scharrer-online.de', license='GPL v3+', packages=['crccheck', 'tests'], version='1.0', url='https://sourceforge.net/projects/crccheck/', download_url='https://sourceforge.net/projects/crccheck/files/', install_requires=[], tests_require=['nose', ], test_suite='nose.collector', classifiers=[ "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "Intended Audience :: Developers", "Intended Audience :: Information Technology", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Topic :: Scientific/Engineering", "Topic :: Software Development :: Embedded Systems", "Topic :: Utilities", ], ) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1600722827.4307842 crccheck-1.0/tests/0000777000000000000000000000000000000000000012414 5ustar0000000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600707310.0 crccheck-1.0/tests/__init__.py0000666000000000000000000000144700000000000014533 0ustar0000000000000000"""Test modules for checksum package. License:: Copyright (C) 2015-2016 by Martin Scharrer 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 . """ ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/tests/test_base.py0000666000000000000000000000754100000000000014746 0ustar0000000000000000import sys from nose.plugins.skip import SkipTest from nose.tools import raises from crccheck.base import CrccheckBase from crccheck.crc import ALLCRCCLASSES import random def randombytes(length): return [random.randint(0, 255) for n in range(0, length)] @raises(NotImplementedError) def test_abstract_method(): """ For coverage """ ab = CrccheckBase() ab.process(bytearray(10)) def test_init(): for CrcClass in ALLCRCCLASSES: for n in range(0, 16): value = random.randint(0, 4294967295) assert CrcClass(value).value() == value def test_reset(): for CrcClass in ALLCRCCLASSES: for n in range(0, 16): value = random.randint(0, 4294967295) c = CrcClass() c.reset(value) assert c.value() == value def test_final(): """.final() should not change internal value""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert crc.final() == crc.final() def test_finalhex(): """.finalhex() should match .final()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert crc.final() == int(crc.finalhex(), 16) def test_finalbytes_big(): """.finalbytes() should match .final()""" if sys.version_info < (3, 3, 0): raise SkipTest for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert crc.final() == int.from_bytes(crc.finalbytes('big'), 'big') def test_finalbytes_little(): """.finalbytes() should match .final()""" if sys.version_info < (3, 3, 0): raise SkipTest for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert crc.final() == int.from_bytes(crc.finalbytes('little'), 'little') def test_calc(): """.calc() must be identical to .process() + .final()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert CrcClass.calc(data) == crc.final() def test_calchex_big(): """.calchex() must be identical to .process() + .finalhex()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert CrcClass.calchex(data, byteorder='big') == crc.finalhex('big') def test_calchex_little(): """.calchex() must be identical to .process() + .finalhex()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert CrcClass.calchex(data, byteorder='little') == crc.finalhex('little') def test_calcbytes_big(): """.calcbytes() must be identical to .process() + .finalbytes()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert CrcClass.calcbytes(data, byteorder='big') == crc.finalbytes('big') def test_calcbytes_little(): """.calcbytes() must be identical to .process() + .finalbytes()""" for CrcClass in ALLCRCCLASSES: for n in range(0, 16): data = randombytes(16) crc = CrcClass() crc.process(data) assert CrcClass.calcbytes(data, byteorder='little') == crc.finalbytes('little') ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/tests/test_checksum.py0000666000000000000000000000776300000000000015644 0ustar0000000000000000""" Unit tests for checksum module. License:: Copyright (C) 2015-2016 by Martin Scharrer 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 . """ import sys from nose.plugins.skip import SkipTest from nose.tools import raises import random from crccheck import checksum from crccheck.checksum import ALLCHECKSUMCLASSES, Checksum32, Checksum, ChecksumXor from crccheck.base import CrccheckError def randombytes(length): return [random.randint(0, 255) for n in range(0, length)] def test_allchecksums_bigendian(): def selftest_bigendian(cls): return cls.selftest(byteorder='big') for checksumclass in ALLCHECKSUMCLASSES: yield selftest_bigendian, checksumclass def test_allchecksums_littleendian(): def selftest_littleendian(cls): return cls.selftest(byteorder='little') for checksumclass in ALLCHECKSUMCLASSES: yield selftest_littleendian, checksumclass # noinspection PyProtectedMember @raises(CrccheckError) def test_allchecksums_fail(): checksumclass = ALLCHECKSUMCLASSES[0] checksumclass.selftest(checksumclass._check_data, ~checksumclass._check_result) def test_generator(): Checksum32.calc((n for n in range(0, 255))) def test_list1(): Checksum32.calc([n for n in range(0, 255)]) def test_list2(): Checksum32.calc([n for n in range(0, 255)], 1) def test_bytearray1(): Checksum32.calc(bytearray.fromhex("12345678909876543210")) def test_bytes(): if sys.version_info < (3, 3, 0): raise SkipTest Checksum32.calc(bytes.fromhex("12345678909876543210")) def test_string1(): if sys.version_info < (3, 3, 0): raise SkipTest Checksum32.calc(b"Teststring") def test_string3(): if sys.version_info < (3, 3, 0): raise SkipTest Checksum32.calc("Teststring".encode(), ) def test_general_checksum_valid_width(): """ Checksum() Should allow for any positive width which is a multiple of 8. """ for n in range(8, 129, 8): Checksum(n) def test_general_checksum_invalid_width(): for n in (0, 1, 7, 9, 33): try: Checksum(n) except ValueError: pass else: raise Exception def test_general_checksum_ident(): data = randombytes(1024) assert checksum.Checksum32.calc(data) == checksum.Checksum(32).process(data).final() assert checksum.Checksum16.calc(data) == checksum.Checksum(16).process(data).final() assert checksum.Checksum8.calc(data) == checksum.Checksum(8).process(data).final() def test_general_checksumxor_valid_width(): """ Checksum() Should allow for any positive width which is a multiple of 8. """ for n in range(8, 129, 8): ChecksumXor(n) def test_general_checksumxor_invalid_width(): for n in (0, 1, 7, 9, 33): try: ChecksumXor(n) except ValueError: pass else: raise Exception def test_general_checksumxor_ident(): data = randombytes(1024) assert checksum.ChecksumXor32.calc(data) == checksum.ChecksumXor(32).process(data).final() assert checksum.ChecksumXor16.calc(data) == checksum.ChecksumXor(16).process(data).final() assert checksum.ChecksumXor8.calc(data) == checksum.ChecksumXor(8).process(data).final() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1600722381.0 crccheck-1.0/tests/test_crc.py0000666000000000000000000001662100000000000014602 0ustar0000000000000000""" Unit tests for checksum module. License:: Copyright (C) 2015-2016 by Martin Scharrer 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 . """ import sys from nose.plugins.skip import SkipTest from nose.tools import raises from crccheck.base import CrccheckError from crccheck.crc import ALLCRCCLASSES, ALLCRCCLASSES_ALIASES, Crc32, Crc, find, identify import crccheck import random def test_allcrc(): """Test if expected 'check' result is calulated with standard test vector.""" for crcclass in ALLCRCCLASSES: yield lambda x: x.selftest(), crcclass def test_allcrcfail(): """Test if 'check' result is not reached with different input.""" def fail(cls): try: cls.selftest(bytearray(b"wrongtestinput"), cls._check_result) except CrccheckError: # This is the correct response pass else: raise Exception("Selftest passed with incorrect input!") for crcclass in ALLCRCCLASSES: yield fail, crcclass def test_generator(): Crc32.calc((n for n in range(0, 255))) def test_list1(): Crc32.calc([n for n in range(0, 255)]) def test_list2(): Crc32.calc([n for n in range(0, 255)], 123) def test_bytearray(): Crc32.calc(bytearray.fromhex("12345678909876543210")) def test_bytes(): if sys.version_info < (3, 3, 0): raise SkipTest Crc32.calc(bytes.fromhex("12345678909876543210")) def test_string1(): if sys.version_info < (3, 3, 0): raise SkipTest Crc32.calc(b"Teststring") def test_string2(): if sys.version_info < (3, 3, 0): raise SkipTest Crc32.calc("Teststring".encode(), ) def test_general_crc(): crc = Crc(32, 0x4C11DB7, 0xFFFFFFFF, True, True, 0x00000000, 0x340BC6D9) crc.selftest() @raises(CrccheckError) def test_general_crc_fail(): crc = Crc(32, 0x4C11DB7, 0xFFFFFFFF, True, True, 0x00000000, ~0x340BC6D9) crc.selftest() def test_backwards_compatible(): """Crc8Base was called Crc8, etc. Must still be equal CRCs""" assert crccheck.crc.Crc8() == crccheck.crc.Crc8Base() assert crccheck.crc.Crc16() == crccheck.crc.Crc16Base() assert crccheck.crc.Crc32() == crccheck.crc.Crc32Base() def test_find32(): for cls in find(width=32): assert cls._width == 32 def test_find_unknown(): assert len(find(width=12345)) == 0 def test_find_all(): assert find() == list(ALLCRCCLASSES) def test_find_some(): assert find(ALLCRCCLASSES[1:3]) == list(ALLCRCCLASSES[1:3]) def test_find_width(): assert find(width=32) == list(cls for cls in ALLCRCCLASSES if cls._width == 32) def test_find_poly(): assert find(poly=0x04C11DB7) == list(cls for cls in ALLCRCCLASSES if cls._poly == 0x04C11DB7) def test_find_initvalue(): assert find(initvalue=0) == list(cls for cls in ALLCRCCLASSES if cls._initvalue == 0) def test_find_reflect_input(): assert find(reflect_input=True) == list(cls for cls in ALLCRCCLASSES if cls._reflect_input) def test_find_reflect_output(): assert find(reflect_output=False) == list(cls for cls in ALLCRCCLASSES if not cls._reflect_output) def test_find_xor_output(): assert find(xor_output=0) == list(cls for cls in ALLCRCCLASSES if cls._xor_output == 0) def test_find_check_result(): assert find(check_result=6) == list(cls for cls in ALLCRCCLASSES if cls._check_result == 6) def test_find_residue(): assert find(residue=0) == list(cls for cls in ALLCRCCLASSES if cls._residue == 0) def test_find_mixed(): assert find(ALLCRCCLASSES[0:20], width=8, residue=0, reflect_input=False, reflect_output=False) \ == list(cls for cls in ALLCRCCLASSES[0:20] if cls._width == 8 and cls._residue == 0 and not cls._reflect_input and not cls._reflect_output) def test_identify_1(): data = bytes(random.randrange(256) for _ in range(10)) cls = crccheck.crc.Crc64GoIso assert identify(data, cls.calc(data))() == cls() def test_identify_2(): data = bytes(random.randrange(256) for _ in range(10)) classes = [crccheck.crc.Crc64GoIso, crccheck.crc.Crc8, crccheck.crc.Crc32IsoHdlc] cls = crccheck.crc.Crc32IsoHdlc assert identify(data, cls.calc(data), classes=classes)() == cls() def test_identify_width(): data = bytes(random.randrange(256) for _ in range(10)) allcrc32 = [c for c in ALLCRCCLASSES if c._width == 32] cls = random.choice(allcrc32) assert identify(data, cls.calc(data), 32)() == cls() assert identify(data, cls.calc(data), 32, allcrc32)() == cls() def test_identify_width_list(): data = bytes(random.randrange(256) for _ in range(10)) allcrc32 = [c for c in ALLCRCCLASSES if c._width == 32] cls = random.choice(allcrc32) result = identify(data, cls.calc(data), 32, one=False) assert len(result) >= 1 and result[0]() == cls() result = identify(data, cls.calc(data), 32, allcrc32, one=False) assert len(result) >= 1 and result[0]() == cls() def test_identify_notexisting(): assert identify(b'Test', 0) is None assert identify(b'Test', 0, 234) is None assert identify(b'Test', 0, classes=[]) is None assert identify(b'Test', 0, 235, classes=[]) is None assert identify(b'Test', 0, one=False) == [] assert identify(b'Test', 0, 234, one=False) == [] assert identify(b'Test', 0, classes=[], one=False) == [] assert identify(b'Test', 0, 235, classes=[], one=False) == [] def test_repr(): """Test if __repr__ does not cause errors""" c = Crc(16, 0xDEAD, initvalue=0x00, reflect_input=False, reflect_output=False, xor_output=0x00, check_result=None, residue=None) r = repr(c) c = Crc(16, 0xDEAD, initvalue=0x00, reflect_input=False, reflect_output=False, xor_output=0x00, check_result=None, residue=0x00) r = repr(c) c = Crc(16, 0xDEAD, initvalue=0x00, reflect_input=False, reflect_output=False, xor_output=0x00, check_result=0x00, residue=None) r = repr(c) c = Crc(16, 0xDEAD, initvalue=0x00, reflect_input=False, reflect_output=False, xor_output=0x00, check_result=0x00, residue=0x00) r = repr(c) def test_selftest_data(): c = Crc(21, 0xDEAD) c.selftest(b'Test', 0x40be8) def test_crc_calc(): assert Crc(21, 0xDEAD).calc(b'Test') == 265192 def test_crc_calchex(): assert Crc(21, 0xDEAD).calchex(b'Test') == '040be8' def test_crc_calcbytes_big(): assert Crc(21, 0xDEAD).calcbytes(b'Test', byteorder='big') == b'\x04\x0b\xe8' def test_crc_calcbytes_little(): assert Crc(21, 0xDEAD).calcbytes(b'Test', byteorder='little') == b'\xe8\x0b\x04' def test_aliases(): assert set(ALLCRCCLASSES).issubset(ALLCRCCLASSES_ALIASES) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1461399253.0 crccheck-1.0/tests/test_reflectbitorder.py0000666000000000000000000000171300000000000017206 0ustar0000000000000000from crccheck.base import reflectbitorder def test_1(): assert reflectbitorder(8, 0x80) == 0x01 def test_2(): assert reflectbitorder(16, 0x8000) == 0x0001 def test_3(): assert reflectbitorder(8, 0x81) == 0x81 def test_4(): assert reflectbitorder(80, (1 << 79)) == 0x01 def test_5(): assert reflectbitorder(65, 0x1) == (1 << 64) def test_6(): assert reflectbitorder(3, 0b110) == 0b011 def test_7(): assert reflectbitorder(3, 0b110) == 0b011 def test_8(): assert reflectbitorder(0, 0) == 0 def expect(w, v, e): assert reflectbitorder(w, v) == e def test_random(): import random random.seed() for width in range(1, 125): randombitstr = "".join([str(random.randint(0, 1)) for m in range(0, width)]) value = int(randombitstr, 2) expectedresult = int("".join(reversed(randombitstr)), 2) yield expect, width, value, expectedresult