-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHMAC256.py
35 lines (26 loc) · 1.18 KB
/
HMAC256.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
from hashlib import sha256
import base64
sha256()
def number_to_repeated_bytes(number, byte_length, byteorder='big', signed=False):
num_bytes = number.to_bytes(
(number.bit_length() + 7) // 8 or 1, byteorder=byteorder, signed=signed)
if len(num_bytes) < byte_length:
repetitions = (byte_length + len(num_bytes) - 1) // len(num_bytes)
num_bytes = (num_bytes * repetitions)[:byte_length]
return num_bytes
def hmac(key: str, message: str):
opad = 0x5c
ipad = 0x36
blocksize = 64
encoded_key = key.encode()
encoded_opad = number_to_repeated_bytes(opad, blocksize)
encoded_ipad = number_to_repeated_bytes(ipad, blocksize)
if len(encoded_key) < blocksize:
encoded_key = encoded_key.ljust(blocksize, b'\x00')
if len(encoded_key) > blocksize:
encoded_key = sha256(encoded_key).digest()
key_opad = bytes([b1 ^ b2 for b1, b2 in zip(encoded_key, encoded_opad)])
key_ipad = bytes([b1 ^ b2 for b1, b2 in zip(encoded_key, encoded_ipad)])
digest = sha256(key_opad + sha256(key_ipad +
message.encode()).digest()).digest()
return base64.urlsafe_b64encode(digest).rstrip(b'=').decode("utf-8")