feat(shared): lift recovery-code primitives into shared crypto module

This commit is contained in:
byGalax
2026-05-15 21:54:57 +02:00
parent d9f3c6c562
commit eef884c782
7 changed files with 96 additions and 1 deletions
@@ -0,0 +1,22 @@
import { getCryptoBackend } from './backend';
export const RECOVERY_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
export const RECOVERY_CODE_LEN = 24;
export async function generateRecoveryCode(): Promise<string> {
const raw = getCryptoBackend().randomBytes(RECOVERY_CODE_LEN);
let out = '';
for (let i = 0; i < raw.length; i++) {
out += RECOVERY_ALPHABET[raw[i]! % RECOVERY_ALPHABET.length];
if ((i + 1) % 6 === 0 && i !== raw.length - 1) out += '-';
}
return out;
}
export function normalizeRecoveryCode(input: string): string {
return input
.toUpperCase()
.split('')
.filter((c) => RECOVERY_ALPHABET.includes(c))
.join('');
}