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':
process.on('message', function(m) { bcrypt.compare(process.argv[3], process.argv[4], function(err, res) {
if (m.type === 'hash') { process.stdout.write(res ? 'true' : 'false');
hash(m.rounds, m.password);
} else if (m.type === 'compare') {
compare(m.password, m.hash);
}
}); });
break;
function hash(rounds, password) { case 'hash':
bcrypt.genSalt(rounds, function(err, salt) { async.waterfall([
if (err) { async.apply(bcrypt.genSalt, parseInt(process.argv[3], 10)),
return process.send({type:'hash', err: {message: err.message}}); function(salt, next) {
bcrypt.hash(process.argv[4], salt, next);
} }
bcrypt.hash(password, salt, function(err, hash) { ], function(err, hash) {
process.send({type:'hash', err: err ? {message: err.message} : null, hash: hash, password: password}); if (!err) {
}); process.stdout.write(hash);
}); } else {
process.stderr.write(err.message);
} }
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});
}); });
break;
} }

@ -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), { module.hash = function(rounds, password, callback) {
env: process.env var child = fork('./bcrypt', ['hash', rounds, password], {
}); silent: true
}),
response = '';
var callbacks = { child.stdout.on('data', function(chunk) {
'hash': {}, response += chunk.toString();
'compare': {} });
};
module.hash = function(rounds, password, callback) { child.stdout.on('end', function() {
sendCommand({type: 'hash', password: password, rounds: rounds}, callback); 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
}),
function sendCommand(data, callback) { response = '';
callbacks[data.type][data.password] = callbacks[data.type][data.password] || [];
callbacks[data.type][data.password].push(callback);
child.send(data);
}
child.on('message', function(msg) { child.stdout.on('data', function(chunk) {
var cbs = callbacks[msg.type] ? callbacks[msg.type][msg.password] : null; response += chunk.toString();
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) { child.stdout.on('end', function() {
callback(null, msg.type === 'hash' ? msg.hash : msg.result); callback(null, response === 'true');
});
cbs.length = 0;
}
}); });
};
}(exports)); return module;
})(exports);
Loading…
Cancel
Save