API Reference

bcrypt

Node.js library for hashing and verifying passwords with the bcrypt adaptive hashing algorithm.

Official Documentation ↗
CompatibilityThis documentation covers version v6.0.0. Check the official docs for the latest API surface.

Installation

npmnpm install bcrypt
pnpmpnpm add bcrypt
yarnyarn add bcrypt

Methods

bcrypt is a Node.js library for hashing passwords using the bcrypt algorithm. It provides salt generation, hashing, and comparison with an adaptive cost factor that slows brute-force attacks.

Key Features

  • Adaptive cost: Tunable work factor to resist hardware attacks
  • Salt handling: Auto-generated or explicit salts
  • Async and sync: Non-blocking and blocking APIs
  • Safe compare: Constant-time comparison to prevent timing attacks
  • Round inspection: Read cost factor from hashes

Errors

The following points are quoted verbatim from the official documentation (https://github.com/kelektiv/node.bcrypt.js). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted from the official documentation (https://github.com/kelektiv/node.bcrypt.js); this is not a complete threat model.

FAQ

What cost factor should I use?
Choose the highest factor that keeps hashing under your latency budget, typically 10-12 for web apps. Increase it over time as hardware improves. Each increment roughly doubles the work factor.
Should I use async or sync bcrypt methods?
Prefer async methods (hash, compare) to avoid blocking the Node.js event loop during the intentionally slow hashing. Use sync variants only in scripts or startup code where blocking is acceptable.
How is bcrypt different from argon2 or scrypt?
bcrypt is battle-tested and widely supported but lacks memory hardness, making it vulnerable to GPU attacks. argon2 is the modern standard with memory and parallelism resistance, recommended for new systems where available.