This commit is contained in:
seb
2026-10-07 21:23:03 +02:00
parent e02bbd65c5
commit 36dd81ee69
7 changed files with 170 additions and 91 deletions

View File

@@ -1,5 +1,24 @@
import { randomBytes } from 'node:crypto';
import { createHash, randomBytes } from 'node:crypto';
export default function newFullHash() {
return randomBytes(32).toString('hex');
const CHARSET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
export function toBase62(buffer) {
let num = BigInt('0x' + buffer.toString('hex'));
if (num === 0n) return CHARSET[0].repeat(8);
let res = '';
while (num > 0n) {
res = CHARSET[Number(num % 62n)] + res;
num /= 62n;
}
return res;
}
export default function newFullHash(buffer) {
if (buffer) {
const digest = createHash('sha256').update(buffer).digest();
return toBase62(digest).slice(0, 8);
}
const bytes = randomBytes(16);
return toBase62(bytes).slice(0, 8);
}