18 lines
725 B
TypeScript
18 lines
725 B
TypeScript
import type { PinRecord } from './storage.js';
|
|
|
|
export const PIN_LENGTH = 4;
|
|
|
|
async function sha256Hex(text: string): Promise<string> {
|
|
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
}
|
|
|
|
export async function createPinRecord(pin: string): Promise<PinRecord> {
|
|
const salt = [...crypto.getRandomValues(new Uint8Array(16))].map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
return { salt, hash: await sha256Hex(`${salt}:${pin}`) };
|
|
}
|
|
|
|
export async function checkPin(pin: string, record: PinRecord): Promise<boolean> {
|
|
return (await sha256Hex(`${record.salt}:${pin}`)) === record.hash;
|
|
}
|