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.
This commit is contained in:
SpudGunMan
2025-11-06 11:49:57 -08:00
parent f6f1b748dc
commit 39c9864682
2 changed files with 9 additions and 9 deletions

View File

@@ -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

View File

@@ -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"""