Files
altcha-lib/lib/helpers.ts
Daniel Regeci e3ca2a5941 0.1.2
2024-03-26 15:10:16 +01:00

48 lines
1.2 KiB
TypeScript

import type { Algorithm } from './types.js';
const encoder = new TextEncoder();
if (!('crypto' in globalThis)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
globalThis.crypto = require('node:crypto').webcrypto;
}
export function ab2hex(ab: ArrayBuffer | Uint8Array) {
return [...new Uint8Array(ab)]
.map((x) => x.toString(16).padStart(2, '0'))
.join('');
}
export async function hash(algorithm: Algorithm, str: string) {
return ab2hex(
await crypto.subtle.digest(algorithm.toUpperCase(), encoder.encode(str))
);
}
export async function hmac(algorithm: Algorithm, str: string, secret: string) {
const key = await crypto.subtle.importKey(
'raw',
encoder.encode(secret),
{
name: 'HMAC',
hash: algorithm,
},
false,
['sign', 'verify']
);
return ab2hex(await crypto.subtle.sign('HMAC', key, encoder.encode(str)));
}
export function randomBytes(length: number) {
const ab = new Uint8Array(length);
crypto.getRandomValues(ab);
return ab;
}
export function randomInt(max: number) {
const ab = new Uint32Array(1);
crypto.getRandomValues(ab);
const randomNumber = ab[0] / (0xffffffff + 1);
return Math.floor(randomNumber * max + 1);
}