mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-11-05 23:11:22 +00:00
Move message checksum logic to crypto.py
This commit is contained in:
parent
b2cb60ef02
commit
5770e29733
@ -4,6 +4,7 @@
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
from base64 import b64encode, b64decode
|
from base64 import b64encode, b64decode
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from hashlib import md5
|
||||||
|
|
||||||
from cryptography.hazmat.backends import default_backend
|
from cryptography.hazmat.backends import default_backend
|
||||||
from cryptography.hazmat.primitives.hashes import SHA256
|
from cryptography.hazmat.primitives.hashes import SHA256
|
||||||
@ -13,6 +14,10 @@ from cryptography.exceptions import InvalidSignature
|
|||||||
from tfw.networking import message_bytes
|
from tfw.networking import message_bytes
|
||||||
|
|
||||||
|
|
||||||
|
def message_checksum(message):
|
||||||
|
return md5(message_bytes(message)).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def sign_message(key, message):
|
def sign_message(key, message):
|
||||||
signature = HMAC(key, message_bytes(message)).signature
|
signature = HMAC(key, message_bytes(message)).signature
|
||||||
message['signature'] = b64encode(signature).decode()
|
message['signature'] = b64encode(signature).decode()
|
||||||
|
@ -2,10 +2,9 @@
|
|||||||
# All Rights Reserved. See LICENSE file for details.
|
# All Rights Reserved. See LICENSE file for details.
|
||||||
|
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from json import dumps
|
|
||||||
from hashlib import md5
|
|
||||||
|
|
||||||
from tfw.networking.event_handlers import ServerConnector
|
from tfw.networking.event_handlers import ServerConnector
|
||||||
|
from tfw.crypto import message_checksum
|
||||||
from tfw.config.logs import logging
|
from tfw.config.logs import logging
|
||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
@ -152,7 +151,7 @@ class BroadcastingEventHandler(EventHandlerBase, ABC):
|
|||||||
self.own_message_hashes = []
|
self.own_message_hashes = []
|
||||||
|
|
||||||
def event_handler_callback(self, message):
|
def event_handler_callback(self, message):
|
||||||
message_hash = self.hash_message(message)
|
message_hash = message_checksum(message)
|
||||||
|
|
||||||
if message_hash in self.own_message_hashes:
|
if message_hash in self.own_message_hashes:
|
||||||
self.own_message_hashes.remove(message_hash)
|
self.own_message_hashes.remove(message_hash)
|
||||||
@ -160,10 +159,5 @@ class BroadcastingEventHandler(EventHandlerBase, ABC):
|
|||||||
|
|
||||||
response = self.dispatch_handling(message)
|
response = self.dispatch_handling(message)
|
||||||
if response:
|
if response:
|
||||||
self.own_message_hashes.append(self.hash_message(response))
|
self.own_message_hashes.append(message_checksum(response))
|
||||||
self.server_connector.broadcast(response)
|
self.server_connector.broadcast(response)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def hash_message(message):
|
|
||||||
message_bytes = dumps(message, sort_keys=True).encode()
|
|
||||||
return md5(message_bytes).hexdigest()
|
|
||||||
|
Loading…
Reference in New Issue
Block a user