Generate pads, encrypt, decrypt, detect tampering — all in one page. Nothing leaves your browser.
crypto.getRandomValues and (2) by refusing short pads. The pad is split:
the first N bytes encrypt an N-byte message, and a separate 32-byte
segment (taken from after the message bytes) authenticates it with HMAC-SHA256. So you
need message length + 32 bytes per message. It cannot enforce (3) or
(4) — that's on you.
Encrypting an N-byte message consumes N + 32 bytes of pad:
After XORing, the tool computes HMAC-SHA256(hmac_key, ciphertext) and
appends the 32-byte tag to the ciphertext. The output is
ciphertext || tag (N + 32 bytes total), shown as hex or Base64.
On decryption, the recipient pulls the tag off the end, recomputes the HMAC over the ciphertext with the same pad bytes, and compares. If the tag doesn't match, the tool refuses to decrypt and tells you the message was altered or the wrong pad was used. This is critical: an unauthenticated OTP lets an attacker flip specific plaintext bits by flipping ciphertext bits. With the tag, any modification is caught.
Why a separate HMAC key? Reusing the same pad bytes for both XOR and HMAC would violate the one-time property — the pad would be doing two jobs. Taking the HMAC key from later in the pad keeps the two roles independent, and those bytes are never used for anything else.
Comparison is constant-time (via crypto.subtle.verify), so timing
side-channels can't leak tag bytes.