bcryptjs
Pure JavaScript bcrypt implementation for hashing and verifying passwords without native compilation.
Official Documentation ↗v3.0.3. Check the official docs for the latest API surface.Installation
npm install bcryptjspnpm add bcryptjsMethods
No methods match this filter.
Overview
bcryptjs is a pure JavaScript bcrypt implementation for hashing and verifying passwords without a native compilation step. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
bcrypt.hash— Hashes a password with a generated salt at the given cost factor.bcrypt.compare— Verifies a plaintext password against a stored hash using constant-time comparison.bcrypt.genSalt— Generates a salt with the requested cost so hashes can share it when needed.
Example
const bcrypt = require('bcryptjs');
const hash = await bcrypt.hash('s3cret', 12);
console.log(hash);
Notes
bcrypt ignores bytes beyond 72, so pre-hash long secrets. Cost factor governs security versus login latency and should be tuned to hardware.
Verification
Hash a password, verify the correct one returns true, and confirm a wrong password returns false.
Security notes
Quoted from the official documentation (https://github.com/dcodeIO/bcrypt.js); this is not a complete threat model.
- Note that maximum input length is not implicitly checked by the library for compatibility with the C++ binding on Node.js, but should be checked with bcrypt.truncates(password) where necessary. (https://github.com/dcodeIO/bcrypt.js)
FAQ
- Which cost factor should I use?
- Choose the highest cost that keeps login under your latency budget; 10 to 12 is a common starting point and should be revisited as hardware improves.
- Does bcrypt truncate long passwords?
- Yes, bcrypt only uses the first 72 bytes; pre-hash longer secrets with SHA-256 and base64 before hashing.
- Why use bcryptjs instead of native bcrypt?
- bcryptjs needs no native build step and is easy to deploy, at the cost of lower throughput than the native module.
Report a problem with this API docOpens a pre-filled issue on GitHub.