diff --git a/meshtastic/node.py b/meshtastic/node.py index 718b81f..5208581 100644 --- a/meshtastic/node.py +++ b/meshtastic/node.py @@ -16,25 +16,19 @@ from meshtastic.util import ( pskToString, stripnl, message_to_json, + channel_hash, ) logger = logging.getLogger(__name__) -def xor_hash(data: bytes) -> int: - """Compute an XOR hash from bytes.""" - result = 0 - for char in data: - result ^= char - return result - def generate_hash(name: str, key: str) -> int: """generate the channel number by hashing the channel name and psk""" if key == "AQ==": key = "1PG7OiApB1nwvP+rz05pAQ==" replaced_key = key.replace("-", "+").replace("_", "/") key_bytes = base64.b64decode(replaced_key.encode("utf-8")) - h_name = xor_hash(bytes(name, "utf-8")) - h_key = xor_hash(key_bytes) + h_name = channel_hash(bytes(name, "utf-8")) + h_key = channel_hash(key_bytes) result: int = h_name ^ h_key return result diff --git a/meshtastic/util.py b/meshtastic/util.py index 243dfe9..de09e5c 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -365,6 +365,12 @@ def remove_keys_from_dict(keys: Union[Tuple, List, Set], adict: Dict) -> Dict: remove_keys_from_dict(keys, val) return adict +def channel_hash(data: bytes) -> int: + """Compute an XOR hash from bytes for channel evaluation.""" + result = 0 + for char in data: + result ^= char + return def hexstr(barray: bytes) -> str: """Print a string of hex digits"""