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

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

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