Skip to content

Instantly share code, notes, and snippets.

@gx0r
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gx0r/9241684 to your computer and use it in GitHub Desktop.
Save gx0r/9241684 to your computer and use it in GitHub Desktop.
// Timer to continue counting while using bcrypt.
// Demonstrates that the bcrypt functions are asynchronous.
;(function() {
console.log('start timer');
var x = 1;
setInterval(function() {
console.log('Timer: ' + x);
x++;
}, 1000);
}());
console.log('loading libs...');
var Promise = require('bluebird');
var bcrypt = Promise.promisifyAll(require('bcrypt'));
var thepasswordis = 'user';
var rounds = 10;
console.log('loading libs done.');
console.log('Generating salt...');
console.time('Generate the salt');
bcrypt.genSaltAsync(rounds).then(function (salt) {
console.log('The salt is: ' + salt);
console.timeEnd('Generate the salt');
console.log('Next, hash the password with the salt');
console.time('Hash the password with the salt');
return bcrypt.hashAsync(thepasswordis, salt);
}).then(function (h) {
console.log('The hash is: ' + h);
console.timeEnd('Hash the password with the salt');
console.log('Finally, compare the password with the hash');
console.time('Compare the hash with the password');
return bcrypt.compareAsync(thepasswordis, h);
}).then(function (comp) {
console.log("They match? " + comp);
console.timeEnd('Compare the hash with the password');
process.exit(0);
}).catch(function (err) {
console.error('Error: ' +err);
process.exit(1);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment