20 lines
745 B
JavaScript
20 lines
745 B
JavaScript
import crypto from 'node:crypto';
|
|
|
|
const DAY = new Intl.DateTimeFormat('en-CA', { timeZone: 'Europe/Berlin', year: 'numeric', month: '2-digit', day: '2-digit' });
|
|
|
|
// Tagessalz nur im RAM; neu erzeugt, sobald sich der Kalendertag in Europe/Berlin ändert.
|
|
export function createSalt({ now = Date.now, random = () => crypto.randomBytes(32) } = {}) {
|
|
let day = null;
|
|
let salt = null;
|
|
const current = () => {
|
|
const d = DAY.format(now());
|
|
if (d !== day) { day = d; salt = random(); }
|
|
return salt;
|
|
};
|
|
const hmac = (s) => crypto.createHmac('sha256', current()).update(s).digest('hex');
|
|
return {
|
|
visitorId: (site, ip, ua) => hmac(`${site}|${ip}|${ua}`).slice(0, 16),
|
|
rateKey: (site, ip) => hmac(`${site}|${ip}`),
|
|
};
|
|
}
|