getRandomBytes()function
Get cryptographically-random bytes from the platform crypto API.
getRandomBytes(length: number): Bytes
Secure password hashing and verification using PBKDF2-SHA-512, plus a helper for generating cryptographically random bytes. Uses the Web Crypto API so it works in browsers, Node.js, and edge runtimes without additional dependencies.
hashPassword() generates a fresh random salt each call — two hashes of the same password will never match each other directly.salt$iterations$hash (all base64url-encoded), roughly 128 characters long.ValueError.verifyPassword() is constant-time to prevent timing attacks.import { hashPassword, verifyPassword } from "shelving/util";
// On sign-up — store the returned string in your database.
const stored = await hashPassword("correct-horse-battery");
// On sign-in — compare the plain-text password against the stored hash.
const ok = await verifyPassword("correct-horse-battery", stored); // true
const bad = await verifyPassword("wrong-password", stored); // falseimport { getRandomBytes } from "shelving/util";
const token = getRandomBytes(32); // Uint8Array of 32 random bytesGet cryptographically-random bytes from the platform crypto API.
getRandomBytes(length: number): Bytes
Hash a password using PBKDF2, generating a new salt, and return the combined salt$iterations$hash string.
hashPassword(password: string, iterations = ITERATIONS): Promise<string>
Verify a password against a stored salt$iterations$hash string using PBKDF2.
verifyPassword(password: string, hash: string): Promise<boolean>