| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# this script is based on the upstream usage example found on README.rst |
| 4 |
# |
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
from crccheck.crc import Crc32, CrcXmodem |
| 9 |
from crccheck.checksum import Checksum32 |
| 10 |
|
| 11 |
CRC_OK = 2090640218 |
| 12 |
CHECKSUM_OK = 3735928559 |
| 13 |
CRCBYTES_OK = bytes(b"\x9e\x8c") |
| 14 |
CRCHEX_OK = "9e8c" |
| 15 |
CRCINT_OK = 40588 |
| 16 |
|
| 17 |
def check(name, value, value_ok): |
| 18 |
if value != value_ok: |
| 19 |
print("ERROR: %s do not match with %s" % (name, str(value_ok))) |
| 20 |
print("%s = %s" % (name, str(value))) |
| 21 |
sys.exit(1) |
| 22 |
|
| 23 |
# quick calculation |
| 24 |
data = bytearray.fromhex("DEADBEEF") |
| 25 |
|
| 26 |
crc = Crc32.calc(data) |
| 27 |
check("crc", crc, CRC_OK) |
| 28 |
|
| 29 |
checksum = Checksum32.calc(data) |
| 30 |
check("checksum", checksum, CHECKSUM_OK) |
| 31 |
|
| 32 |
# process multiple data buffers |
| 33 |
data1 = b"Binary string" |
| 34 |
data2 = bytes.fromhex("1234567890") |
| 35 |
data3 = (0x0, 255, 12, 99) |
| 36 |
crcinst = CrcXmodem() |
| 37 |
crcinst.process(data1) |
| 38 |
crcinst.process(data2) |
| 39 |
crcinst.process(data3[1:-1]) |
| 40 |
|
| 41 |
crcbytes = crcinst.finalbytes() |
| 42 |
check("crcbytes", crcbytes, CRCBYTES_OK) |
| 43 |
|
| 44 |
crchex = crcinst.finalhex() |
| 45 |
check("crchex", crchex, CRCHEX_OK) |
| 46 |
|
| 47 |
crcint = crcinst.final() |
| 48 |
check("crcint", crcint, CRCINT_OK) |
| 49 |
|
| 50 |
sys.exit(0) |