Skip to content

Instantly share code, notes, and snippets.

@abcdabcd987
Created September 25, 2015 06:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abcdabcd987/5b098de6545a9c1d0b62 to your computer and use it in GitHub Desktop.
Save abcdabcd987/5b098de6545a9c1d0b62 to your computer and use it in GitHub Desktop.
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