bcrypt.js Password Hashing

3.0.3 · active · verified Sat Apr 18

bcrypt.js is an optimized implementation of the bcrypt password hashing function in pure JavaScript, providing zero dependencies and full TypeScript support. Compatible with the C++ bcrypt binding, it is currently stable at version 3.0.3 and is actively maintained with releases as needed to address bugs and modernize the codebase.

Common errors

Warnings

Install

Imports

Quickstart

Hashes a password with an automatically generated salt (10 rounds) and then demonstrates how to compare a password against the stored hash using the asynchronous API.

import bcrypt from "bcryptjs";

async function main() {
  const password = "mySecretPassword123";
  // Auto-generate a salt with 10 rounds and hash the password
  const hash = await bcrypt.hash(password, 10);
  console.log("Hashed password:", hash);

  // Compare a password against the stored hash
  const isMatch = await bcrypt.compare(password, hash);
  console.log("Password matches:", isMatch); // true

  const notMatch = await bcrypt.compare("wrongPassword", hash);
  console.log("Wrong password matches:", notMatch); // false
}
main();

view raw JSON →