import * as OTP from 'https://deno.land/x/otpauth@v9.0.2/dist/otpauth.esm.js'
export default { secret(length = 64) { return [...Array(length)].map(() => Math.floor(Math.random() * 16).toString(16) ).join('') },
token(secret: string, timestamp?: number) { const totp = new OTP.TOTP({ algorithm: 'SHA1', digits: 6, period: 30, secret: OTP.Secret.fromHex(secret) }) return totp.generate({ timestamp }) },
uri(label: string, issuer: string, secret: string) { const totp = new OTP.TOTP({ issuer, label, algorithm: 'SHA1', digits: 6, period: 30, secret: OTP.Secret.fromHex(secret) }) return totp.toString() },
validate(token: string, secret: string) { const totp = new OTP.TOTP({ algorithm: 'SHA1', digits: 6, period: 30, secret: OTP.Secret.fromHex(secret) }) return totp.validate({ token }) === 0 }}