Last active
August 29, 2015 13:56
-
-
Save gx0r/9241684 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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