You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/password.js

72 lines
1.6 KiB
JavaScript

11 years ago
'use strict';
8 years ago
var path = require('path');
var bcrypt = require('bcryptjs');
var async = require('async');
11 years ago
var fork = require('./meta/debugFork');
exports.hash = function (rounds, password, callback) {
8 years ago
forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
};
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);
8 years ago
}
async.waterfall([
function (next) {
exports.hash(12, Math.random().toString(), next);
},
function (hash, next) {
fakeHashCache = hash;
next(null, fakeHashCache);
},
], callback);
}
8 years ago
function forkChild(message, callback) {
var child = fork(path.join(__dirname, 'password'));
10 years ago
8 years ago
child.on('message', function (msg) {
callback(msg.err ? new Error(msg.err) : null, msg.result);
8 years ago
});
11 years ago
8 years ago
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();
}