#!/usr/bin/env python3
"""Reference vector generator for the pht_ API token construction.

Independent of RustCrypto — uses stdlib base64 + hashlib so the
Rust implementation is checked against an external reference.

Construction:
    raw    = 32 bytes from OsRng  (fixed here for test vectors)
    token  = "pht_" + base64url_no_pad(raw)          # 47 chars
    hash   = SHA-256(token.encode("ascii"))           # 32 bytes
"""

import base64
import hashlib

def gen(raw: bytes) -> tuple[str, bytes]:
    assert len(raw) == 32
    encoded = base64.urlsafe_b64encode(raw).rstrip(b"=").decode("ascii")
    assert len(encoded) == 43
    token = "pht_" + encoded
    assert len(token) == 47
    digest = hashlib.sha256(token.encode("ascii")).digest()
    return token, digest

def emit(label, raw):
    token, digest = gen(raw)
    print(f"# {label}")
    print(f"#   raw          = {raw.hex()}")
    print(f"#   token        = {token!r}")
    print(f"#   sha256(token)= {digest.hex()}")
    print()

if __name__ == "__main__":
    print("# generated by gen_pht.py (stdlib base64 + hashlib)\n")

    emit("vector 1 — sequential 0x00..0x1f",
         bytes(range(0x00, 0x20)))

    emit("vector 2 — reverse-sequential 0xff..0xe0",
         bytes(range(0xff, 0xdf, -1)))

    emit("vector 3 — alternating 0xa5/0x5a pattern",
         bytes([0xa5 if i % 2 == 0 else 0x5a for i in range(32)]))
