Created
September 25, 2015 06:59
-
-
Save abcdabcd987/5b098de6545a9c1d0b62 to your computer and use it in GitHub Desktop.
using pbkdf2 to hash password (node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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