checksum_analyzer.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. import sys
  3. def compute_checksum(data: bytes) -> int:
  4. # PSA checksum excludes first 4 bytes ("MEL\0")
  5. relevant = data[4:]
  6. total = sum(relevant)
  7. folded = (total & 0xFF) + (total >> 8)
  8. return (~folded) & 0xFF
  9. def interactive_mode():
  10. print("PSA Checksum Generator (Melody 0x3001 packets)")
  11. print("Paste the hex payload WITHOUT the checksum byte. Type 'exit' to quit.")
  12. while True:
  13. user_input = input("Payload> ").strip()
  14. if user_input.lower() in ("exit", "quit"):
  15. break
  16. try:
  17. data = bytes.fromhex(user_input)
  18. checksum = compute_checksum(data)
  19. completed = data + bytes([checksum])
  20. print("Checksum: {:02X}".format(checksum))
  21. print("Full Packet: {}".format(completed.hex()))
  22. except ValueError:
  23. print("Invalid hex input. Try again.")
  24. if __name__ == "__main__":
  25. if len(sys.argv) == 2 and sys.argv[1] == "--batch":
  26. print("Batch mode not implemented. Run without arguments for interactive mode.")
  27. else:
  28. interactive_mode()