-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.py
31 lines (24 loc) · 1 KB
/
mac.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
import hashlib
import hmac
import os
def generate_mac(padded_message):
# generate random key
mac_key = os.urandom(16)
if isinstance(mac_key, str):
mac_key = mac_key.encode('utf-8')
if isinstance(padded_message, str):
padded_message = padded_message.encode('utf-8')
hmac_calculated = hmac.new(mac_key, padded_message, hashlib.sha256)
return mac_key, hmac_calculated.digest()
def verify_mac(Dec_Data, mac, mac_key):
if isinstance(mac_key, str):
mac_key = mac_key.encode('utf-8')
if isinstance(Dec_Data, str):
Dec_Data = Dec_Data.encode('utf-8')
hmac_calculated = hmac.new(mac_key, Dec_Data, hashlib.sha256)
hmac_digest = hmac_calculated.digest()
print("Mac for the decrypted image:",hmac_digest)
if hmac.compare_digest(mac, hmac_digest):
print("\nMAC verification successful.")
else:
print("\nMAC verification failed.")