-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmacSha256.py
executable file
·45 lines (33 loc) · 1.29 KB
/
hmacSha256.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
"""
The MIT License (MIT)
Copyright © 2018 Jean-Christophe Bos & HC² (www.hc2.fr)
"""
from hashlib import sha256
# ============================================================================
# ============================================================================
# ============================================================================
def HMACSha256(keyBin, msgBin) :
block_size = 64 # SHA-256 blocks size
trans_5C = bytearray(256)
for x in range(len(trans_5C)) :
trans_5C[x] = x^0x5C
trans_36 = bytearray(256)
for x in range(len(trans_36)) :
trans_36[x] = x^0x36
def translate(d, t) :
res = bytearray(len(d))
for x in range(len(d)) :
res[x] = t[d[x]]
return res
keyBin = keyBin + b'\x00' * (block_size - len(keyBin))
inner = sha256()
inner.update(translate(keyBin, trans_36))
inner.update(msgBin)
inner = inner.digest()
outer = sha256()
outer.update(translate(keyBin, trans_5C))
outer.update(inner)
return outer.digest()
# ============================================================================
# ============================================================================
# ============================================================================