#!/usr/bin/env python3 """Generate src/hmac_key.h with a random 32-byte HMAC-SHA256 key. Usage: python3 tools/generate_hmac_key.py This creates src/hmac_key.h (gitignored) and prints the hex key to stdout. The hex key should be entered in the Android app's Settings screen. """ import os import sys import binascii KEY_LENGTH = 32 OUTPUT_FILE = os.path.join("src", "hmac_key.h") def generate(): key = os.urandom(KEY_LENGTH) hex_key = binascii.hexlify(key).decode().upper() header = f"""#ifndef HMAC_KEY_H #define HMAC_KEY_H // AUTO-GENERATED by tools/generate_hmac_key.py // Do NOT commit this file. Do NOT edit by hand. // To regenerate: python3 tools/generate_hmac_key.py const uint8_t HMAC_KEY[{KEY_LENGTH}] = {{ {', '.join(f'0x{b:02x}' for b in key)} }}; #endif // HMAC_KEY_H """ os.makedirs("src", exist_ok=True) with open(OUTPUT_FILE, "w") as f: f.write(header) print(f"HMAC key written to {OUTPUT_FILE}") print(f"Enter this key in the Android app Settings:") print(f" {hex_key}") print() print(f"ESP serial output after boot will show the same key.") if __name__ == "__main__": if os.path.exists(OUTPUT_FILE): overwrite = input(f"{OUTPUT_FILE} already exists. Overwrite? [y/N] ") if overwrite.lower() != 'y': print("Aborted.") sys.exit(0) generate()