Skip to content

Instantly share code, notes, and snippets.

@davidwood
Created September 22, 2012 20:13
Show Gist options
  • Save davidwood/3767666 to your computer and use it in GitHub Desktop.
Save davidwood/3767666 to your computer and use it in GitHub Desktop.
Hash password
var crypto = require('crypto'),
saltLength = 10,
algorithm = 'sha1';
function hash(password) {
var salt = crypto.randomBytes(10).toString('binary'),
hash = crypto.createHmac(algorithm, salt).update(password).digest();
return { hash: hash, salt: salt };
}
function verify(password, salt, hash) {
var newHash = crypto.createHmac(algorithm, salt).update(password).digest();
console.log(newHash);
return newHash === hash;
}
var password = 'mypassword',
hashed = hash(password);
console.log('salt:', hashed.salt);
console.log('hashed password:', hashed.hash);
console.log('verify:', verify(password, hashed.salt, hashed.hash));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment