mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-09 15:35:47 +01:00
use send for child process
This commit is contained in:
44
bcrypt.js
44
bcrypt.js
@@ -2,28 +2,40 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var bcrypt = require('bcryptjs'),
|
var bcrypt = require('bcryptjs'),
|
||||||
async = require('async'),
|
async = require('async');
|
||||||
action = process.argv[2];
|
|
||||||
|
|
||||||
switch(action) {
|
|
||||||
case 'compare':
|
|
||||||
bcrypt.compare(process.argv[3], process.argv[4], function(err, res) {
|
|
||||||
process.stdout.write(res ? 'true' : 'false');
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'hash':
|
process.on('message', function(msg) {
|
||||||
|
if (msg.type === 'hash') {
|
||||||
|
hashPassword(msg.password, msg.rounds);
|
||||||
|
} else if (msg.type === 'compare') {
|
||||||
|
compare(msg.password, msg.hash);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function hashPassword(password, rounds) {
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(bcrypt.genSalt, parseInt(process.argv[3], 10)),
|
function(next) {
|
||||||
|
bcrypt.genSalt(parseInt(rounds, 10), next);
|
||||||
|
},
|
||||||
function(salt, next) {
|
function(salt, next) {
|
||||||
bcrypt.hash(process.argv[4], salt, next);
|
bcrypt.hash(password, salt, next);
|
||||||
}
|
}
|
||||||
], function(err, hash) {
|
], function(err, hash) {
|
||||||
if (!err) {
|
if (err) {
|
||||||
process.stdout.write(hash);
|
return process.send({err: err.message});
|
||||||
} else {
|
|
||||||
process.stderr.write(err.message);
|
|
||||||
}
|
}
|
||||||
|
process.send({result: hash});
|
||||||
|
process.disconnect();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function compare(password, hash) {
|
||||||
|
bcrypt.compare(password, hash, function(err, res) {
|
||||||
|
if (err) {
|
||||||
|
return process.send({err: err.message});
|
||||||
|
}
|
||||||
|
process.send({result: res});
|
||||||
|
process.disconnect();
|
||||||
});
|
});
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
@@ -4,34 +4,28 @@
|
|||||||
var fork = require('child_process').fork;
|
var fork = require('child_process').fork;
|
||||||
|
|
||||||
module.hash = function(rounds, password, callback) {
|
module.hash = function(rounds, password, callback) {
|
||||||
var child = fork('./bcrypt', ['hash', rounds, password], {
|
forkChild({type: 'hash', rounds: rounds, password: password}, callback);
|
||||||
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) {
|
||||||
var child = fork('./bcrypt', ['compare', password, hash], {
|
forkChild({type: 'compare', password: password, hash: hash}, callback);
|
||||||
silent: true
|
|
||||||
}),
|
|
||||||
response = '';
|
|
||||||
|
|
||||||
child.stdout.on('data', function(chunk) {
|
|
||||||
response += chunk.toString();
|
|
||||||
});
|
|
||||||
|
|
||||||
child.stdout.on('end', function() {
|
|
||||||
callback(null, response === 'true');
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function forkChild(message, callback) {
|
||||||
|
var child = fork('./bcrypt', {
|
||||||
|
silent: true
|
||||||
|
});
|
||||||
|
|
||||||
|
child.on('message', function(msg) {
|
||||||
|
if (msg.err) {
|
||||||
|
return callback(new Error(msg.err));
|
||||||
|
}
|
||||||
|
|
||||||
|
callback(null, msg.result);
|
||||||
|
});
|
||||||
|
|
||||||
|
child.send(message);
|
||||||
|
}
|
||||||
|
|
||||||
return module;
|
return module;
|
||||||
})(exports);
|
})(exports);
|
||||||
Reference in New Issue
Block a user