v1.18.x
Julian Lam 11 years ago
parent cf22fad6fd
commit ea2fbcfcfc

@ -1,30 +1,29 @@
'use strict'; 'use strict';
var bcrypt = require('bcryptjs'),
async = require('async'),
action = process.argv[2];
var bcrypt = require('bcryptjs'); switch(action) {
case 'compare':
bcrypt.compare(process.argv[3], process.argv[4], function(err, res) {
process.stdout.write(res ? 'true' : 'false');
});
break;
process.on('message', function(m) { case 'hash':
if (m.type === 'hash') { async.waterfall([
hash(m.rounds, m.password); async.apply(bcrypt.genSalt, parseInt(process.argv[3], 10)),
} else if (m.type === 'compare') { function(salt, next) {
compare(m.password, m.hash); bcrypt.hash(process.argv[4], salt, next);
} }
}); ], function(err, hash) {
if (!err) {
function hash(rounds, password) { process.stdout.write(hash);
bcrypt.genSalt(rounds, function(err, salt) { } else {
if (err) { process.stderr.write(err.message);
return process.send({type:'hash', err: {message: err.message}}); }
} });
bcrypt.hash(password, salt, function(err, hash) { break;
process.send({type:'hash', err: err ? {message: err.message} : null, hash: hash, password: password});
});
});
}
function compare(password, hash) {
bcrypt.compare(password, hash, function(err, res) {
process.send({type:'compare', err: err ? {message: err.message} : null, hash: hash, password: password, result: res});
});
} }

@ -1,51 +1,37 @@
'use strict'; 'use strict';
var fork = require('child_process').fork;
(function(module) { (function(module) {
var fork = require('child_process').fork;
var child = fork('./bcrypt', process.argv.slice(2), {
env: process.env
});
var callbacks = {
'hash': {},
'compare': {}
};
module.hash = function(rounds, password, callback) { module.hash = function(rounds, password, callback) {
sendCommand({type: 'hash', password: password, rounds: rounds}, callback); var child = fork('./bcrypt', ['hash', rounds, password], {
silent: true
}),
response = '';
child.stdout.on('data', function(chunk) {
response += chunk.toString();
});
child.stdout.on('end', function() {
callback(null, response);
});
}; };
module.compare = function(password, hash, callback) { module.compare = function(password, hash, callback) {
sendCommand({type: 'compare', password: password, hash: hash}, callback); var child = fork('./bcrypt', ['compare', password, hash], {
silent: true
}),
response = '';
child.stdout.on('data', function(chunk) {
response += chunk.toString();
});
child.stdout.on('end', function() {
callback(null, response === 'true');
});
}; };
function sendCommand(data, callback) { return module;
callbacks[data.type][data.password] = callbacks[data.type][data.password] || []; })(exports);
callbacks[data.type][data.password].push(callback);
child.send(data);
}
child.on('message', function(msg) {
var cbs = callbacks[msg.type] ? callbacks[msg.type][msg.password] : null;
if (Array.isArray(cbs)) {
if (msg.err) {
var err = new Error(msg.err.message);
cbs.forEach(function(callback) {
callback(err);
});
cbs.length = 0;
return;
}
cbs.forEach(function(callback) {
callback(null, msg.type === 'hash' ? msg.hash : msg.result);
});
cbs.length = 0;
}
});
}(exports));
Loading…
Cancel
Save