import { createHmac } from 'node:crypto' /** RFC 6238 TOTP (SHA-1, 6 digits, 30 s) — the code an authenticator app shows for `secret` (base32). */ export function totp(secret: string, now = Date.now()): string { const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567' let bits = '' for (const ch of secret.replace(/=+$/, '').toUpperCase()) {bits += alphabet.indexOf(ch).toString(2).padStart(5, '0')} const key = Buffer.from((bits.match(/.{8}/g) ?? []).map((b) => parseInt(b, 2))) const counter = Buffer.alloc(8) counter.writeBigUInt64BE(BigInt(Math.floor(now / 1000 / 30))) const hmac = createHmac('sha1', key).update(counter).digest() const offset = hmac[hmac.length - 1] & 0xf const code = (hmac.readUInt32BE(offset) & 0x7fffffff) % 1_000_000 return code.toString().padStart(6, '0') }