Skip to content

Node.js based forum software built for the modern web

closed #2242

Julian Lamea2fbcfcfc21
diff --git a/bcrypt.js b/bcrypt.js
index a926310923..2d27fddcd1 100644
--- a/bcrypt.js
+++ b/bcrypt.js
@@ -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);
- }
-});
-
-function hash(rounds, password) {
- bcrypt.genSalt(rounds, function(err, salt) {
- if (err) {
- return process.send({type:'hash', err: {message: 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});
- });
+ case 'hash':
+ async.waterfall([
+ async.apply(bcrypt.genSalt, parseInt(process.argv[3], 10)),
+ function(salt, next) {
+ bcrypt.hash(process.argv[4], salt, next);
+ }
+ ], function(err, hash) {
+ if (!err) {
+ process.stdout.write(hash);
+ } else {
+ process.stderr.write(err.message);
+ }
+ });
+ break;
}
\ No newline at end of file
diff --git a/src/password.js b/src/password.js
index c4a7febca8..1b20b8dd2a 100644
--- a/src/password.js
+++ b/src/password.js
@@ -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);
\ No newline at end of file