forked from expressjs/express
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update auth example to utilize cores pbkdf2
- Loading branch information
Showing
2 changed files
with
63 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var crypto = require('crypto'); | ||
|
||
/** | ||
* Bytesize. | ||
*/ | ||
|
||
var len = 128; | ||
|
||
/** | ||
* Iterations. ~300ms | ||
*/ | ||
|
||
var 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 = function (pwd, salt, fn) { | ||
if (3 == arguments.length) { | ||
crypto.pbkdf2(pwd, salt, iterations, len, fn); | ||
} else { | ||
fn = salt; | ||
crypto.randomBytes(len, function(err, salt){ | ||
if (err) return fn(err); | ||
salt = salt.toString('base64'); | ||
crypto.pbkdf2(pwd, salt, iterations, len, function(err, hash){ | ||
if (err) return fn(err); | ||
fn(null, salt, hash); | ||
}); | ||
}); | ||
} | ||
}; |