-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFletcherChecksumLib.py
115 lines (104 loc) · 4.24 KB
/
FletcherChecksumLib.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# -----------------------------------------------------
# #
# Fletcher’s Checksum Library #
# Checksum in the current version of the library: #
# #
# Fletcher – 16: YES #
# Fletcher – 32: YES #
# Fletcher – 64: YES #
# #
# Possible input values for calculations: #
# #
# STRING: YES #
# BYTES: YES #
# #
# -----------------------------------------------------
class FletcherChecksumStr:
"""
This class contains static methods for working with STRING values as input parameters.
"""
@staticmethod
def get_fletcher16(data: str) -> dict:
"""
Accepts a string as input.
Returns the Fletcher16 checksum value in decimal and hexadecimal format.
8-bit implementation (16-bit checksum)
"""
sum1, sum2 = int(), int()
data = data.encode()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 255
sum2 = (sum2 + sum1) % 255
result = (sum2 << 8) | sum1
return {"Fletcher16_dec": result, "Fletcher16_hex": hex(result)}
@staticmethod
def get_fletcher32(data: str) -> dict:
"""
Accepts a string as input.
Returns the Fletcher32 checksum value in decimal and hexadecimal format.
16-bit implementation (32-bit checksum)
"""
sum1, sum2 = int(), int()
data = data.encode()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 65535
sum2 = (sum2 + sum1) % 65535
result = (sum2 << 16) | sum1
return {"Fletcher32_dec": result, "Fletcher32_hex": hex(result)}
@staticmethod
def get_fletcher64(data: str) -> dict:
"""
Accepts a string as input.
Returns the Fletcher64 checksum value in decimal and hexadecimal format.
32-bit implementation (64-bit checksum)
"""
sum1, sum2 = int(), int()
data = data.encode()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 4294967295
sum2 = (sum2 + sum1) % 4294967295
result = (sum2 << 32) | sum1
return {"Fletcher64_dec": result, "Fletcher64_hex": hex(result)}
class FletcherChecksumBytes:
"""
This class contains static methods for working with BYTES values as input parameters.
"""
@staticmethod
def get_fletcher16(data: bytes) -> dict:
"""
Accepts bytes as input.
Returns the Fletcher16 checksum value in decimal and hexadecimal format.
8-bit implementation (16-bit checksum)
"""
sum1, sum2 = int(), int()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 255
sum2 = (sum2 + sum1) % 255
result = (sum2 << 8) | sum1
return {"Fletcher16_dec": result, "Fletcher16_hex": hex(result)}
@staticmethod
def get_fletcher32(data: bytes) -> dict:
"""
Accepts bytes as input.
Returns the Fletcher32 checksum value in decimal and hexadecimal format.
16-bit implementation (32-bit checksum)
"""
sum1, sum2 = int(), int()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 65535
sum2 = (sum2 + sum1) % 65535
result = (sum2 << 16) | sum1
return {"Fletcher32_dec": result, "Fletcher32_hex": hex(result)}
@staticmethod
def get_fletcher64(data: bytes) -> dict:
"""
Accepts bytes as input.
Returns the Fletcher64 checksum value in decimal and hexadecimal format.
32-bit implementation (64-bit checksum)
"""
sum1, sum2 = int(), int()
for index in range(len(data)):
sum1 = (sum1 + data[index]) % 4294967295
sum2 = (sum2 + sum1) % 4294967295
result = (sum2 << 32) | sum1
return {"Fletcher64_dec": result, "Fletcher64_hex": hex(result)}