2023-02-18 06:42:42 +01:00
|
|
|
import crypto from 'crypto';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { v4, v5 } from 'uuid';
|
2020-08-21 04:38:20 +02:00
|
|
|
import { startOfMonth } from 'date-fns';
|
2022-08-29 05:20:54 +02:00
|
|
|
import { hash } from 'next-basics';
|
2020-07-23 05:45:09 +02:00
|
|
|
|
|
|
|
export function secret() {
|
2022-12-28 06:38:23 +01:00
|
|
|
return hash(process.env.APP_SECRET || process.env.DATABASE_URL);
|
2020-07-23 05:45:09 +02:00
|
|
|
}
|
2020-07-23 00:46:05 +02:00
|
|
|
|
2020-08-21 04:38:20 +02:00
|
|
|
export function salt() {
|
2022-08-29 05:20:54 +02:00
|
|
|
const ROTATING_SALT = hash(startOfMonth(new Date()).toUTCString());
|
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
return hash(secret(), ROTATING_SALT);
|
2020-08-21 04:38:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-23 05:45:09 +02:00
|
|
|
export function uuid(...args) {
|
2020-08-08 02:19:42 +02:00
|
|
|
if (!args.length) return v4();
|
|
|
|
|
2022-10-31 19:02:37 +01:00
|
|
|
return v5(hash(...args, salt()), v5.DNS);
|
2020-07-23 00:46:05 +02:00
|
|
|
}
|
2023-02-18 06:42:42 +01:00
|
|
|
|
|
|
|
export function md5(...args) {
|
|
|
|
return crypto.createHash('md5').update(args.join('')).digest('hex');
|
|
|
|
}
|