Skip to content

Commit 39c9864

Browse files
committed
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.
1 parent f6f1b74 commit 39c9864

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

meshtastic/node.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@
1616
pskToString,
1717
stripnl,
1818
message_to_json,
19+
channel_hash,
1920
)
2021

2122
logger = logging.getLogger(__name__)
2223

23-
def xor_hash(data: bytes) -> int:
24-
"""Compute an XOR hash from bytes."""
25-
result = 0
26-
for char in data:
27-
result ^= char
28-
return result
29-
3024
def generate_hash(name: str, key: str) -> int:
3125
"""generate the channel number by hashing the channel name and psk"""
3226
if key == "AQ==":
3327
key = "1PG7OiApB1nwvP+rz05pAQ=="
3428
replaced_key = key.replace("-", "+").replace("_", "/")
3529
key_bytes = base64.b64decode(replaced_key.encode("utf-8"))
36-
h_name = xor_hash(bytes(name, "utf-8"))
37-
h_key = xor_hash(key_bytes)
30+
h_name = channel_hash(bytes(name, "utf-8"))
31+
h_key = channel_hash(key_bytes)
3832
result: int = h_name ^ h_key
3933
return result
4034

meshtastic/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ def remove_keys_from_dict(keys: Union[Tuple, List, Set], adict: Dict) -> Dict:
365365
remove_keys_from_dict(keys, val)
366366
return adict
367367

368+
def channel_hash(data: bytes) -> int:
369+
"""Compute an XOR hash from bytes for channel evaluation."""
370+
result = 0
371+
for char in data:
372+
result ^= char
373+
return
368374

369375
def hexstr(barray: bytes) -> str:
370376
"""Print a string of hex digits"""

0 commit comments

Comments
 (0)