aboutsummaryrefslogtreecommitdiff
path: root/research/sample-data/ip-butt-big-extract/testers/checksum_analyzer.py
blob: 3760727a2e3d0c3665b52b1dd4b5ce61e16c99aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
import sys


def compute_checksum(data: bytes) -> int:
    # PSA checksum excludes first 4 bytes ("MEL\0")
    relevant = data[4:]
    total = sum(relevant)
    folded = (total & 0xFF) + (total >> 8)
    return (~folded) & 0xFF


def interactive_mode():
    print("PSA Checksum Generator (Melody 0x3001 packets)")
    print("Paste the hex payload WITHOUT the checksum byte. Type 'exit' to quit.")
    while True:
        user_input = input("Payload> ").strip()
        if user_input.lower() in ("exit", "quit"):
            break
        try:
            data = bytes.fromhex(user_input)
            checksum = compute_checksum(data)
            completed = data + bytes([checksum])
            print("Checksum: {:02X}".format(checksum))
            print("Full Packet: {}".format(completed.hex()))
        except ValueError:
            print("Invalid hex input. Try again.")


if __name__ == "__main__":
    if len(sys.argv) == 2 and sys.argv[1] == "--batch":
        print("Batch mode not implemented. Run without arguments for interactive mode.")
    else:
        interactive_mode()