mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-22 23:01:31 +00:00
Refactor message signing and verifying logic
This commit is contained in:
parent
5770e29733
commit
7c13d31de0
@ -19,16 +19,20 @@ def message_checksum(message):
|
|||||||
|
|
||||||
|
|
||||||
def sign_message(key, message):
|
def sign_message(key, message):
|
||||||
signature = HMAC(key, message_bytes(message)).signature
|
signature = message_signature(key, message)
|
||||||
message['signature'] = b64encode(signature).decode()
|
message['signature'] = b64encode(signature).decode()
|
||||||
|
|
||||||
|
|
||||||
|
def message_signature(key, message):
|
||||||
|
return HMAC(key, message_bytes(message)).signature
|
||||||
|
|
||||||
|
|
||||||
def verify_message(key, message):
|
def verify_message(key, message):
|
||||||
message = deepcopy(message)
|
message = deepcopy(message)
|
||||||
try:
|
try:
|
||||||
signature_b64 = message.pop('signature')
|
signature_b64 = message.pop('signature')
|
||||||
signature = b64decode(signature_b64)
|
signature = b64decode(signature_b64)
|
||||||
actual_signature = HMAC(key, message_bytes(message)).signature
|
actual_signature = message_signature(key, message)
|
||||||
return signature == actual_signature
|
return signature == actual_signature
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user