pass.js
This file
Path
examples/auth/pass.js
46 lines · 883 bytes · JavaScript
Last changed by
docsTJ Holowaychukbefore adoptionbddcdee
History of this fileTake it
update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
line 1why, one step back1docsTJ Holowaychukbefore adoptionbddcdee
lines 2–3why, one step back2// check out https://github.com/visionmedia/node-pwd3update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
lines 4–46why, one step back4/**5 * Module dependencies.6 */78var crypto = require('crypto');910/**11 * Bytesize.12 */1314var len = 128;1516/**17 * Iterations. ~300ms18 */1920var iterations = 12000;2122/**23 * Hashes a password with optional `salt`, otherwise24 * generate a salt for `pass` and invoke `fn(err, salt, hash)`.25 *26 * @param {String} password to hash27 * @param {String} optional salt28 * @param {Function} callback29 * @api public30 */3132exports.hash = function (pwd, salt, fn) {33 if (3 == arguments.length) {34 crypto.pbkdf2(pwd, salt, iterations, len, fn);35 } else {36 fn = salt;37 crypto.randomBytes(len, function(err, salt){38 if (err) return fn(err);39 salt = salt.toString('base64');40 crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){41 if (err) return fn(err);42 fn(null, salt, hash);43 });44 });45 }46};