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
|
#!/usr/bin/python3
#
# this script is based on the upstream usage example found on README.rst
#
import sys
from crccheck.crc import Crc32, CrcXmodem
from crccheck.checksum import Checksum32
CRC_OK = 2090640218
CHECKSUM_OK = 3735928559
CRCBYTES_OK = bytes(b"\x9e\x8c")
CRCHEX_OK = "9e8c"
CRCINT_OK = 40588
def check(name, value, value_ok):
if value != value_ok:
print("ERROR: %s do not match with %s" % (name, str(value_ok)))
print("%s = %s" % (name, str(value)))
sys.exit(1)
# quick calculation
data = bytearray.fromhex("DEADBEEF")
crc = Crc32.calc(data)
check("crc", crc, CRC_OK)
checksum = Checksum32.calc(data)
check("checksum", checksum, CHECKSUM_OK)
# process multiple data buffers
data1 = b"Binary string"
data2 = bytes.fromhex("1234567890")
data3 = (0x0, 255, 12, 99)
crcinst = CrcXmodem()
crcinst.process(data1)
crcinst.process(data2)
crcinst.process(data3[1:-1])
crcbytes = crcinst.finalbytes()
check("crcbytes", crcbytes, CRCBYTES_OK)
crchex = crcinst.finalhex()
check("crchex", crchex, CRCHEX_OK)
crcint = crcinst.final()
check("crcint", crcint, CRCINT_OK)
sys.exit(0)
|