-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMAC.py
More file actions
48 lines (34 loc) · 1.49 KB
/
HMAC.py
File metadata and controls
48 lines (34 loc) · 1.49 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
import hashlib
from random import randbytes
# key = randbytes(64)
key = b'0000000000000000000000000000000000000000000000000000000000000000'
ipad = b'\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36'\
b'\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36'\
b'\x36\x36\x36\x36\x36\x36\x36\x36\x36\x36'
opad = b'\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c'\
b'\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c'\
b'\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c\x5c'
def mac(k, m):
tagf = h(xor(k, opad) + h(xor(k, ipad)+m))
return tagf
def vrfy(k, m, t):
if h(xor(k, opad) + h(xor(k, ipad)+m)) == t:
return True
else:
return False
def h(data):
s = hashlib.sha256()
s.update(data)
return s.digest()
def xor(bytes1, bytes2):
if len(bytes1) != len(bytes2):
raise ValueError("Bytes objects must be same length to XOR them.")
output = b''
for i in range(len(bytes1)):
output = output + bytes([bytes1[i] ^ bytes2[i]])
return output
if __name__ == "__main__":
m2 = b"Sherman is Better than Mocha"
tag = mac(key, m2)
verify = vrfy(key, m2, tag)
print("Message:\n" + str(m2.decode()) + "\nTag:\n" + str(tag) + "\nKey:\n" + str(key.decode()) + "\nVerify:\n" + str(verify))