shaneMangudi / bcrypt-nodejs

Native implementation of bcrypt for NodeJS
Other
574 stars 69 forks source link

Note: This project is not actively maintained

If you are looking for a javscript-only bcrypt implementation we recommend you use bcrypt.js, which is based on bcrypt-nodejs.

bcrypt-nodejs

===========================================

Join the chat at https://gitter.im/shaneGirish/bcrypt-nodejs Build Status Dependency Status

Warning : A change was made in v0.0.3 to allow encoding of UTF-8 encoded strings. This causes strings encoded in v0.0.2 or earlier to not work in v0.0.3 anymore.

Native JS implementation of BCrypt for Node. Has the same functionality as node.bcrypt.js expect for a few tiny differences. Mainly, it doesn't let you set the seed length for creating the random byte array.

I created this version due to a small problem I faced with node.bcrypt.js. Basically, to deploy one of my apps which uses node.bcrypt.js on a winx64 platform, I have to force the user to download about 1.6gb of sdks, buildtools and other requirements of which some fail to install ! Microsoft :(

This code is based on javascript-bcrypt and uses crypto to create random byte arrays.

Basic usage:

Installing the Package

npm install bcrypt-nodejs or yarn add bcrypt-nodejs

Synchronous

var hash = bcrypt.hashSync("bacon");

bcrypt.compareSync("bacon", hash); // true
bcrypt.compareSync("veggies", hash); // false

Asynchronous

bcrypt.hash("bacon", null, null, function(err, hash) {
  // Store hash in your password DB.
});

// Load hash from your password DB.
bcrypt.compare("bacon", hash, function(err, res) {
    // res == true
});
bcrypt.compare("veggies", hash, function(err, res) {
    // res = false
});

In the above examples, the salt is automatically generated and attached to the hash. Though you can use your custom salt and there is no need for salts to be persisted as it will always be included in the final hash result and can be retrieved.

API

Contributors

Credits

I heavily reused code from javascript-bcrypt. Though "Clipperz Javascript Crypto Library" was removed and its functionality replaced with crypto.