bcrypt
bcrypt is a library for hashing passwords using the bcrypt algorithm, which is specifically designed for secure password storage. Unlike general-purpose hash fu…
Installation
npm install bcrypt
yarn add bcrypt
pnpm add bcrypt
Import
import bcrypt from 'bcrypt';
Quick Example
import bcrypt from 'bcrypt';
const password = 'user-password';
const hash = await bcrypt.hash(password, 12);
const isValid = await bcrypt.compare('user-password', hash);
console.log(isValid); // trueAbout bcrypt
bcrypt is a library for hashing passwords using the bcrypt algorithm, which is specifically designed for secure password storage. Unlike general-purpose hash functions (MD5, SHA-256) that are designed to be fast, bcrypt is intentionally slow and computationally expensive, making brute-force attacks impractical. The algorithm incorporates a configurable work factor (salt rounds) that determines the computational cost — each increment doubles the time required, allowing the cost to increase as hardware improves. bcrypt automatically generates and embeds a random salt in the hash output, preventing rainbow table attacks without requiring separate salt storage. The library provides both synchronous and asynchronous APIs: bcrypt.hash(password, saltRounds) creates a hash, and bcrypt.compare(password, hash) verifies a password against a stored hash. The output is a 60-character string containing the algorithm version, cost factor, salt, and hash in a self-describing format. bcrypt uses native C++ bindings through N-API for optimal performance. For environments where native compilation is not possible, bcryptjs provides a pure JavaScript implementation with the same API. bcrypt is the most widely recommended password hashing library for Node.js applications.
Quick Facts
| Package | bcrypt |
| Category | Auth |
| Weekly Downloads | 2M+ |
| License | MIT |
| Install | npm install bcrypt |
Related Packages
Argon2 is a Node.js binding for the Argon2 password hashing algorithm, the winner of the 2015 Passwo…
Passport is the most popular authentication middleware for Node.js, providing a modular framework fo…
jsonwebtoken is the most widely used library for creating and verifying JSON Web Tokens (JWTs) in No…
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.