mirror of
https://github.com/avatao-content/baseimage-tutorial-framework
synced 2024-12-23 02:31:56 +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 base64 import b64encode, b64decode
|
||||
from copy import deepcopy
|
||||
from hashlib import md5
|
||||
|
||||
from cryptography.hazmat.backends import default_backend
|
||||
from cryptography.hazmat.primitives.hashes import SHA256
|
||||
@ -13,6 +14,10 @@ from cryptography.exceptions import InvalidSignature
|
||||
from tfw.networking import message_bytes
|
||||
|
||||
|
||||
def message_checksum(message):
|
||||
return md5(message_bytes(message)).hexdigest()
|
||||
|
||||
|
||||
def sign_message(key, message):
|
||||
signature = HMAC(key, message_bytes(message)).signature
|
||||
message['signature'] = b64encode(signature).decode()
|
||||
|
@ -2,10 +2,9 @@
|
||||
# All Rights Reserved. See LICENSE file for details.
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from json import dumps
|
||||
from hashlib import md5
|
||||
|
||||
from tfw.networking.event_handlers import ServerConnector
|
||||
from tfw.crypto import message_checksum
|
||||
from tfw.config.logs import logging
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -82,7 +81,7 @@ class EventHandlerBase(ABC):
|
||||
Note that you can subscribe to the same key several times in which
|
||||
case you will need to unsubscribe multiple times in order to stop
|
||||
receiving events.
|
||||
|
||||
|
||||
:param keys: list of keys to subscribe to
|
||||
"""
|
||||
for key in keys:
|
||||
@ -152,7 +151,7 @@ class BroadcastingEventHandler(EventHandlerBase, ABC):
|
||||
self.own_message_hashes = []
|
||||
|
||||
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:
|
||||
self.own_message_hashes.remove(message_hash)
|
||||
@ -160,10 +159,5 @@ class BroadcastingEventHandler(EventHandlerBase, ABC):
|
||||
|
||||
response = self.dispatch_handling(message)
|
||||
if response:
|
||||
self.own_message_hashes.append(self.hash_message(response))
|
||||
self.own_message_hashes.append(message_checksum(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