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

Loading…
Cancel
Save