Import
What it does
CryptoUtils exposes static methods for common cryptographic and encoding operations: secure token generation, UUID v4, Base64 encoding/decoding (standard and URL-safe), hashing (simple and SubtleCrypto-based), HMAC, AES-256-GCM encryption/decryption, and constant-time string comparison.
Methods
generateToken(length?)
generateUUID()
base64Encode(str)
base64Decode(str)
base64UrlEncode(str)
+/ with -_, strips =).
base64UrlDecode(str)
simpleHash(str)
hash(str, algorithm?)
hmac(message, secret, algorithm?)
verifyHash(str, hash, algorithm?)
str and compares it to hash. Returns true if they match.
encrypt(str, key)
decrypt(encrypted, key)
encrypt.
randomBytes(length)
Uint8Array of cryptographically random bytes.
constantTimeCompare(a, b)
true if they are equal.
Examples
Webhook signature verification
Token-based authentication
Encrypt sensitive data at rest
Async methods (
hash, hmac, encrypt, decrypt, verifyHash) use the Web Crypto API (SubtleCrypto) and require a secure context (HTTPS) in browsers.