Node.js based forum software built for the modern web
move bcrypt into password
Barış Soner Uşaklıff38abc2259d
diff --git a/src/bcrypt.js b/src/bcrypt.jsdeleted file mode 100644index 8cce80372e..0000000000--- a/src/bcrypt.js+++ /dev/null@@ -1,34 +0,0 @@--'use strict';--var bcrypt = require('bcryptjs');-var async = require('async');---process.on('message', function (msg) {- if (msg.type === 'hash') {- hashPassword(msg.password, msg.rounds);- } else if (msg.type === 'compare') {- bcrypt.compare(String(msg.password || ''), String(msg.hash || ''), done);- }-});--function hashPassword(password, rounds) {- async.waterfall([- function (next) {- bcrypt.genSalt(parseInt(rounds, 10), next);- },- function (salt, next) {- bcrypt.hash(password, salt, next);- },- ], done);-}--function done(err, result) {- if (err) {- process.send({ err: err.message });- return process.disconnect();- }- process.send({ result: result });- process.disconnect();-}diff --git a/src/password.js b/src/password.jsindex 439c0592d7..1d26e16e33 100644--- a/src/password.js+++ b/src/password.js@@ -1,53 +1,71 @@'use strict';var path = require('path');+var bcrypt = require('bcryptjs');+var async = require('async');var fork = require('./meta/debugFork');-function hash(rounds, password, callback) {+exports.hash = function (rounds, password, callback) {forkChild({ type: 'hash', rounds: rounds, password: password }, callback);-}+};-exports.hash = hash;+exports.compare = function (password, hash, callback) {+ async.waterfall([+ getFakeHash,+ function (fakeHash, next) {+ forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, next);+ },+ ], callback);+};var fakeHashCache;function getFakeHash(callback) {if (fakeHashCache) {return callback(null, fakeHashCache);}-- hash(12, Math.random().toString(), function (err, hash) {- if (err) {- return callback(err);- }-- fakeHashCache = hash;- callback(null, fakeHashCache);- });-}--function compare(password, hash, callback) {- getFakeHash(function (err, fakeHash) {- if (err) {- return callback(err);- }-- forkChild({ type: 'compare', password: password, hash: hash || fakeHash }, callback);- });+ async.waterfall([+ function (next) {+ exports.hash(12, Math.random().toString(), next);+ },+ function (hash, next) {+ fakeHashCache = hash;+ next(null, fakeHashCache);+ },+ ], callback);}-exports.compare = compare;-function forkChild(message, callback) {- var child = fork(path.join(__dirname, 'bcrypt'));+ var child = fork(path.join(__dirname, 'password'));child.on('message', function (msg) {- if (msg.err) {- return callback(new Error(msg.err));- }-- callback(null, msg.result);+ callback(msg.err ? new Error(msg.err) : null, msg.result);});child.send(message);}++// child process+process.on('message', function (msg) {+ if (msg.type === 'hash') {+ hashPassword(msg.password, msg.rounds);+ } else if (msg.type === 'compare') {+ bcrypt.compare(String(msg.password || ''), String(msg.hash || ''), done);+ }+});++function hashPassword(password, rounds) {+ async.waterfall([+ function (next) {+ bcrypt.genSalt(parseInt(rounds, 10), next);+ },+ function (salt, next) {+ bcrypt.hash(password, salt, next);+ },+ ], done);+}++function done(err, result) {+ process.send(err ? { err: err.message } : { result: result });+ process.disconnect();+}