Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using pbkdf2 to hash password (node.js)
// See: http://stackoverflow.com/questions/17218089/salt-and-hash-using-pbkdf2
// See: https://crackstation.net/hashing-security.htm
'use strict';
let Promise = require('bluebird');
let crypto = require('crypto');
function calcPassword(password) {
const RANDOM_BYTES = 64;
const ITERATIONS = 1<<14;
const ALGORITHM = 'sha256';
return new Promise((fulfill, reject) => {
crypto.randomBytes(RANDOM_BYTES, (err, bytes) => {
if (err) return reject(err);
const salt = bytes.toString('hex');
crypto.pbkdf2(password, salt, ITERATIONS, 64, ALGORITHM, (err, key) => {
if (err) return reject(err);
const hash = key.toString('hex');
fulfill(`${ALGORITHM}:${ITERATIONS}:${salt}:${hash}`);
});
})
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment