From 7c13d31de026f9eab6c5c0d67429a54d2e82d90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Mon, 16 Jul 2018 10:29:06 +0200 Subject: [PATCH] Refactor message signing and verifying logic --- lib/tfw/crypto.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/tfw/crypto.py b/lib/tfw/crypto.py index be2d54e..a40cf5c 100644 --- a/lib/tfw/crypto.py +++ b/lib/tfw/crypto.py @@ -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