Skip to content

Instantly share code, notes, and snippets.

@zhangzhhz
Last active January 30, 2020 03:44
Show Gist options
  • Save zhangzhhz/a9ce31060d7e6f2db97f702b489a1779 to your computer and use it in GitHub Desktop.
Save zhangzhhz/a9ce31060d7e6f2db97f702b489a1779 to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
const util = require('util');
const randomBytes = util.promisify(crypto.randomBytes);
async function hashPassword(password) {
// Hash a password for storing.
try {
const buf = await randomBytes(64);
const salt = crypto.createHash('sha256').update(buf.toString('hex')).digest('hex');
const hashedPassword = crypto.scryptSync(password, salt, 64);
return salt + hashedPassword.toString('hex'); // prefix salt
}
catch (err) {
return null;
}
}
function comparePasswords(storedPassword, providedPassword) {
const salt = storedPassword.substring(0, 64);
storedPassword = storedPassword.substring(64);
let hashedPassword = crypto.scryptSync(providedPassword, salt, 64);
hashedPassword = hashedPassword.toString('hex');
if (hashedPassword === storedPassword) {
return true;
}
return false;
}
// test
(async () => {
const passwordHashed = await hashPassword('我的密码');
console.log(`Hashed password is [${passwordHashed}]`);
// correct password
console.log(comparePasswords(passwordHashed, '我的密码'));
// incorrect password
console.log(comparePasswords(passwordHashed, '错误的密码'));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment