generate_hash to util

This commit is contained in:
SpudGunMan
2025-11-06 12:00:16 -08:00
parent 39c9864682
commit 471e3ce145
2 changed files with 22 additions and 12 deletions

View File

@@ -16,22 +16,11 @@ from meshtastic.util import (
pskToString,
stripnl,
message_to_json,
channel_hash,
generate_hash,
)
logger = logging.getLogger(__name__)
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 = channel_hash(bytes(name, "utf-8"))
h_key = channel_hash(key_bytes)
result: int = h_name ^ h_key
return result
class Node:
"""A model of a (local or remote) node in the mesh

View File

@@ -372,6 +372,27 @@ def channel_hash(data: bytes) -> int:
result ^= char
return
def generate_hash(name, key) -> int:
"""generate the channel number by hashing the channel name and psk (accepts str or bytes for both)"""
# Handle key as str or bytes
if isinstance(key, bytes):
key = key.decode("utf-8")
if key == "AQ==":
key = "1PG7OiApB1nwvP+rz05pAQ=="
replaced_key = key.replace("-", "+").replace("_", "/")
key_bytes = base64.b64decode(replaced_key.encode("utf-8"))
# Handle name as str or bytes
if isinstance(name, bytes):
name_bytes = name
else:
name_bytes = name.encode("utf-8")
h_name = channel_hash(name_bytes)
h_key = channel_hash(key_bytes)
result: int = h_name ^ h_key
return result
def hexstr(barray: bytes) -> str:
"""Print a string of hex digits"""
return ":".join(f"{x:02x}" for x in barray)