move bcrypt into password

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 4e297921c4
commit ff38abc225

@ -1,34 +0,0 @@
'use strict';
var bcrypt = require('bcryptjs');
var async = require('async');
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) {
if (err) {
process.send({ err: err.message });
return process.disconnect();
}
process.send({ result: result });
process.disconnect();
}

@ -1,53 +1,71 @@
'use strict'; 'use strict';
var path = require('path'); var path = require('path');
var bcrypt = require('bcryptjs');
var async = require('async');
var fork = require('./meta/debugFork'); var fork = require('./meta/debugFork');
function hash(rounds, password, callback) { exports.hash = function (rounds, password, callback) {
forkChild({ type: 'hash', rounds: rounds, password: password }, callback); forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
} };
exports.hash = hash; exports.compare = function (password, hash, callback) {
async.waterfall([
getFakeHash,
function (fakeHash, next) {
forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, next);
},
], callback);
};
var fakeHashCache; var fakeHashCache;
function getFakeHash(callback) { function getFakeHash(callback) {
if (fakeHashCache) { if (fakeHashCache) {
return callback(null, fakeHashCache); return callback(null, fakeHashCache);
} }
async.waterfall([
hash(12, Math.random().toString(), function (err, hash) { function (next) {
if (err) { exports.hash(12, Math.random().toString(), next);
return callback(err); },
} function (hash, next) {
fakeHashCache = hash; fakeHashCache = hash;
callback(null, fakeHashCache); next(null, fakeHashCache);
}); },
} ], callback);
function compare(password, hash, callback) {
getFakeHash(function (err, fakeHash) {
if (err) {
return callback(err);
}
forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, callback);
});
} }
exports.compare = compare;
function forkChild(message, callback) { function forkChild(message, callback) {
var child = fork(path.join(__dirname, 'bcrypt')); var child = fork(path.join(__dirname, 'password'));
child.on('message', function (msg) { child.on('message', function (msg) {
if (msg.err) { callback(msg.err ? new Error(msg.err) : null, msg.result);
return callback(new Error(msg.err)); });
child.send(message);
} }
callback(null, msg.result); // 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);
}
}); });
child.send(message); 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();
} }

Loading…
Cancel
Save