Files
NodeBB/src/password.js

75 lines
1.6 KiB
JavaScript
Raw Normal View History

2014-08-12 21:41:23 -04:00
'use strict';
2017-05-27 01:44:26 -04:00
var path = require('path');
2018-10-31 15:10:45 -04:00
var bcrypt = require('bcryptjs');
var async = require('async');
2014-08-12 21:41:23 -04:00
var fork = require('./meta/debugFork');
2017-06-01 14:04:23 -06:00
2018-10-31 15:10:45 -04:00
exports.hash = function (rounds, password, callback) {
2017-05-27 01:44:26 -04:00
forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
2018-10-31 15:10:45 -04:00
};
2018-10-31 15:10:45 -04:00
exports.compare = function (password, hash, callback) {
async.waterfall([
getFakeHash,
function (fakeHash, next) {
forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, next);
},
], callback);
};
2014-11-18 22:55:44 -05:00
var fakeHashCache;
function getFakeHash(callback) {
if (fakeHashCache) {
return callback(null, fakeHashCache);
2017-05-27 01:44:26 -04:00
}
2018-10-31 15:10:45 -04:00
async.waterfall([
function (next) {
exports.hash(12, Math.random().toString(), next);
},
function (hash, next) {
fakeHashCache = hash;
next(null, fakeHashCache);
},
], callback);
}
2017-05-27 01:44:26 -04:00
function forkChild(message, callback) {
2018-10-31 15:10:45 -04:00
var child = fork(path.join(__dirname, 'password'));
2014-10-07 19:49:18 -04:00
2017-05-27 01:44:26 -04:00
child.on('message', function (msg) {
2018-10-31 15:10:45 -04:00
callback(msg.err ? new Error(msg.err) : null, msg.result);
2017-05-27 01:44:26 -04:00
});
2014-08-12 21:41:23 -04:00
2017-05-27 01:44:26 -04:00
child.send(message);
}
2018-10-31 15:10:45 -04:00
// child process
process.on('message', function (msg) {
if (msg.type === 'hash') {
hashPassword(msg.password, msg.rounds);
} else if (msg.type === 'compare') {
bcrypt.compare(String(msg.password || ''), String(msg.hash || ''), done);
}
});
function hashPassword(password, rounds) {
async.waterfall([
function (next) {
bcrypt.genSalt(parseInt(rounds, 10), next);
},
function (salt, next) {
bcrypt.hash(password, salt, next);
},
], done);
}
function done(err, result) {
process.send(err ? { err: err.message } : { result: result });
process.disconnect();
}
2019-07-16 14:17:10 -04:00
require('./promisify')(exports);