#!/usr/bin/env python3 """Encrypt every hidden problem implementation into a .dat file. For each .py in ../T2_UNSW_solution/ it writes an ENCRYPTED .dat in this project's root. The cipher is a SHA-256 counter-mode keystream (no third-party deps); the browser decrypts it with Web Crypto using the same passphrase (see PASSPHRASE in index.html). Usage: python3 build_blackbox.py NOTE: this is obfuscation that raises the bar, not real secrecy -- the passphrase ships inside index.html, so a determined reader can still recover the source. For true protection the implementation must live on a server. Deploy only the .dat files (and index.html). The plaintext source lives in ../T2_UNSW_solution/ -- a SIBLING of this project directory, not nested inside it -- because this whole project is served wholesale as a static site (no server-side access control), so anything nested inside it is effectively public the moment it ships, link or no link. Never move that directory back inside T2_UNSW/. """ import base64 import hashlib import pathlib # Must match PASSPHRASE in index.html exactly. PASSPHRASE = b"t2-unsw-bbt-v1-rotate-me" def _keystream_block(key: bytes, counter: int) -> bytes: return hashlib.sha256(key + counter.to_bytes(8, "big")).digest() def encrypt(data: bytes, passphrase: bytes) -> bytes: key = hashlib.sha256(passphrase).digest() out = bytearray(len(data)) for off in range(len(data)): ks = _keystream_block(key, off // 32) out[off] = data[off] ^ ks[off % 32] return bytes(out) ROOT = pathlib.Path(__file__).parent src_dir = ROOT.parent / "T2_UNSW_solution" count = 0 for py in sorted(src_dir.glob("*.py")): plain = py.read_text(encoding="utf-8").encode("utf-8") cipher = encrypt(plain, PASSPHRASE) encoded = base64.b64encode(cipher).decode("ascii") out = ROOT / (py.stem + ".dat") out.write_text(encoded, encoding="utf-8") # self-check: round-trip back to plaintext assert encrypt(cipher, PASSPHRASE) == plain, f"round-trip failed for {py.name}" print(f" {py.name:14} -> {out.name:10} ({len(encoded)} chars, encrypted)") count += 1 print(f"OK: encrypted {count} problem file(s).")