Package Exports
- bcrypt
- bcrypt/bcrypt
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bcrypt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node.bcrypt.js
Lib to help you hash passwords. bcrypt on wikipedia
Catalyst for this module: How To Safely Store A Password
Security Issues/Concerns
As should be the case with any security tool, this library should be scrutinized by anyone using it. If you find or suspect an issue with the code- please bring it to my attention and I'll spend some time trying to make sure that this tool is as secure as possible.
To make it easier for people using this tool to analyze what has been surveyed, here is a list of BCrypt related security issues/concerns as they've come up.
- 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.
Dependencies
- NodeJS
- OpenSSL
From NPM
npm install bcrypt
From Source
Assuming you've already built node, you can run the waf script:
node-waf configure
node-waf build
npm link
Usage - Sync
To hash a password:
var bcrypt = require('bcrypt');
var salt = bcrypt.gen_salt_sync(10);
var hash = bcrypt.encrypt_sync("B4c0/\/", salt);
// Store hash in your password DB.
To check a password:
// Load hash from your password DB.
bcrypt.compare_sync("B4c0/\/", hash); // true
bcrypt.compare_sync("not_bacon", hash); // false
Usage - Async
To hash a password:
var bcrypt = require('bcrypt');
bcrypt.gen_salt(10, function(err, salt) {
bcrypt.encrypt("B4c0/\/", salt, function(err, hash) {
// Store hash in your password DB.
});
});
To check a password:
// Load hash from your password DB.
bcrypt.compare("B4c0/\/", hash, function(err, res) {
// res == true
});
bcrypt.compare("not_bacon", hash, function(err, res) {
// res = false
});
API
BCrypt.
gen_salt_sync(rounds, seed_length)
rounds
- [OPTIONAL] - the number of rounds to process the data for. (default - 10)seed_length
- [OPTIONAL] - RAND_bytes wants a length. to make that a bit flexible, you can specify a seed_length. (default - 20)
gen_salt(rounds, seed_length, cb)
rounds
- [OPTIONAL] - the number of rounds to process the data for. (default - 10)seed_length
- [OPTIONAL] - RAND_bytes wants a length. to make that a bit flexible, you can specify a seed_length. (default - 20)cb
- [REQUIRED] - a callback to be fired once the salt has been generated. uses eio making it asynchronous.err
- First parameter to the callback detailing any errors.salt
- Second parameter to the callback providing the generated salt.
encrypt_sync(data, salt)
data
- [REQUIRED] - the data to be encrypted.salt
- [REQUIRED] - the salt to be used in encryption.
encrypt(data, salt, cb)
data
- [REQUIRED] - the data to be encrypted.salt
- [REQUIRED] - the salt to be used in encryption.cb
- [REQUIRED] - a callback to be fired once the data has been encrypted. uses eio making it asynchronous.err
- First parameter to the callback detailing any errors.encrypted
- Second parameter to the callback providing the encrypted form.
compare_sync(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
- [REQUIRED] - a callback to be fired once the data has been compared. uses eio making it asynchronous.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].
Hash Info
The characters that comprise the resultant hash are ./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789$
.
Testing
I am using nodeunit. I like the way you write tests with it and I like the default output. As such you'll need it to run the tests. I suspect my tests would run on an older version, but these were written and worked against 0.5.1
npm install nodeunit@0.5.1
nodeunit 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 and general rot reduction
- Vadim Graboys - Code changes to support 0.5.5+
License
Unless stated elsewhere, file headers or otherwise, the license as stated in the LICENSE file.