UNPKG

otp-io

Version:

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

18 lines • 824 B
'use strict';const hmac$1=require('./hmac.js');const algorithmMap = new Map([ [hmac$1.HmacAlgorithm.SHA1, "SHA-1"], [hmac$1.HmacAlgorithm.SHA256, "SHA-256"], [hmac$1.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); };exports.hmac=hmac;exports.randomBytes=randomBytes;