-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
21 lines (16 loc) · 713 Bytes
/
utils.py
File metadata and controls
21 lines (16 loc) · 713 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import hashlib
def intToHex(number: int) -> str:
return hex(number)[2:].upper()
def strToHex(str: str) -> str:
return str.encode().hex().ljust(24, "0").upper()
#Size hex correctly (little-endian, big-endian, ...)
def size(str: str, size: int) -> str:
return str.rjust(size*2, "0").upper()
#Reverse hex bytes (03 02 01 -> 01 02 03)
def reverseHex(hex: str) -> str:
return ''.join(hex[i:i+2] for i in range(0, len(hex), 2)[::-1]).upper()
#Build the checksum of payload (double hash the payload and take the first 4 bytes)
def checksum(payload: str) -> str:
hash1 = hashlib.sha256(bytes.fromhex(payload)).digest()
hash2 = hashlib.sha256(hash1).digest()
return hash2[:4].hex().upper()