Created
June 4, 2014 19:33
-
-
Save colelawrence/7d383273da7eaa5d7c40 to your computer and use it in GitHub Desktop.
Password hashing for nodejs
This file contains hidden or 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
# check out https://github.com/visionmedia/node-pwd | |
# Module dependencies. | |
crypto = require('crypto'); | |
# Bytesize. | |
len = 128; | |
# Iterations. ~300ms | |
iterations = 12000; | |
### | |
# Hashes a password with optional `salt`, otherwise | |
# generate a salt for `pass` and invoke `fn(err, salt, hash)`. | |
# | |
# @param {String} password to hash | |
# @param {String} optional salt | |
# @param {Function} callback | |
# @api public | |
### | |
exports.hash = (pwd, salt, fn) -> | |
if (3 == arguments.length) | |
crypto.pbkdf2 pwd, salt, iterations, len, (err, hash)-> | |
fn(err, hash.toString('base64')) | |
else | |
fn = salt | |
crypto.randomBytes len, (err, salt)-> | |
return fn(err) if (err) | |
salt = salt.toString('base64') | |
crypto.pbkdf2 pwd, salt, iterations, len, (err, hash) -> | |
return fn(err) if (err) | |
fn(null, salt, hash.toString('base64')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment