From 39c9864682a60e8d6729c656a815d8ba43b9a72b Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Thu, 6 Nov 2025 11:49:57 -0800 Subject: [PATCH] move and rename hash function this function, xor_hash, and a variable for the default key (as bytes, I think, rather than the base64 version) really all belong in meshtastic.util rather than here. There's multiple forms of hashing in firmware so this should be named to denote that, perhaps channel_hash. If we later want to add the frequency-slot-style hash, better if it's distinguished better from the start. --- meshtastic/node.py | 12 +++--------- meshtastic/util.py | 6 ++++++ 2 files changed, 9 insertions(+), 9 deletions(-) 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"""