diff options
Diffstat (limited to 'executables/hex_checksum-v2.py')
-rw-r--r-- | executables/hex_checksum-v2.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/executables/hex_checksum-v2.py b/executables/hex_checksum-v2.py new file mode 100644 index 0000000..71409f1 --- /dev/null +++ b/executables/hex_checksum-v2.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +import sys + +def compute_psa_checksum(data: bytes) -> bytes: + var_e = 0x0000 # Correct seed from firmware logic + for i in range(len(data)): + var_e ^= (data[i] + i) & 0xFFFF + return var_e.to_bytes(2, 'big') # 2-byte checksum, big-endian + +def add_checksum(hexstring: str) -> str: + try: + data = bytes.fromhex(hexstring.strip()) + except ValueError: + return "Invalid hex input!" + checksum = compute_psa_checksum(data) + return (data + checksum).hex() + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Usage: ./hex_checksum.py <hexstring_without_checksum>") + sys.exit(1) + result = add_checksum(sys.argv[1]) + print(result) |