Encryption, hashing, CSRF protection, and secure operations.

Uses the Web Crypto API for:

  • Hybrid RSA-OAEP + AES-256-GCM encryption for PII at rest
  • HMAC-SHA256 for webhooks and CSRF tokens
  • Argon2-style password hashing
  • Constant-time comparison for timing-safe checks

Functions

f
clearEncryptionKeyCache

Clear all crypto caches (encryption key, HMAC key, private key, hybrid decrypt LRU) Called on key rotation and during test setup/teardown

f
computeHmacSha256

Compute HMAC-SHA256 using Web Crypto API, returning raw ArrayBuffer

f
computeTicketTokenIndex

Compute ticket token index using HMAC for blind lookups Similar to slug_index for events - allows lookup without decrypting

f
constantTimeEqual

Constant-time string comparison to prevent timing attacks Always iterates over the longer string and XORs the lengths so that different-length inputs don't leak via an early return.

f
decrypt

Decrypt a string value encrypted with encrypt() Expects format: enc:1:$base64iv:$base64ciphertext

f
decryptAttendeePII

Decrypt attendee PII using the private key Used in admin views after obtaining private key from session

f
decryptBytes

Decrypt binary data encrypted with encryptBytes().

f
decryptWithKey

Decrypt data with a symmetric key

f
deriveKEK

Derive a Key Encryption Key (KEK) from password hash and DB_ENCRYPTION_KEY Uses PBKDF2 with the password hash as input and DB_ENCRYPTION_KEY as salt

f
encrypt

Encrypt a string value using AES-256-GCM via Web Crypto API Returns format: enc:1:$base64iv:$base64ciphertext Note: ciphertext includes auth tag appended (Web Crypto API does this automatically)

f
encryptAttendeePII

Encrypt attendee PII using the public key from settings This can be called without authentication (for public ticket forms)

f
encryptBytes

Encrypt binary data with AES-256-GCM. Delegates to encrypt() via base64 encoding for maximum code reuse. Used for encrypting image files before CDN storage.

f
encryptWithKey

Encrypt data with a symmetric key (for wrapping private key with DATA_KEY)

f
generateDataKey

Generate a random 256-bit symmetric key for data encryption

f
generateKeyPair

Generate an RSA key pair for asymmetric encryption Returns { publicKey, privateKey } as exportable JWK strings

f
generateSecureToken

Generate a cryptographically secure random token Uses Web Crypto API getRandomValues

f
generateTicketToken

Generate a 5-byte uppercase hex ticket token for public ticket URLs

f
getCurrentCsrfToken

Get the most recently generated CSRF token (for synchronous JSX rendering)

f
getPrivateKeyFromSession

Derive the private key from session credentials Used to decrypt attendee PII in admin views Results are cached per session token for 10 seconds

f
hashPassword

Hash a password using PBKDF2 Returns format: pbkdf2:iterations:$base64salt:$base64hash

f
hashSessionToken

Hash a session token using SHA-256 Used to store session lookups without exposing the actual token

f
hmacHash

HMAC-SHA256 hash using DB_ENCRYPTION_KEY Used for blind indexes and hashing limited keyspace values Returns deterministic output for same input (unlike encrypt)

f
hmacToBase64

Convert ArrayBuffer to base64 string

f
hmacToHex

Convert ArrayBuffer to hex string

f
hybridDecrypt

Decrypt data using hybrid encryption Expects format: hyb:1:$base64WrappedKey:$base64iv:$base64ciphertext Results are cached in a bounded LRU (ciphertext -> plaintext)

f
hybridEncrypt

Encrypt data using hybrid encryption (RSA + AES)

f
importPrivateKey

Import a private key from JWK string

f
importPublicKey

Import a public key from JWK string

f
isSignedCsrfToken

Check whether a token uses the signed format

f
secureCompare

Constant-time string comparison to prevent timing attacks

f
signCsrfToken

Create a signed CSRF token: s1.{timestamp}.{nonce}.{hmac}

f
unwrapKey

Unwrap a symmetric key Expects format: wk:1:$base64iv:$base64wrapped

f
unwrapKeyWithToken

Unwrap a key using a session token

f
validateEncryptionKey

Validate encryption key is present and valid Call this on startup to fail fast if key is missing

f
verifyPassword

Verify a password against a hash Uses constant-time comparison to prevent timing attacks

f
verifySignedCsrfToken

Verify a signed CSRF token's signature and expiry

f
wrapKey

Wrap a symmetric key with another key using AES-GCM Returns format: wk:1:$base64iv:$base64wrapped

f
wrapKeyWithToken

Wrap a key using a session token (derives a wrapping key from the token)

Variables

v
CSRF_INVALID_FORM_MESSAGE

Default message for invalid/expired CSRF form submissions