import type { PinRecord } from './storage.js'; export const PIN_LENGTH = 4; async function sha256Hex(text: string): Promise { 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 { 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 { return (await sha256Hex(`${record.salt}:${pin}`)) === record.hash; }