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

54 lines
1019 B
JavaScript

11 years ago
'use strict';
8 years ago
var path = require('path');
11 years ago
var fork = require('./meta/debugFork');
function hash(rounds, password, callback) {
8 years ago
forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
}
exports.hash = hash;
var fakeHashCache;
function getFakeHash(callback) {
if (fakeHashCache) {
return callback(null, fakeHashCache);
8 years ago
}
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;
10 years ago
8 years ago
function forkChild(message, callback) {
var child = fork(path.join(__dirname, 'bcrypt'));
10 years ago
8 years ago
child.on('message', function (msg) {
if (msg.err) {
return callback(new Error(msg.err));
}
8 years ago
callback(null, msg.result);
});
11 years ago
8 years ago
child.send(message);
}