mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 22:45:46 +01:00
closed #2242
This commit is contained in:
@@ -1,51 +1,37 @@
|
||||
|
||||
|
||||
'use strict';
|
||||
var fork = require('child_process').fork;
|
||||
|
||||
(function(module) {
|
||||
|
||||
var child = fork('./bcrypt', process.argv.slice(2), {
|
||||
env: process.env
|
||||
});
|
||||
|
||||
var callbacks = {
|
||||
'hash': {},
|
||||
'compare': {}
|
||||
};
|
||||
var fork = require('child_process').fork;
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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));
|
||||
return module;
|
||||
})(exports);
|
||||
Reference in New Issue
Block a user