feat(shared): lift recovery-code primitives into shared crypto module
This commit is contained in:
@@ -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('');
|
||||
}
|
||||
Reference in New Issue
Block a user