1
2
3
4
5
6
7
8
9
10 CRC7_POLY = 0x91
11
13 """
14 Compute CRC of a single byte.
15 """
16 for i in range(8):
17 if v & 1:
18 v ^= CRC7_POLY
19
20 v >>= 1
21
22 return v
23
24 CRC7_TABLE = tuple(byte_crc7(i) for i in range(256))
25
27 """
28 Compute CRC of a whole message.
29 """
30 crc = 0
31
32 for c in data:
33 crc = CRC7_TABLE[crc ^ c]
34
35 return crc
36
37
38 if __name__ == '__main__':
39 import sys
40
41 if len(sys.argv) > 1:
42 data = sys.argv[1]
43 else:
44 data = sys.stdin.read()
45
46 data = [int(b) for b in data.replace('\n', ',').split(',') if b != '']
47 print '\n{0:#x}, {0}'.format(crc7(data))
48