File tree Expand file tree Collapse file tree 2 files changed +22
-12
lines changed
Expand file tree Collapse file tree 2 files changed +22
-12
lines changed Original file line number Diff line number Diff line change 1616 pskToString ,
1717 stripnl ,
1818 message_to_json ,
19- channel_hash ,
19+ generate_hash ,
2020)
2121
2222logger = logging .getLogger (__name__ )
2323
24- def generate_hash (name : str , key : str ) -> int :
25- """generate the channel number by hashing the channel name and psk"""
26- if key == "AQ==" :
27- key = "1PG7OiApB1nwvP+rz05pAQ=="
28- replaced_key = key .replace ("-" , "+" ).replace ("_" , "/" )
29- key_bytes = base64 .b64decode (replaced_key .encode ("utf-8" ))
30- h_name = channel_hash (bytes (name , "utf-8" ))
31- h_key = channel_hash (key_bytes )
32- result : int = h_name ^ h_key
33- return result
34-
3524class Node :
3625 """A model of a (local or remote) node in the mesh
3726
Original file line number Diff line number Diff line change @@ -372,6 +372,27 @@ def channel_hash(data: bytes) -> int:
372372 result ^= char
373373 return
374374
375+ def generate_hash (name , key ) -> int :
376+ """generate the channel number by hashing the channel name and psk (accepts str or bytes for both)"""
377+ # Handle key as str or bytes
378+ if isinstance (key , bytes ):
379+ key = key .decode ("utf-8" )
380+ if key == "AQ==" :
381+ key = "1PG7OiApB1nwvP+rz05pAQ=="
382+ replaced_key = key .replace ("-" , "+" ).replace ("_" , "/" )
383+ key_bytes = base64 .b64decode (replaced_key .encode ("utf-8" ))
384+
385+ # Handle name as str or bytes
386+ if isinstance (name , bytes ):
387+ name_bytes = name
388+ else :
389+ name_bytes = name .encode ("utf-8" )
390+
391+ h_name = channel_hash (name_bytes )
392+ h_key = channel_hash (key_bytes )
393+ result : int = h_name ^ h_key
394+ return result
395+
375396def hexstr (barray : bytes ) -> str :
376397 """Print a string of hex digits"""
377398 return ":" .join (f"{ x :02x} " for x in barray )
You can’t perform that action at this time.
0 commit comments