Skip to content

Instantly share code, notes, and snippets.

@chrisdl
Last active April 9, 2020 22:49
Show Gist options
  • Save chrisdl/123af540c1c89f5cfb04c129b1dbb4de to your computer and use it in GitHub Desktop.
Save chrisdl/123af540c1c89f5cfb04c129b1dbb4de to your computer and use it in GitHub Desktop.
Saving passwords assignment
const crypto = require("crypto");
class Db {
constructor(users={}) {
this.users = users;
}
// Expects passwordPayload to be a string of the form 'salt$passwordHash'
// Where the '$' is used as a separator.
storeUser(username, passwordPayload) {
this.users[username] = passwordPayload;
}
getUserPassword(username) {
let hash = this.users[username];
return {
salt: hash.substr(0, hash.indexOf('$')),
passwordHash: hash.substr(hash.indexOf('$') + 1)
};
}
}
const db = new Db();
// STUDENT CODE STARTS HERE ------------
const hashPassword = (password, salt) => {
// TODO (1) create a passwordHash using the crypto.pbkdf2Sync command and sha256.
// https://nodejs.org/api/crypto.html#crypto_crypto_pbkdf2sync_password_salt_iterations_keylen_digest
// it should have a key length of 64. I'll leave the iteration count up to you though.
// The parameters are (in order): password, salt, iterations, keylength, hashFunctionName
// const passwordHash = ...
return passwordHash.toString('hex');
};
const saveUser = (username, password) => {
// TODO (2) Generate a salt using crypto.randomBytes in 'hex' format. (length 64)
// https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
// const salt = ...
// TODO (3) Hash the password using the hashPassword function.
// const passwordHash = ...
// TODO (4) create payload (string) of the form 'salt$passwordHash'
// const payload = ...
db.storeUser(username, payload);
};
// Return true if authenticated, false otherwise.
const isUserAuthenticated = (username, password) => {
// TODO (5) Get the users password payload from the database.
// let payload = ...
// TODO (6) If the user is not in the database it will throw a TypeError, you should catch
// and deal with it.
// TODO (10) Return the comparison of the passwords using the comparePasswords function.
// return comparePasswords(...);
};
// returns true if passwords match, otherwise false.
const comparePasswords = (plaintextPassword, salt, passwordHashFromDb) => {
// TODO (7) Hash the plaintextPassword using the salt.
// TODO (8) compare the passwordHash you just created with the one from the database.
// TODO (9) Return true if they match otherwise return false.
};
// STUDENT CODE ENDS HERE ------------
saveUser('[email protected]', '123456qwerty');
console.log(isUserAuthenticated('[email protected]', 'HelloWorld!')) // false
console.log(isUserAuthenticated('[email protected]', 'HelloWorld!')) // false
console.log(isUserAuthenticated('[email protected]', 'HelloWorld!')) // false
console.log(isUserAuthenticated('[email protected]', '123456qwerty')) // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment