2017-08-28 12:27:45 -04:00
|
|
|
// check out https://github.com/tj/node-pwd
|
|
|
|
|
2018-08-30 15:05:23 -04:00
|
|
|
/*jslint node: true */
|
|
|
|
/*jshint node: true */
|
|
|
|
/*jshint strict:false */
|
|
|
|
/*jshint -W097 */
|
|
|
|
/*jshint esversion: 6 */
|
|
|
|
"use strict";
|
2018-08-27 15:24:15 -04:00
|
|
|
|
2017-08-28 12:27:45 -04:00
|
|
|
// Module dependencies.
|
2018-05-03 14:09:29 -04:00
|
|
|
const crypto = require('crypto');
|
2017-08-28 12:27:45 -04:00
|
|
|
|
|
|
|
// Bytesize.
|
2018-05-03 14:09:29 -04:00
|
|
|
const len = 128;
|
2017-08-28 12:27:45 -04:00
|
|
|
|
|
|
|
// Iterations. ~300ms
|
2018-05-03 14:09:29 -04:00
|
|
|
const iterations = 12000;
|
2017-08-28 12:27:45 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2019-05-14 17:39:26 -04:00
|
|
|
exports.hash = function (pwd, salt, fn, tag) {
|
|
|
|
if (4 == arguments.length) {
|
2017-08-28 12:27:45 -04:00
|
|
|
try {
|
2019-05-14 17:39:26 -04:00
|
|
|
crypto.pbkdf2(pwd, salt, iterations, len, 'sha384', function (err, hash) { fn(err, hash.toString('base64'), tag); });
|
2017-08-28 12:27:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
// If this previous call fails, it's probably because older pbkdf2 did not specify the hashing function, just use the default.
|
2019-05-14 17:39:26 -04:00
|
|
|
crypto.pbkdf2(pwd, salt, iterations, len, function (err, hash) { fn(err, hash.toString('base64'), tag); });
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-14 17:39:26 -04:00
|
|
|
tag = fn;
|
2017-08-28 12:27:45 -04:00
|
|
|
fn = salt;
|
|
|
|
crypto.randomBytes(len, function (err, salt) {
|
|
|
|
if (err) return fn(err);
|
|
|
|
salt = salt.toString('base64');
|
|
|
|
try {
|
2019-05-14 17:39:26 -04:00
|
|
|
crypto.pbkdf2(pwd, salt, iterations, len, 'sha384', function (err, hash) { if (err) { return fn(err); } fn(null, salt, hash.toString('base64'), tag); });
|
2017-08-28 12:27:45 -04:00
|
|
|
} catch (e) {
|
|
|
|
// If this previous call fails, it's probably because older pbkdf2 did not specify the hashing function, just use the default.
|
2019-05-14 17:39:26 -04:00
|
|
|
crypto.pbkdf2(pwd, salt, iterations, len, function (err, hash) { if (err) { return fn(err); } fn(null, salt, hash.toString('base64'), tag); });
|
2017-08-28 12:27:45 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-11-02 21:44:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.iishash = function (type, pwd, salt, fn) {
|
|
|
|
if (type == 0) {
|
|
|
|
fn(null, pwd);
|
|
|
|
} else if (type == 1) {
|
|
|
|
const hash = crypto.createHash('sha1');
|
2019-01-02 21:03:34 -05:00
|
|
|
hash.update(Buffer.concat([Buffer.from(salt, 'base64'), Buffer.from(pwd, 'utf16le')]));
|
2017-11-02 21:44:27 -04:00
|
|
|
fn(null, hash.digest().toString('base64'));
|
|
|
|
} else {
|
|
|
|
fn('invalid type');
|
|
|
|
}
|
|
|
|
};
|