UNPKG

otp-io

Version:

🕖 Typed library to work 2fa via Google Authenticator/Time-based TOTP/Hmac-based HOTP

18 lines • 770 B
import {HmacAlgorithm}from'./hmac.mjs';const algorithmMap = new Map([ [HmacAlgorithm.SHA1, "SHA-1"], [HmacAlgorithm.SHA256, "SHA-256"], [HmacAlgorithm.SHA512, "SHA-512"] ]); const hmac = async function hmac(algorithm, key, message) { const name = algorithmMap.get(algorithm); if (!name) { throw new Error(`Invalid algorithm: "${algorithm}"`); } const cryptoKey = await crypto.subtle.importKey("raw", key, { name: "HMAC", hash: { name } }, false, ["sign"]); const signature = await crypto.subtle.sign("HMAC", cryptoKey, message); return new Uint8Array(signature); }; const randomBytes = function randomBytes(size) { const buffer = new Uint8Array(size); return crypto.getRandomValues(buffer); };export{hmac,randomBytes};