-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge28.py
76 lines (57 loc) · 1.93 KB
/
challenge28.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
import struct
import hashlib
def left_rotate(value, shift):
return ((value << shift) & 0xffffffff) | (value >> (32 - shift))
def sha1(message, ml=None, h0=0x67452301, h1=0xEFCDAB89, h2=0x98BADCFE, h3=0x10325476, h4=0xC3D2E1F0):
if ml is None:
ml = len(message) * 8
message += b'\x80'
while (len(message) * 8) % 512 != 448:
message += b'\x00'
message += struct.pack('>Q', ml)
for i in range(0, len(message), 64):
w = [0] * 80
for j in range(16):
w[j] = struct.unpack('>I', message[i + j * 4:i + j * 4 + 4])[0]
for j in range(16, 80):
w[j] = left_rotate(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1)
a = h0
b = h1
c = h2
d = h3
e = h4
# Main loop
for j in range(80):
if j <= 19:
f = d ^ (b & (c ^ d))
k = 0x5A827999
elif 20 <= j <= 39:
f = b ^ c ^ d
k = 0x6ED9EBA1
elif 40 <= j <= 59:
f = (b & c) | (d & (b | c))
k = 0x8F1BBCDC
else:
f = b ^ c ^ d
k = 0xCA62C1D6
temp = left_rotate(a, 5) + f + e + k + w[j] & 0xffffffff
e = d
d = c
c = left_rotate(b, 30)
b = a
a = temp
h0 = (h0 + a) & 0xffffffff
h1 = (h1 + b) & 0xffffffff
h2 = (h2 + c) & 0xffffffff
h3 = (h3 + d) & 0xffffffff
h4 = (h4 + e) & 0xffffffff
return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)
def sha1_mac(key, message):
return sha1(key + message)
if __name__ == '__main__':
key = b'this is a secret key'
message = b'This is a message to test the implementation'
message2 = b'This is another test message'
print(sha1_mac(key, message))
print(sha1_mac(key, message2))
print(sha1_mac(key, message) == sha1_mac(key, message2))