Installations
npm install bcrypt
Score
55.1
Supply Chain
98
Quality
79
Maintenance
100
Vulnerability
98.2
License
Developer
kelektiv
Developer Guide
Module System
CommonJS
Min. Node Version
>= 10.0.0
Typescript Support
No
Node Version
20.5.0
NPM Version
9.8.0
Statistics
7,503 Stars
599 Commits
518 Forks
63 Watching
17 Branches
79 Contributors
Updated on 29 Nov 2024
Languages
C++ (50.98%)
JavaScript (38.19%)
C (5.86%)
Dockerfile (1.84%)
Python (1.62%)
Shell (1.27%)
Makefile (0.25%)
Total Downloads
Cumulative downloads
Total Downloads
287,245,525
Last day
-13.6%
291,256
Compared to previous day
Last week
-2.3%
1,890,305
Compared to previous week
Last month
3.6%
8,019,358
Compared to previous month
Last year
42.3%
85,583,383
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Dev Dependencies
1
node.bcrypt.js
A library to help you hash passwords.
You can read about bcrypt in Wikipedia as well as in the following article: How To Safely Store A Password
If You Are Submitting Bugs or Issues
Please verify that the NodeJS version you are using is a stable version; Unstable versions are currently not supported and issues created while using an unstable version will be closed.
If you are on a stable version of NodeJS, please provide a sufficient code snippet or log files for installation issues. The code snippet does not require you to include confidential information. However, it must provide enough information so the problem can be replicable, or it may be closed without an explanation.
Version Compatibility
Please upgrade to atleast v5.0.0 to avoid security issues mentioned below.
Node Version | Bcrypt Version |
---|---|
0.4 | <= 0.4 |
0.6, 0.8, 0.10 | >= 0.5 |
0.11 | >= 0.8 |
4 | <= 2.1.0 |
8 | >= 1.0.3 < 4.0.0 |
10, 11 | >= 3 |
12 onwards | >= 3.0.6 |
node-gyp
only works with stable/released versions of node. Since the bcrypt
module uses node-gyp
to build and install, you'll need a stable version of node to use bcrypt. If you do not, you'll likely see an error that starts with:
gyp ERR! stack Error: "pre" versions of node cannot be installed, use the --nodedir flag instead
Security Issues And Concerns
Per bcrypt implementation, only the first 72 bytes of a string are used. Any extra bytes are ignored when matching passwords. Note that this is not the first 72 characters. It is possible for a string to contain less than 72 characters, while taking up more than 72 bytes (e.g. a UTF-8 encoded string containing emojis). If a string is provided, it will be encoded using UTF-8.
As should be the case with any security tool, anyone using this library should scrutinise it. If you find or suspect an issue with the code, please bring it to the maintainers' attention. We will spend some time ensuring that this library is as secure as possible.
Here is a list of BCrypt-related security issues/concerns that have come up over the years.
- An issue with passwords was found with a version of the Blowfish algorithm developed for John the Ripper. This is not present in the OpenBSD version and is thus not a problem for this module. HT zooko.
- Versions
< 5.0.0
suffer from bcrypt wrap-around bug and will truncate passwords >= 255 characters leading to severely weakened passwords. Please upgrade at earliest. See this wiki page for more details. - Versions
< 5.0.0
do not handle NUL characters inside passwords properly leading to all subsequent characters being dropped and thus resulting in severely weakened passwords. Please upgrade at earliest. See this wiki page for more details.
Compatibility Note
This library supports $2a$
and $2b$
prefix bcrypt hashes. $2x$
and $2y$
hashes are specific to bcrypt implementation developed for John the Ripper. In theory, they should be compatible with $2b$
prefix.
Compatibility with hashes generated by other languages is not 100% guaranteed due to difference in character encodings. However, it should not be an issue for most cases.
Migrating from v1.0.x
Hashes generated in earlier version of bcrypt
remain 100% supported in v2.x.x
and later versions. In most cases, the migration should be a bump in the package.json
.
Hashes generated in v2.x.x
using the defaults parameters will not work in earlier versions.
Dependencies
- NodeJS
node-gyp
- Please check the dependencies for this tool at: https://github.com/nodejs/node-gyp
- Windows users will need the options for c# and c++ installed with their visual studio instance.
- Python 2.x/3.x
OpenSSL
- This is only required to build thebcrypt
project if you are using versions <= 0.7.7. Otherwise, we're using the builtin node crypto bindings for seed data (which use the same OpenSSL code paths we were, but don't have the external dependency).
Install via NPM
npm install bcrypt
Note: OS X users using Xcode 4.3.1 or above may need to run the following command in their terminal prior to installing if errors occur regarding xcodebuild: sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
Pre-built binaries for various NodeJS versions are made available on a best-effort basis.
Only the current stable and supported LTS releases are actively tested against.
There may be an interval between the release of the module and the availabilty of the compiled modules.
Currently, we have pre-built binaries that support the following platforms:
- Windows x32 and x64
- Linux x64 (GlibC and musl)
- macOS
If you face an error like this:
node-pre-gyp ERR! Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.2/bcrypt_lib-v1.0.2-node-v48-linux-x64.tar.gz
make sure you have the appropriate dependencies installed and configured for your platform. You can find installation instructions for the dependencies for some common platforms in this page.
Usage
async (recommended)
1const bcrypt = require('bcrypt'); 2const saltRounds = 10; 3const myPlaintextPassword = 's0/\/\P4$$w0rD'; 4const someOtherPlaintextPassword = 'not_bacon';
To hash a password:
Technique 1 (generate a salt and hash on separate function calls):
1bcrypt.genSalt(saltRounds, function(err, salt) { 2 bcrypt.hash(myPlaintextPassword, salt, function(err, hash) { 3 // Store hash in your password DB. 4 }); 5});
Technique 2 (auto-gen a salt and hash):
1bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) { 2 // Store hash in your password DB. 3});
Note that both techniques achieve the same end-result.
To check a password:
1// Load hash from your password DB. 2bcrypt.compare(myPlaintextPassword, hash, function(err, result) { 3 // result == true 4}); 5bcrypt.compare(someOtherPlaintextPassword, hash, function(err, result) { 6 // result == false 7});
with promises
bcrypt uses whatever Promise
implementation is available in global.Promise
. NodeJS >= 0.12 has a native Promise
implementation built in. However, this should work in any Promises/A+ compliant implementation.
Async methods that accept a callback, return a Promise
when callback is not specified if Promise support is available.
1bcrypt.hash(myPlaintextPassword, saltRounds).then(function(hash) { 2 // Store hash in your password DB. 3});
1// Load hash from your password DB. 2bcrypt.compare(myPlaintextPassword, hash).then(function(result) { 3 // result == true 4}); 5bcrypt.compare(someOtherPlaintextPassword, hash).then(function(result) { 6 // result == false 7});
This is also compatible with async/await
1async function checkUser(username, password) { 2 //... fetch user from a db etc. 3 4 const match = await bcrypt.compare(password, user.passwordHash); 5 6 if(match) { 7 //login 8 } 9 10 //... 11}
ESM import
1import bcrypt from "bcrypt"; 2 3// later 4await bcrypt.compare(password, hash);
sync
1const bcrypt = require('bcrypt'); 2const saltRounds = 10; 3const myPlaintextPassword = 's0/\/\P4$$w0rD'; 4const someOtherPlaintextPassword = 'not_bacon';
To hash a password:
Technique 1 (generate a salt and hash on separate function calls):
1const salt = bcrypt.genSaltSync(saltRounds); 2const hash = bcrypt.hashSync(myPlaintextPassword, salt); 3// Store hash in your password DB.
Technique 2 (auto-gen a salt and hash):
1const hash = bcrypt.hashSync(myPlaintextPassword, saltRounds); 2// Store hash in your password DB.
As with async, both techniques achieve the same end-result.
To check a password:
1// Load hash from your password DB. 2bcrypt.compareSync(myPlaintextPassword, hash); // true 3bcrypt.compareSync(someOtherPlaintextPassword, hash); // false
Why is async mode recommended over sync mode?
We recommend using async API if you use bcrypt
on a server. Bcrypt hashing is CPU intensive which will cause the sync APIs to block the event loop and prevent your application from servicing any inbound requests or events. The async version uses a thread pool which does not block the main event loop.
API
BCrypt.
genSaltSync(rounds, minor)
rounds
- [OPTIONAL] - the cost of processing the data. (default - 10)minor
- [OPTIONAL] - minor version of bcrypt to use. (default - b)
genSalt(rounds, minor, cb)
rounds
- [OPTIONAL] - the cost of processing the data. (default - 10)minor
- [OPTIONAL] - minor version of bcrypt to use. (default - b)cb
- [OPTIONAL] - a callback to be fired once the salt has been generated. uses eio making it asynchronous. Ifcb
is not specified, aPromise
is returned if Promise support is available.err
- First parameter to the callback detailing any errors.salt
- Second parameter to the callback providing the generated salt.
hashSync(data, salt)
data
- [REQUIRED] - the data to be encrypted.salt
- [REQUIRED] - the salt to be used to hash the password. if specified as a number then a salt will be generated with the specified number of rounds and used (see example under Usage).
hash(data, salt, cb)
data
- [REQUIRED] - the data to be encrypted.salt
- [REQUIRED] - the salt to be used to hash the password. if specified as a number then a salt will be generated with the specified number of rounds and used (see example under Usage).cb
- [OPTIONAL] - a callback to be fired once the data has been encrypted. uses eio making it asynchronous. Ifcb
is not specified, aPromise
is returned if Promise support is available.err
- First parameter to the callback detailing any errors.encrypted
- Second parameter to the callback providing the encrypted form.
compareSync(data, encrypted)
data
- [REQUIRED] - data to compare.encrypted
- [REQUIRED] - data to be compared to.
compare(data, encrypted, cb)
data
- [REQUIRED] - data to compare.encrypted
- [REQUIRED] - data to be compared to.cb
- [OPTIONAL] - a callback to be fired once the data has been compared. uses eio making it asynchronous. Ifcb
is not specified, aPromise
is returned if Promise support is available.err
- First parameter to the callback detailing any errors.same
- Second parameter to the callback providing whether the data and encrypted forms match [true | false].
getRounds(encrypted)
- return the number of rounds used to encrypt a given hashencrypted
- [REQUIRED] - hash from which the number of rounds used should be extracted.
A Note on Rounds
A note about the cost: when you are hashing your data, the module will go through a series of rounds to give you a secure hash. The value you submit is not just the number of rounds the module will go through to hash your data. The module will use the value you enter and go through 2^rounds
hashing iterations.
From @garthk, on a 2GHz core you can roughly expect:
rounds=8 : ~40 hashes/sec
rounds=9 : ~20 hashes/sec
rounds=10: ~10 hashes/sec
rounds=11: ~5 hashes/sec
rounds=12: 2-3 hashes/sec
rounds=13: ~1 sec/hash
rounds=14: ~1.5 sec/hash
rounds=15: ~3 sec/hash
rounds=25: ~1 hour/hash
rounds=31: 2-3 days/hash
A Note on Timing Attacks
Because it's come up multiple times in this project and other bcrypt projects, it needs to be said. The bcrypt
library is not susceptible to timing attacks. From codahale/bcrypt-ruby#42:
One of the desired properties of a cryptographic hash function is preimage attack resistance, which means there is no shortcut for generating a message which, when hashed, produces a specific digest.
A great thread on this, in much more detail can be found @ codahale/bcrypt-ruby#43
If you're unfamiliar with timing attacks and want to learn more you can find a great writeup @ A Lesson In Timing Attacks
However, timing attacks are real. And the comparison function is not time safe. That means that it may exit the function early in the comparison process. Timing attacks happen because of the above. We don't need to be careful that an attacker will learn anything, and our comparison function provides a comparison of hashes. It is a utility to the overall purpose of the library. If you end up using it for something else, we cannot guarantee the security of the comparator. Keep that in mind as you use the library.
Hash Info
The characters that comprise the resultant hash are ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$
.
Resultant hashes will be 60 characters long and they will include the salt among other parameters, as follows:
$[algorithm]$[cost]$[salt][hash]
- 2 chars hash algorithm identifier prefix.
"$2a$" or "$2b$"
indicates BCrypt - Cost-factor (n). Represents the exponent used to determine how many iterations 2^n
- 16-byte (128-bit) salt, base64 encoded to 22 characters
- 24-byte (192-bit) hash, base64 encoded to 31 characters
Example:
$2b$10$nOUIs5kJ7naTuTFkBy1veuK0kSxUFXfuaOKdOKf9xYT0KKIGSJwFa
| | | |
| | | hash-value = K0kSxUFXfuaOKdOKf9xYT0KKIGSJwFa
| | |
| | salt = nOUIs5kJ7naTuTFkBy1veu
| |
| cost-factor => 10 = 2^10 rounds
|
hash-algorithm identifier => 2b = BCrypt
Testing
If you create a pull request, tests better pass :)
npm install
npm test
Credits
The code for this comes from a few sources:
- blowfish.cc - OpenBSD
- bcrypt.cc - OpenBSD
- bcrypt::gen_salt - gen_salt inclusion to bcrypt
- bcrypt_node.cc - me
Contributors
- Antonio Salazar Cardozo - Early MacOS X support (when we used libbsd)
- Ben Glow - Fixes for thread safety with async calls
- Van Nguyen - Found a timing attack in the comparator
- NewITFarmer - Initial Cygwin support
- David Trejo - packaging fixes
- Alfred Westerveld - packaging fixes
- Vincent Côté-Roy - Testing around concurrency issues
- Lloyd Hilaiel - Documentation fixes
- Roman Shtylman - Code refactoring, general rot reduction, compile options, better memory management with delete and new, and an upgrade to libuv over eio/ev.
- Vadim Graboys - Code changes to support 0.5.5+
- Ben Noordhuis - Fixed a thread safety issue in nodejs that was perfectly mappable to this module.
- Nate Rajlich - Bindings and build process.
- Sean McArthur - Windows Support
- Fanie Oosthuysen - Windows Support
- Amitosh Swain Mahapatra - $2b$ hash support, ES6 Promise support
- Nicola Del Gobbo - Initial implementation with N-API
License
Unless stated elsewhere, file headers or otherwise, the license as stated in the LICENSE file.
Stable Version
The latest stable version of the package.
Stable Version
5.1.1
MODERATE
1
5.9/10
Summary
Integer Overflow or Wraparound and Use of a Broken or Risky Cryptographic Algorithm in bcrypt
Affected Versions
< 5.0.0
Patched Versions
5.0.0
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/build-pack-publish.yml:83
Reason
5 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Warn: no linked content found
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
Found 5/11 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:52: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:61: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:67: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:78: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:88: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:89: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:93: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:109: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:110: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build-pack-publish.yml:114: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/build-pack-publish.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/ci.yaml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:20: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/ci.yaml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yaml:35: update your workflow using https://app.stepsecurity.io/secureworkflow/kelektiv/node.bcrypt.js/ci.yaml/master?enable=pin
- Warn: containerImage not pinned by hash: Dockerfile:21
- Warn: containerImage not pinned by hash: Dockerfile-alpine:10
- Warn: npmCommand not pinned by hash: Dockerfile:28-37
- Warn: npmCommand not pinned by hash: Dockerfile-alpine:17-21
- Warn: npmCommand not pinned by hash: build-all.sh:12
- Info: 0 out of 15 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 0 out of 2 containerImage dependencies pinned
- Info: 6 out of 9 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/build-pack-publish.yml:106
- Warn: no topLevel permission defined: .github/workflows/build-pack-publish.yml:1
- Warn: no topLevel permission defined: .github/workflows/ci.yaml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
Project has not signed or included provenance with any releases.
Details
- Warn: release artifact v5.1.1 not signed: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/115062859
- Warn: release artifact v5.1.0 not signed: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/79179441
- Warn: release artifact v5.0.1 not signed: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/38407008
- Warn: release artifact v5.0.0 not signed: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/27107778
- Warn: release artifact v4.0.1 not signed: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/24082207
- Warn: release artifact v5.1.1 does not have provenance: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/115062859
- Warn: release artifact v5.1.0 does not have provenance: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/79179441
- Warn: release artifact v5.0.1 does not have provenance: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/38407008
- Warn: release artifact v5.0.0 does not have provenance: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/27107778
- Warn: release artifact v4.0.1 does not have provenance: https://api.github.com/repos/kelektiv/node.bcrypt.js/releases/24082207
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 29 are checked with a SAST tool
Score
3.9
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More