-
Notifications
You must be signed in to change notification settings - Fork 50
/
sum_hashes.py
134 lines (83 loc) · 2.18 KB
/
sum_hashes.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
Hex Viewer.
Licensed under MIT
Copyright (c) 2013-2020 Isaac Muse <[email protected]>
"""
BIT8_MOD = 256
BIT16_MOD = 65536
BIT24_MOD = 16777216
BIT32_MOD = 4294967296
class sum8(object): # noqa
"""Sum8 hash."""
__name = "sum8"
__digest_size = 1
def __init__(self, arg=b""):
"""Initialize."""
self.sum = 0
self.update(arg)
@property
def name(self):
"""Name of the hash."""
return self.__name
@property
def digest_size(self):
"""Size of the digest."""
return self.__digest_size
def update(self, arg):
"""Update the hash."""
for b in arg:
self.sum += int(b)
def digest(self):
"""Get the digest."""
return self.sum % BIT8_MOD
def hexdigest(self):
"""Get the hex digest."""
return "%02x" % self.digest()
def copy(self):
"""Get the copy."""
import copy
return copy.deepcopy(self)
class sum16(sum8): # noqa
"""Sum16 hash."""
__name = "sum16"
__digest_size = 2
def digest(self):
"""Get the digest."""
return self.sum % BIT16_MOD
def hexdigest(self):
"""Get the hex digest."""
return "%04x" % self.digest()
class sum24(sum8): # noqa
"""Sum24 hash."""
__name = "sum24"
__digest_size = 3
def digest(self):
"""Get the digest."""
return self.sum % BIT24_MOD
def hexdigest(self):
"""Get the hex digest."""
return "%06x" % self.digest()
class sum32(sum8): # noqa
"""Sum32 hash."""
__name = "sum32"
__digest_size = 4
def digest(self):
"""Get the digest."""
return self.sum % BIT32_MOD
def hexdigest(self):
"""Get the hex digest."""
return "%08x" % self.digest()
class xor8(sum8): # noqa
"""Xor8 hash."""
__name = "xor8"
__digest_size = 1
def update(self, arg):
"""Update the hash."""
for b in arg:
self.sum ^= int(b) & 0xFF
def digest(self):
"""Get the digest."""
return int(self.sum)
def hexdigest(self):
"""Get the hex digest."""
return "%02x" % self.digest()