2014-08-12 21:41:23 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-08-30 16:16:56 -04:00
|
|
|
const path = require('path');
|
2020-11-06 08:40:00 -05:00
|
|
|
const crypto = require('crypto');
|
2023-09-26 10:48:58 -04:00
|
|
|
const workerpool = require('workerpool');
|
2014-08-12 21:41:23 -04:00
|
|
|
|
2023-09-26 10:48:58 -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) {
|
2020-11-06 08:40:00 -05:00
|
|
|
password = crypto.createHash('sha512').update(password).digest('hex');
|
2023-09-26 10:48:58 -04:00
|
|
|
return await pool.exec('hash', [password, rounds]);
|
2019-08-30 16:16:56 -04:00
|
|
|
};
|
|
|
|
|
|
2020-11-06 08:40:00 -05:00
|
|
|
exports.compare = async function (password, hash, shaWrapped) {
|
2019-08-30 16:16:56 -04:00
|
|
|
const fakeHash = await getFakeHash();
|
2020-11-06 08:40:00 -05:00
|
|
|
|
|
|
|
|
if (shaWrapped) {
|
|
|
|
|
password = crypto.createHash('sha512').update(password).digest('hex');
|
|
|
|
|
}
|
2023-09-26 10:48:58 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2024-12-09 18:18:02 -05:00
|
|
|
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);
|