Files
NodeBB/src/password.js

39 lines
947 B
JavaScript
Raw Normal View History

2014-08-12 21:41:23 -04:00
'use strict';
2019-08-30 16:16:56 -04:00
const path = require('path');
const crypto = require('crypto');
const workerpool = require('workerpool');
2014-08-12 21:41:23 -04:00
const pool = workerpool.pool(
path.join(__dirname, '/password_worker.js'), {
minWorkers: 1,
}
);
2019-08-30 16:16:56 -04:00
exports.hash = async function (rounds, password) {
password = crypto.createHash('sha512').update(password).digest('hex');
return await pool.exec('hash', [password, rounds]);
2019-08-30 16:16:56 -04:00
};
exports.compare = async function (password, hash, shaWrapped) {
2019-08-30 16:16:56 -04:00
const fakeHash = await getFakeHash();
if (shaWrapped) {
password = crypto.createHash('sha512').update(password).digest('hex');
}
return await pool.exec('compare', [password, hash || fakeHash]);
2019-08-30 16:16:56 -04:00
};
let fakeHashCache;
async function getFakeHash() {
if (fakeHashCache) {
return fakeHashCache;
}
const length = 18;
fakeHashCache = crypto.randomBytes(Math.ceil(length / 2))
.toString('hex').slice(0, length);
2019-08-30 16:16:56 -04:00
return fakeHashCache;
}
2019-07-16 14:17:10 -04:00
require('./promisify')(exports);