1// check out https://github.com/tj/node-pwd
5/*jshint strict:false */
7/*jshint esversion: 6 */
10// Module dependencies.
11const crypto = require('crypto');
17const iterations = 12000;
20 * Hashes a password with optional `salt`, otherwise
21 * generate a salt for `pass` and invoke `fn(err, salt, hash)`.
23 * @param {String} password to hash
24 * @param {String} optional salt
25 * @param {Function} callback
28exports.hash = function (pwd, salt, fn, tag) {
29 if (4 == arguments.length) {
31 crypto.pbkdf2(pwd, salt, iterations, len, 'sha384', function (err, hash) { fn(err, hash.toString('base64'), tag); });
33 // If this previous call fails, it's probably because older pbkdf2 did not specify the hashing function, just use the default.
34 crypto.pbkdf2(pwd, salt, iterations, len, function (err, hash) { fn(err, hash.toString('base64'), tag); });
39 crypto.randomBytes(len, function (err, salt) {
40 if (err) return fn(err);
41 salt = salt.toString('base64');
43 crypto.pbkdf2(pwd, salt, iterations, len, 'sha384', function (err, hash) { if (err) { return fn(err); } fn(null, salt, hash.toString('base64'), tag); });
45 // If this previous call fails, it's probably because older pbkdf2 did not specify the hashing function, just use the default.
46 crypto.pbkdf2(pwd, salt, iterations, len, function (err, hash) { if (err) { return fn(err); } fn(null, salt, hash.toString('base64'), tag); });
52exports.iishash = function (type, pwd, salt, fn) {
55 } else if (type == 1) {
56 const hash = crypto.createHash('sha1');
57 hash.update(Buffer.concat([Buffer.from(salt, 'base64'), Buffer.from(pwd, 'utf16le')]));
58 fn(null, hash.digest().toString('base64'));