UNPKG

native-browser-otp

Version:

Lightweight library for generating TOTP and HOTP codes using browser's native Web Cryptography API. Perfect for implementing two-factor authentication (2FA) in web applications.

2 lines (1 loc) 1.11 kB
(function(n,o){typeof exports=="object"&&typeof module!="undefined"?o(exports,require("base32-decode")):typeof define=="function"&&define.amd?define(["exports","base32-decode"],o):(n=typeof globalThis!="undefined"?globalThis:n||self,o(n.Ticker={},n.base32_decode))})(this,function(n,o){"use strict";function f(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var u=f(o);async function d(e,t){const i=await crypto.subtle.importKey("raw",u.default(e,"RFC4648"),{name:"HMAC",hash:{name:"SHA-1"}},!1,["sign","verify"]),r=await crypto.subtle.sign("HMAC",i,t);return new Uint8Array(r)}function s(e){const t=e[e.length-1]&15;return(e[t]&127)<<24|(e[t+1]&255)<<16|(e[t+2]&255)<<8|e[t+3]&255}async function c(e,t){const i=new Uint8Array(8);for(let a=i.length-1;a>=0;a--)i[a]=t&255,t>>=8;const r=await d(e,new Uint8Array(i)),p=s(r)%1e6;return String(p).padStart(6,"0")}async function l(e){return await c(e,Math.floor(Date.now()/3e4))}function y(){return 30-Math.floor(Date.now()/1e3)%30}n.hotp=c,n.timeLeft=y,n.totp=l,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});