From ff38abc2259dba5ef45c93dbf5aa7c06e525986f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 31 Oct 2018 15:10:45 -0400 Subject: [PATCH] move bcrypt into password --- src/bcrypt.js | 34 --------------------- src/password.js | 78 ++++++++++++++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 64 deletions(-) delete mode 100644 src/bcrypt.js diff --git a/src/bcrypt.js b/src/bcrypt.js deleted file mode 100644 index 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.js index 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(); +}