Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
ohboy
2,232 PointsError: Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue
const saltRounds = 10;
const originalHash = '$2y$10$tOlFqtv8gUZqukwS9be/nOKzu3zm73oDz8kelCmuguZOgCXmMG5K2';
console.log('Original Hash: ' + originalHash);
//1. Install and include the bcrypt npm package
var bcrypt = require('bcrypt');
//2. Hash the string 'password' and display to screen
console.log(bcrypt.hashSync('password', saltRounds));
//3. Hash the string 'bad_password' and display to screen
console.log(bcrypt.hashSync('not_password', saltRounds));
//4. Use bcrypt to compare the string 'password' with the variable originalHash
console.log('Comparing string password to hash: ' + bcrypt.compareSync('password', originalHash));
//5. Use bcrypt to compare the string 'bad_password' with the variable originalHash
console.log('Comparing string not_password to hash: ' + bcrypt.hashSync('not_password', originalHash));
I'm so confused
1 Answer
Neil McPartlin
14,662 PointsFirst lets display your code in a more friendly way by following the instructions provided in the Markdown Cheatsheet below...
const saltRounds = 10;
const originalHash = '$2y$10$tOlFqtv8gUZqukwS9be/nOKzu3zm73oDz8kelCmuguZOgCXmMG5K2';
console.log('Original Hash: ' + originalHash);
//1. Install and include the bcrypt npm package
var bcrypt = require('bcrypt');
//2. Hash the string 'password' and display to screen
console.log(bcrypt.hashSync('password', saltRounds));
//3. Hash the string 'bad_password' and display to screen
console.log(bcrypt.hashSync('not_password', saltRounds));
//4. Use bcrypt to compare the string 'password' with the variable originalHash
console.log('Comparing string password to hash: ' + bcrypt.compareSync('password', originalHash));
//5. Use bcrypt to compare the string 'bad_password' with the variable originalHash
console.log('Comparing string not_password to hash: ' + bcrypt.hashSync('not_password', originalHash));
As you say, we get this error message...
Error: Invalid salt. Salt must be in the form of: $Vers$log2(NumRounds)$saltvalue
I commented out all your console.log statements, then uncommented each one, one by one, until the problem was isolated to the last line. Here you accidentally used hashSync when you intended to use compareSync just like you did in the previous line. So amend this line like so...
console.log('Comparing string not_password to hash: ' + bcrypt.compareSync('not_password', originalHash));
So now we see...
Original Hash: $2y$10$tOlFqtv8gUZqukwS9be/nOKzu3zm73oDz8kelCmuguZOgCXmMG5K2
$2a$10$un48JbMk1txjgTiwW3LtUOQNYxT1c4gZEtIhZ7ueSthE3aXwfxKJS
$2a$10$YdTXtjbpL7KcBzS695wzP.3YoTfesAFy1/dEKSiZSWQBCWP4rcVwy
Comparing string password to hash: false
Comparing string not_password to hash: false
...but we are not quite there.
The response in the 2nd last line should be showing true. But you are seeing false and in fact, this is correct because you are using a different originalHash to the one provided by Alena (the teacher). Her hash was created using the text password and yours was not. So just replace your originalHash with Alena's like so...
const originalHash = '$2a$10$7h/0SQ4FXRG5eX3602o3/.aO.RYkxKuhGkzvIXHLUiMJlFt1P.6Pe';
Then if we try one more time, we get...
Comparing string password to hash: true