Refactor message signing and verifying logic

This commit is contained in:
Kristóf Tóth 2018-07-16 10:29:06 +02:00
parent 5770e29733
commit 7c13d31de0

View File

@ -19,16 +19,20 @@ def message_checksum(message):
def sign_message(key, message):
signature = HMAC(key, message_bytes(message)).signature
signature = message_signature(key, message)
message['signature'] = b64encode(signature).decode()
def message_signature(key, message):
return HMAC(key, message_bytes(message)).signature
def verify_message(key, message):
message = deepcopy(message)
try:
signature_b64 = message.pop('signature')
signature = b64decode(signature_b64)
actual_signature = HMAC(key, message_bytes(message)).signature
actual_signature = message_signature(key, message)
return signature == actual_signature
except KeyError:
return False