var l=class extends Error{constructor(e){super(e),this.name="URIParseError"}},a=class extends l{constructor(e){super(`Invalid otpauth URI: ${e}`),this.name="InvalidURIError"}},g=class extends l{constructor(e){super(`Missing required parameter: ${e}`),this.name="MissingParameterError"}},c=class extends l{constructor(e,n){super(`Invalid value for parameter '${e}': ${n}`),this.name="InvalidParameterError"}};var m=2048,d=512,y=1024;function O(r,e){let n=r instanceof Error?r.message:String(r);return`Invalid URI encoding in ${e}: ${n}`}function u(r,e,n){if(r.length>e*3)throw new a(`${n} exceeds maximum length`);try{let t=decodeURIComponent(r);if(t.length>e)throw new a(`${n} exceeds maximum length of ${e} characters`);return t}catch(t){throw t instanceof a?t:new a(O(t,n))}}function f(r){if(r.length>m)throw new a(`URI exceeds maximum length of ${m} characters`);if(!r.startsWith("otpauth://"))throw new a(r);let e=r.slice(10),n=e.indexOf("/");if(n===-1)throw new a(r);let t=e.slice(0,n);if(t!=="hotp"&&t!=="totp")throw new c("type",t);let i=e.slice(n+1),o=i.indexOf("?"),s,p;o===-1?(s=u(i,d,"label"),p=""):(s=u(i.slice(0,o),d,"label"),p=i.slice(o+1));let I=P(p);return{type:t,label:s,params:I}}function P(r){let e={};if(!r)return e;let n=r.split("&");for(let t of n){let i=t.indexOf("=");if(i===-1)continue;let o=u(t.slice(0,i),64,"parameter key"),s=u(t.slice(i+1),y,`parameter '${o}'`);switch(o){case"secret":e.secret=s;break;case"issuer":e.issuer=s;break;case"algorithm":e.algorithm=T(s);break;case"digits":e.digits=b(s);break;case"counter":e.counter=parseInt(s,10);break;case"period":e.period=parseInt(s,10);break}}return e}function T(r){let e=r.toLowerCase();if(e==="sha1"||e==="sha-1")return"sha1";if(e==="sha256"||e==="sha-256")return"sha256";if(e==="sha512"||e==="sha-512")return"sha512";throw new c("algorithm",r)}function b(r){let e=parseInt(r,10);if(e===6||e===7||e===8)return e;throw new c("digits",r)}function h(r){let{type:e,label:n,params:t}=r,i=n.split(":").map(p=>encodeURIComponent(p)).join(":"),o=`otpauth://${e}/${i}?`,s=[];return t.secret&&s.push(`secret=${t.secret}`),t.issuer&&s.push(`issuer=${encodeURIComponent(t.issuer)}`),t.algorithm&&t.algorithm!=="sha1"&&s.push(`algorithm=${t.algorithm.toUpperCase()}`),t.digits&&t.digits!==6&&s.push(`digits=${t.digits}`),e==="hotp"&&t.counter!==void 0&&s.push(`counter=${t.counter}`),e==="totp"&&t.period!==void 0&&t.period!==30&&s.push(`period=${t.period}`),o+=s.join("&"),o}function x(r){let{issuer:e,label:n,secret:t,algorithm:i="sha1",digits:o=6,period:s=30}=r,p=e?`${e}:${n}`:n;return h({type:"totp",label:p,params:{secret:t,issuer:e,algorithm:i,digits:o,period:s}})}function U(r){let{issuer:e,label:n,secret:t,counter:i=0,algorithm:o="sha1",digits:s=6}=r,p=e?`${e}:${n}`:n;return h({type:"hotp",label:p,params:{secret:t,issuer:e,algorithm:o,digits:s,counter:i}})}export{c as InvalidParameterError,a as InvalidURIError,g as MissingParameterError,l as URIParseError,h as generate,U as generateHOTP,x as generateTOTP,f as parse,f as parseURI};
//# sourceMappingURL=index.js.map