Files
NodeBB/src/password.js

54 lines
1019 B
JavaScript
Raw Normal View History

2014-08-12 21:41:23 -04:00
'use strict';
2017-05-27 01:44:26 -04:00
var path = require('path');
2014-08-12 21:41:23 -04:00
var fork = require('./meta/debugFork');
2017-06-01 14:04:23 -06:00
function hash(rounds, password, callback) {
2017-05-27 01:44:26 -04:00
forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
}
exports.hash = hash;
2014-11-18 22:55:44 -05:00
var fakeHashCache;
function getFakeHash(callback) {
if (fakeHashCache) {
return callback(null, fakeHashCache);
2017-05-27 01:44:26 -04:00
}
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);
});
}
exports.compare = compare;
2014-10-07 19:49:18 -04:00
2017-05-27 01:44:26 -04:00
function forkChild(message, callback) {
var child = fork(path.join(__dirname, 'bcrypt'));
2014-10-07 19:49:18 -04:00
2017-05-27 01:44:26 -04:00
child.on('message', function (msg) {
if (msg.err) {
return callback(new Error(msg.err));
}
2014-11-18 22:55:44 -05:00
2017-05-27 01:44:26 -04:00
callback(null, msg.result);
});
2014-08-12 21:41:23 -04:00
2017-05-27 01:44:26 -04:00
child.send(message);
}