variable
password
Hash and verify passwords using argon2 or bcrypt. The default is argon2. Password hashing functions are necessarily slow, so the asynchronous functions run in a worker thread.
The underlying implementation of these functions is provided by the rust-argon2 and bcrypt Rust crates.
Example with argon2
import {password} from "bun";
const hash = await password.hash("hello world");
const verify = await password.verify("hello world", hash);
console.log(verify); // trueExample with bcrypt
import {password} from "bun";
const hash = await password.hash("hello world", "bcrypt");
// algorithm is optional, will be inferred from the hash if not specified
const verify = await password.verify("hello world", hash, "bcrypt");
console.log(verify); // trueAsynchronously hash a password using argon2 or bcrypt. The default is argon2.
The password to hash
If empty, this function throws an error. It is usually a programming mistake to hash an empty password.
When using bcrypt, passwords longer than 72 bytes are hashed with SHA-512 before being passed to bcrypt
A promise that resolves to the hashed password
Example with argon2
import {password} from "bun";
const hash = await password.hash("hello world");
console.log(hash); // $argon2id$v=1...
const verify = await password.verify("hello world", hash);Example with bcrypt
import {password} from "bun";
const hash = await password.hash("hello world", "bcrypt");
console.log(hash); // $2b$10$...
const verify = await password.verify("hello world", hash);Synchronously hash a password using argon2 or bcrypt. The default is argon2.
Warning: password hashing is slow. Prefer Bun.password.hash, which runs in a worker thread.
The underlying implementation of these functions is provided by the rust-argon2 and bcrypt Rust crates.
The password to hash
If empty, this function throws an error. It is usually a programming mistake to hash an empty password.
When using bcrypt, passwords longer than 72 bytes are hashed with SHA-512 before being passed to bcrypt
Example with argon2
import {password} from "bun";
const hash = await password.hashSync("hello world");
const verify = await password.verifySync("hello world", hash);
console.log(verify); // trueExample with bcrypt
import {password} from "bun";
const hash = await password.hashSync("hello world", "bcrypt");
// algorithm is optional, will be inferred from the hash if not specified
const verify = await password.verifySync("hello world", hash, "bcrypt");
console.log(verify); // trueVerify a password against a previously hashed password.
The password to verify.
If empty, always returns false
Previously hashed password. If empty, always returns false
If not specified, the algorithm is inferred from the hash.
If specified and the algorithm does not match the hash, this function throws an error.
true if the password matches, false otherwise
import {password} from "bun";
await password.verify("hey", "$argon2id$v=19$m=65536,t=2,p=1$ddbcyBcbAcagei7wSkZFiouX6TqnUQHmTyS5mxGCzeM$+3OIaFatZ3n6LtMhUlfWbgJyNp7h8/oIsLK+LzZO+WI");
// trueSynchronously verify a password against a previously hashed password using argon2 or bcrypt. The default is argon2.
Warning: password hashing is slow. Prefer Bun.password.verify, which runs in a worker thread.
The underlying implementation of these functions is provided by the rust-argon2 and bcrypt Rust crates.
The password to verify.
The hash to verify against.
If not specified, the algorithm is inferred from the hash.
Example with argon2
import {password} from "bun";
const hash = await password.hashSync("hello world");
const verify = await password.verifySync("hello world", hash);
console.log(verify); // trueExample with bcrypt
import {password} from "bun";
const hash = await password.hashSync("hello world", "bcrypt");
// algorithm is optional, will be inferred from the hash if not specified
const verify = await password.verifySync("hello world", hash, "bcrypt");
console.log(verify); // true