Last active
June 28, 2018 07:17
-
-
Save guangningyu/2559b072ff57af1399785a5ca393c44e to your computer and use it in GitHub Desktop.
This file contains 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
var bcrypt = require('bcrypt'); | |
const saltRounds = 10; | |
const myPlaintextPassword = 's0/\/\P4$$w0rD'; | |
const someOtherPlaintextPassword = 'not_bacon'; | |
// Hash a password | |
bcrypt.genSalt(saltRounds, function(err, salt) { | |
bcrypt.hash(myPlaintextPassword, salt, function(err, hash) { | |
console.log('> hash a password (generate a salt and hash on separate function calls)'); | |
console.log(myPlaintextPassword); | |
console.log(salt); | |
console.log(hash); // save as hash1 | |
}); | |
}); | |
bcrypt.hash(myPlaintextPassword, saltRounds, function(err, hash) { | |
console.log('> hash a password (auto-gen a salt and hash)'); | |
console.log(myPlaintextPassword); | |
console.log(hash); // save as hash2 | |
}); | |
// Check a password | |
var hash1 = "$2b$10$0B3SxKpaaPr.j9LXOcUdPOc.6V6bAe1SBeUdap9Y0vmiNPFDAenJS"; | |
var hash2 = "$2b$10$Hb63I0ICt.EtAdzWPMNkS./TD8pKZVDrcJNas5P/cgikPn6M9f9ve"; | |
bcrypt.compare(myPlaintextPassword, hash1, function(err, res) { | |
console.log('> check hash1'); | |
console.log(myPlaintextPassword); | |
console.log(hash1); | |
console.log(res); // return true | |
}); | |
bcrypt.compare(myPlaintextPassword, hash2, function(err, res) { | |
console.log('> check hash2'); | |
console.log(myPlaintextPassword); | |
console.log(hash2); | |
console.log(res); // return true | |
}); | |
bcrypt.compare(someOtherPlaintextPassword, hash2, function(err, res) { | |
console.log('> check hash2 (wrong password)'); | |
console.log(someOtherPlaintextPassword); | |
console.log(hash2); | |
console.log(res); // return false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment