-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHA1.py
160 lines (123 loc) · 3.87 KB
/
SHA1.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import sys
class SHA1:
def __init__(self):
self.__H = [
0x67452301,
0xEFCDAB89,
0x98BADCFE,
0x10325476,
0xC3D2E1F0
]
def __str__(self):
return ''.join((hex(h)[2:]).rjust(8, '0') for h in self.__H)
# Private static methods used for internal operations.
@staticmethod
def __ROTL(n, x, w=32):
return ((x << n) | (x >> w - n))
@staticmethod
def __padding(stream):
l = len(stream) # Bytes
hl = [int((hex(l*8)[2:]).rjust(16, '0')[i:i+2], 16)
for i in range(0, 16, 2)]
l0 = (56 - l) % 64
if not l0:
l0 = 64
if isinstance(stream, str):
stream += chr(0b10000000)
stream += chr(0)*(l0-1)
for a in hl:
stream += chr(a)
elif isinstance(stream, bytes):
stream += bytes([0b10000000])
stream += bytes(l0-1)
stream += bytes(hl)
return stream
@staticmethod
def __prepare(stream):
M = []
n_blocks = len(stream) // 64
stream = bytearray(stream)
for i in range(n_blocks): # 64 Bytes per Block
m = []
for j in range(16): # 16 Words per Block
n = 0
for k in range(4): # 4 Bytes per Word
n <<= 8
n += stream[i*64 + j*4 + k]
m.append(n)
M.append(m[:])
return M
@staticmethod
def __debug_print(t, a, b, c, d, e):
print('t = {0} : \t'.format(t),
(hex(a)[2:]).rjust(8, '0'),
(hex(b)[2:]).rjust(8, '0'),
(hex(c)[2:]).rjust(8, '0'),
(hex(d)[2:]).rjust(8, '0'),
(hex(e)[2:]).rjust(8, '0')
)
# Private instance methods used for internal operations.
def __process_block(self, block):
MASK = 2**32-1
W = block[:]
for t in range(16, 80):
W.append(SHA1.__ROTL(1, (W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]))
& MASK)
a, b, c, d, e = self.__H[:]
for t in range(80):
if t <= 19:
K = 0x5a827999
f = (b & c) ^ (~b & d)
elif t <= 39:
K = 0x6ed9eba1
f = b ^ c ^ d
elif t <= 59:
K = 0x8f1bbcdc
f = (b & c) ^ (b & d) ^ (c & d)
else:
K = 0xca62c1d6
f = b ^ c ^ d
T = ((SHA1.__ROTL(5, a) + f + e + K + W[t]) & MASK)
e = d
d = c
c = SHA1.__ROTL(30, b) & MASK
b = a
a = T
#SHA1.debug_print(t, a,b,c,d,e)
self.__H[0] = (a + self.__H[0]) & MASK
self.__H[1] = (b + self.__H[1]) & MASK
self.__H[2] = (c + self.__H[2]) & MASK
self.__H[3] = (d + self.__H[3]) & MASK
self.__H[4] = (e + self.__H[4]) & MASK
# Public methods for class use.
def update(self, stream):
stream = SHA1.__padding(stream)
stream = SHA1.__prepare(stream)
for block in stream:
self.__process_block(block)
def digest(self):
pass
def hexdigest(self):
s = ''
for h in self.__H:
s += (hex(h)[2:]).rjust(8, '0')
return s
def usage():
print('Usage: python SHA1.py <file> [<file> ...]')
sys.exit()
def main():
if len(sys.argv) < 2:
usage()
for filename in sys.argv[1:]:
try:
with open(filename, 'rb') as f:
content = f.read()
except:
print ('ERROR: Input file "{0}" cannot be read.'.format(filename))
else:
h = SHA1()
h.update(content)
hex_sha = h.hexdigest()
print("{0} {1}".format(hex_sha, filename))
if __name__ == '__main__':
main()