-
-
Notifications
You must be signed in to change notification settings - Fork 50.5k
Expand file tree
/
Copy pathcrc32.py
More file actions
71 lines (57 loc) · 1.73 KB
/
crc32.py
File metadata and controls
71 lines (57 loc) · 1.73 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
CRC-32 is a cyclic redundancy check algorithm that produces a 32-bit checksum.
It is widely used for error detection in network transmissions and file integrity
checks (ZIP, PNG, gzip, etc.). This implementation uses the reflected polynomial
0xEDB88320 with a precomputed 256-entry lookup table.
Reference: https://en.wikipedia.org/wiki/Cyclic_redundancy_check
"""
def _generate_crc32_table() -> list[int]:
"""
Build a 256-entry lookup table for CRC-32 using the reflected
polynomial 0xEDB88320.
>>> table = _generate_crc32_table()
>>> len(table)
256
>>> hex(table[0])
'0x0'
>>> hex(table[1])
'0x77073096'
"""
table: list[int] = []
for byte in range(256):
crc = byte
for _ in range(8):
if crc & 1:
crc = (crc >> 1) ^ 0xEDB88320
else:
crc >>= 1
table.append(crc)
return table
_CRC32_TABLE = _generate_crc32_table()
def crc32(data: bytes) -> int:
"""
Compute the CRC-32 checksum for the given bytes.
The output matches Python's ``zlib.crc32()`` for the same input.
>>> crc32(b'')
0
>>> hex(crc32(b'hello'))
'0x3610a686'
>>> crc32(b'hello world')
222957957
>>> hex(crc32(b'The quick brown fox jumps over the lazy dog'))
'0x414fa339'
Verify against zlib:
>>> import zlib
>>> crc32(b'hello world') == zlib.crc32(b'hello world')
True
>>> crc32(b'\\x00\\xff\\x55\\xaa') == zlib.crc32(b'\\x00\\xff\\x55\\xaa')
True
"""
crc = 0xFFFFFFFF
for byte in data:
lookup_index = (crc ^ byte) & 0xFF
crc = (crc >> 8) ^ _CRC32_TABLE[lookup_index]
return crc ^ 0xFFFFFFFF
if __name__ == "__main__":
import doctest
doctest.testmod()