UNPKG

otpauth

Version:

One Time Password (HOTP/TOTP) library for Node.js and browser

28 lines 25.3 kB
/*! otpauth v5.0.3 | (c) Héctor Molinero Fernández <hector@molinero.dev> | https://github.com/hectorm/otpauth | MIT */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.OTPAuth=e():t.OTPAuth=e()}(this,(function(){return function(t){var e={};function r(i){if(e[i])return e[i].exports;var n=e[i]={i:i,l:!1,exports:{}};return t[i].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=t,r.c=e,r.d=function(t,e,i){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(r.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)r.d(i,n,function(e){return t[e]}.bind(null,n));return i},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){ return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(module,__webpack_exports__,__webpack_require__){"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}__webpack_require__.d(__webpack_exports__,"b",(function(){return Utils})),__webpack_require__.d(__webpack_exports__,"a",(function(){return InternalUtils}));var Utils={uint:{fromBuf:function(t){for(var e=new Uint8Array(t),r=0,i=0;i<e.length;i++)0!==e[i]&&(r*=256,r+=e[i]);return r},toBuf:function(t){for(var e=new ArrayBuffer(8),r=new Uint8Array(e),i=t,n=7;n>=0&&0!==i;n--)r[n]=255&i,i-=r[n],i/=256;return e}},raw:{fromBuf:function(t){for(var e=new Uint8Array(t),r="",i=0;i<e.length;i++)r+=String.fromCharCode(e[i]);return r},toBuf:function(t){ for(var e=new ArrayBuffer(t.length),r=new Uint8Array(e),i=0;i<t.length;i++)r[i]=t.charCodeAt(i);return e}},b32:{alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",fromBuf:function(t){for(var e=new Uint8Array(t),r=0,i=0,n="",o=0;o<e.length;o++)for(i=i<<8|e[o],r+=8;r>=5;)n+=Utils.b32.alphabet[i>>>r-5&31],r-=5;return r>0&&(n+=Utils.b32.alphabet[i<<5-r&31]),n},toBuf:function(t){t=t.toUpperCase().replace(/=+$/,"");for(var e=new ArrayBuffer(5*t.length/8|0),r=new Uint8Array(e),i=0,n=0,o=0,a=0;a<t.length;a++){var s=Utils.b32.alphabet.indexOf(t[a]);if(-1===s)throw new TypeError("Invalid character found: ".concat(t[a]));n=n<<5|s,(i+=5)>=8&&(r[o++]=n>>>i-8&255,i-=8)}return e}},hex:{fromBuf:function(t){for(var e=new Uint8Array(t),r="",i=0;i<e.length;i++){var n=e[i].toString(16);r+=2===n.length?n:"0".concat(n)}return r.toUpperCase()},toBuf:function(t){for(var e=new ArrayBuffer(t.length/2),r=new Uint8Array(e),i=0,n=0;i<r.length;i+=1,n+=2)r[i]=parseInt(t.substr(n,2),16);return e}},pad:function(t,e){ for(var r="",i=e-String(t).length;i-- >0;)r+="0";return"".concat(r).concat(t)}},InternalUtils={get globalThis(){var t;if("object"===("undefined"==typeof globalThis?"undefined":_typeof(globalThis)))t=globalThis;else{Object.defineProperty(Object.prototype,"__magicalGlobalThis__",{get:function(){return this},configurable:!0});try{t=__magicalGlobalThis__}finally{delete Object.prototype.__magicalGlobalThis__}}return Object.defineProperty(this,"globalThis",{enumerable:!0,value:t}),this.globalThis},get console(){var t;if("object"===_typeof(InternalUtils.globalThis.console))t=InternalUtils.globalThis.console;else{t={};for(var e=0,r=["assert","clear","count","countReset","debug","error","info","log","table","trace","warn","dir","dirxml","group","groupCollapsed","groupEnd","time","timeLog","timeEnd","exception","timeStamp","profile","profileEnd"];e<r.length;e++){t[r[e]]=function(){}}for(var i=0,n=["memory"];i<n.length;i++){t[n[i]]={}}}return Object.defineProperty(this,"console",{enumerable:!0,value:t}),this.console}, get isNode(){var t="[object process]"===Object.prototype.toString.call(InternalUtils.globalThis.process);return Object.defineProperty(this,"isNode",{enumerable:!0,value:t}),this.isNode},nodeRequire:function nodeRequire(name){return eval("require")(name)}}},function(t,e,r){"use strict";r.r(e);var i,n=r(0),o={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(t){this.toString=function(){return"CORRUPT: "+this.message},this.message=t},invalid:function(t){this.toString=function(){return"INVALID: "+this.message},this.message=t},bug:function(t){this.toString=function(){return"BUG: "+this.message},this.message=t},notReady:function(t){this.toString=function(){return"NOT READY: "+this.message},this.message=t}}};o.bitArray={bitSlice:function(t,e,r){return t=o.bitArray._shiftRight(t.slice(e/32),32-(31&e)).slice(1),void 0===r?t:o.bitArray.clamp(t,r-e)},extract:function(t,e,r){var i=Math.floor(-e-r&31);return(-32&(e+r-1^e)?t[e/32|0]<<32-i^t[e/32+1|0]>>>i:t[e/32|0]>>>i)&(1<<r)-1}, concat:function(t,e){if(0===t.length||0===e.length)return t.concat(e);var r=t[t.length-1],i=o.bitArray.getPartial(r);return 32===i?t.concat(e):o.bitArray._shiftRight(e,i,0|r,t.slice(0,t.length-1))},bitLength:function(t){var e,r=t.length;return 0===r?0:(e=t[r-1],32*(r-1)+o.bitArray.getPartial(e))},clamp:function(t,e){if(32*t.length<e)return t;var r=(t=t.slice(0,Math.ceil(e/32))).length;return e&=31,r>0&&e&&(t[r-1]=o.bitArray.partial(e,t[r-1]&2147483648>>e-1,1)),t},partial:function(t,e,r){return 32===t?e:(r?0|e:e<<32-t)+1099511627776*t},getPartial:function(t){return Math.round(t/1099511627776)||32},equal:function(t,e){if(o.bitArray.bitLength(t)!==o.bitArray.bitLength(e))return!1;var r,i=0;for(r=0;r<t.length;r++)i|=t[r]^e[r];return 0===i},_shiftRight:function(t,e,r,i){var n,a,s;for(void 0===i&&(i=[]);e>=32;e-=32)i.push(r),r=0;if(0===e)return i.concat(t);for(n=0;n<t.length;n++)i.push(r|t[n]>>>e),r=t[n]<<32-e;return a=t.length?t[t.length-1]:0,s=o.bitArray.getPartial(a), i.push(o.bitArray.partial(e+s&31,e+s>32?r:i.pop(),1)),i},_xor4:function(t,e){return[t[0]^e[0],t[1]^e[1],t[2]^e[2],t[3]^e[3]]},byteswapM:function(t){var e,r;for(e=0;e<t.length;++e)r=t[e],t[e]=r>>>24|r>>>8&65280|(65280&r)<<8|r<<24;return t}},"undefined"==typeof ArrayBuffer&&((i=void 0).ArrayBuffer=function(){},i.DataView=function(){}),o.codec.arrayBuffer={fromBits:function(t,e,r){var i,n,a,s,u;if(e=null==e||e,r=r||8,0===t.length)return new ArrayBuffer(0);if(a=o.bitArray.bitLength(t)/8,o.bitArray.bitLength(t)%8!=0)throw new o.exception.invalid("Invalid bit size, must be divisble by 8 to fit in an arraybuffer correctly");for(e&&a%r!=0&&(a+=r-a%r),s=new DataView(new ArrayBuffer(4*t.length)),n=0;n<t.length;n++)s.setUint32(4*n,t[n]<<32);if((i=new DataView(new ArrayBuffer(a))).byteLength===s.byteLength)return s.buffer;for(u=s.byteLength<i.byteLength?s.byteLength:i.byteLength,n=0;n<u;n++)i.setUint8(n,s.getUint8(n));return i.buffer},toBits:function(t){var e,r,i,n=[];if(0===t.byteLength)return[] ;e=(r=new DataView(t)).byteLength-r.byteLength%4;for(var a=0;a<e;a+=4)n.push(r.getUint32(a));if(r.byteLength%4!=0){i=new DataView(new ArrayBuffer(4));a=0;for(var s=r.byteLength%4;a<s;a++)i.setUint8(a+4-s,r.getUint8(e+a));n.push(o.bitArray.partial(r.byteLength%4*8,i.getUint32(0)))}return n},hexDumpBuffer:function(t){for(var e,r,i=new DataView(t),n="",o=0;o<i.byteLength;o+=2)o%16==0&&(n+="\n"+o.toString(16)+"\t"),n+=(e=i.getUint16(o).toString(16),((e+="").length>=(r=4)?e:new Array(r-e.length+1).join("0")+e)+" ");void 0===typeof console&&(console=console||{log:function(){}}),console.log(n.toUpperCase())}},o.hash.sha1=function(t){t?(this._h=t._h.slice(0),this._buffer=t._buffer.slice(0),this._length=t._length):this.reset()},o.hash.sha1.hash=function(t){return(new o.hash.sha1).update(t).finalize()},o.hash.sha1.prototype={blockSize:512,reset:function(){return this._h=this._init.slice(0),this._buffer=[],this._length=0,this},update:function(t){"string"==typeof t&&(t=o.codec.utf8String.toBits(t)) ;var e,r=this._buffer=o.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+o.bitArray.bitLength(t);if(n>9007199254740991)throw new o.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var a=new Uint32Array(r),s=0;for(e=this.blockSize+i-(this.blockSize+i&this.blockSize-1);e<=n;e+=this.blockSize)this._block(a.subarray(16*s,16*(s+1))),s+=1;r.splice(0,16*s)}else for(e=this.blockSize+i-(this.blockSize+i&this.blockSize-1);e<=n;e+=this.blockSize)this._block(r.splice(0,16));return this},finalize:function(){var t,e=this._buffer,r=this._h;for(t=(e=o.bitArray.concat(e,[o.bitArray.partial(1,1)])).length+2;15&t;t++)e.push(0);for(e.push(Math.floor(this._length/4294967296)),e.push(0|this._length);e.length;)this._block(e.splice(0,16));return this.reset(),r},_init:[1732584193,4023233417,2562383102,271733878,3285377520],_key:[1518500249,1859775393,2400959708,3395469782],_f:function(t,e,r,i){return t<=19?e&r|~e&i:t<=39?e^r^i:t<=59?e&r|e&i|r&i:t<=79?e^r^i:void 0}, _S:function(t,e){return e<<t|e>>>32-t},_block:function(t){var e,r,i,n,o,a,s,u,h=this._h;if("undefined"!=typeof Uint32Array){u=Array(80);for(var f=0;f<16;f++)u[f]=t[f]}else u=t;for(i=h[0],n=h[1],o=h[2],a=h[3],s=h[4],e=0;e<=79;e++)e>=16&&(u[e]=this._S(1,u[e-3]^u[e-8]^u[e-14]^u[e-16])),r=this._S(5,i)+this._f(e,n,o,a)+s+u[e]+this._key[Math.floor(e/20)]|0,s=a,a=o,o=this._S(30,n),n=i,i=r;h[0]=h[0]+i|0,h[1]=h[1]+n|0,h[2]=h[2]+o|0,h[3]=h[3]+a|0,h[4]=h[4]+s|0}},o.hash.sha256=function(t){this._key[0]||this._precompute(),t?(this._h=t._h.slice(0),this._buffer=t._buffer.slice(0),this._length=t._length):this.reset()},o.hash.sha256.hash=function(t){return(new o.hash.sha256).update(t).finalize()},o.hash.sha256.prototype={blockSize:512,reset:function(){return this._h=this._init.slice(0),this._buffer=[],this._length=0,this},update:function(t){"string"==typeof t&&(t=o.codec.utf8String.toBits(t));var e,r=this._buffer=o.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+o.bitArray.bitLength(t) ;if(n>9007199254740991)throw new o.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var a=new Uint32Array(r),s=0;for(e=512+i-(512+i&511);e<=n;e+=512)this._block(a.subarray(16*s,16*(s+1))),s+=1;r.splice(0,16*s)}else for(e=512+i-(512+i&511);e<=n;e+=512)this._block(r.splice(0,16));return this},finalize:function(){var t,e=this._buffer,r=this._h;for(t=(e=o.bitArray.concat(e,[o.bitArray.partial(1,1)])).length+2;15&t;t++)e.push(0);for(e.push(Math.floor(this._length/4294967296)),e.push(0|this._length);e.length;)this._block(e.splice(0,16));return this.reset(),r},_init:[],_key:[],_precompute:function(){var t,e,r=0,i=2;function n(t){return 4294967296*(t-Math.floor(t))|0}for(;r<64;i++){for(e=!0,t=2;t*t<=i;t++)if(i%t==0){e=!1;break}e&&(r<8&&(this._init[r]=n(Math.pow(i,.5))),this._key[r]=n(Math.pow(i,1/3)),r++)}},_block:function(t){var e,r,i,n,o=this._h,a=this._key,s=o[0],u=o[1],h=o[2],f=o[3],c=o[4],l=o[5],p=o[6],d=o[7];for(e=0;e<64;e++)e<16?r=t[e]:(i=t[e+1&15],n=t[e+14&15], r=t[15&e]=(i>>>7^i>>>18^i>>>3^i<<25^i<<14)+(n>>>17^n>>>19^n>>>10^n<<15^n<<13)+t[15&e]+t[e+9&15]|0),r=r+d+(c>>>6^c>>>11^c>>>25^c<<26^c<<21^c<<7)+(p^c&(l^p))+a[e],d=p,p=l,l=c,c=f+r|0,f=h,h=u,s=r+((u=s)&h^f&(u^h))+(u>>>2^u>>>13^u>>>22^u<<30^u<<19^u<<10)|0;o[0]=o[0]+s|0,o[1]=o[1]+u|0,o[2]=o[2]+h|0,o[3]=o[3]+f|0,o[4]=o[4]+c|0,o[5]=o[5]+l|0,o[6]=o[6]+p|0,o[7]=o[7]+d|0}},o.hash.sha512=function(t){this._key[0]||this._precompute(),t?(this._h=t._h.slice(0),this._buffer=t._buffer.slice(0),this._length=t._length):this.reset()},o.hash.sha512.hash=function(t){return(new o.hash.sha512).update(t).finalize()},o.hash.sha512.prototype={blockSize:1024,reset:function(){return this._h=this._init.slice(0),this._buffer=[],this._length=0,this},update:function(t){"string"==typeof t&&(t=o.codec.utf8String.toBits(t));var e,r=this._buffer=o.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+o.bitArray.bitLength(t);if(n>9007199254740991)throw new o.exception.invalid("Cannot hash more than 2^53 - 1 bits") ;if("undefined"!=typeof Uint32Array){var a=new Uint32Array(r),s=0;for(e=1024+i-(1024+i&1023);e<=n;e+=1024)this._block(a.subarray(32*s,32*(s+1))),s+=1;r.splice(0,32*s)}else for(e=1024+i-(1024+i&1023);e<=n;e+=1024)this._block(r.splice(0,32));return this},finalize:function(){var t,e=this._buffer,r=this._h;for(t=(e=o.bitArray.concat(e,[o.bitArray.partial(1,1)])).length+4;31&t;t++)e.push(0);for(e.push(0),e.push(0),e.push(Math.floor(this._length/4294967296)),e.push(0|this._length);e.length;)this._block(e.splice(0,32));return this.reset(),r},_init:[],_initr:[12372232,13281083,9762859,1914609,15106769,4090911,4308331,8266105],_key:[], _keyr:[2666018,15689165,5061423,9034684,4764984,380953,1658779,7176472,197186,7368638,14987916,16757986,8096111,1480369,13046325,6891156,15813330,5187043,9229749,11312229,2818677,10937475,4324308,1135541,6741931,11809296,16458047,15666916,11046850,698149,229999,945776,13774844,2541862,12856045,9810911,11494366,7844520,15576806,8533307,15795044,4337665,16291729,5553712,15684120,6662416,7413802,12308920,13816008,4303699,9366425,10176680,13195875,4295371,6546291,11712675,15708924,1519456,15772530,6568428,6495784,8568297,13007125,7492395,2515356,12632583,14740254,7262584,1535930,13146278,16321966,1853211,294276,13051027,13221564,1051980,4080310,6651434,14088940,4675607],_precompute:function(){var t,e,r=0,i=2;function n(t){return 4294967296*(t-Math.floor(t))|0}function o(t){return 1099511627776*(t-Math.floor(t))&255}for(;r<80;i++){for(e=!0,t=2;t*t<=i;t++)if(i%t==0){e=!1;break}e&&(r<8&&(this._init[2*r]=n(Math.pow(i,.5)),this._init[2*r+1]=o(Math.pow(i,.5))<<24|this._initr[r]),this._key[2*r]=n(Math.pow(i,1/3)), this._key[2*r+1]=o(Math.pow(i,1/3))<<24|this._keyr[r],r++)}},_block:function(t){var e,r,i,n,o=this._h,a=this._key,s=o[0],u=o[1],h=o[2],f=o[3],c=o[4],l=o[5],p=o[6],d=o[7],g=o[8],b=o[9],y=o[10],v=o[11],_=o[12],m=o[13],w=o[14],k=o[15];if("undefined"!=typeof Uint32Array){n=Array(160);for(var A=0;A<32;A++)n[A]=t[A]}else n=t;var U=s,B=u,S=h,T=f,I=c,O=l,x=p,P=d,C=g,R=b,M=y,j=v,L=_,z=m,E=w,H=k;for(e=0;e<80;e++){if(e<16)r=n[2*e],i=n[2*e+1];else{var D=n[2*(e-15)],V=n[2*(e-15)+1],q=(V<<31|D>>>1)^(V<<24|D>>>8)^D>>>7,N=(D<<31|V>>>1)^(D<<24|V>>>8)^(D<<25|V>>>7),$=n[2*(e-2)],G=n[2*(e-2)+1],Y=(G<<13|$>>>19)^($<<3|G>>>29)^$>>>6,Z=($<<13|G>>>19)^(G<<3|$>>>29)^($<<26|G>>>6),F=n[2*(e-7)],J=n[2*(e-7)+1],K=n[2*(e-16)],Q=n[2*(e-16)+1];r=q+F+((i=N+J)>>>0<N>>>0?1:0),r+=Y+((i+=Z)>>>0<Z>>>0?1:0),r+=K+((i+=Q)>>>0<Q>>>0?1:0)}n[2*e]=r|=0,n[2*e+1]=i|=0 ;var W=C&M^~C&L,X=R&j^~R&z,tt=U&S^U&I^S&I,et=B&T^B&O^T&O,rt=(B<<4|U>>>28)^(U<<30|B>>>2)^(U<<25|B>>>7),it=(U<<4|B>>>28)^(B<<30|U>>>2)^(B<<25|U>>>7),nt=(R<<18|C>>>14)^(R<<14|C>>>18)^(C<<23|R>>>9),ot=(C<<18|R>>>14)^(C<<14|R>>>18)^(R<<23|C>>>9),at=a[2*e],st=a[2*e+1],ut=H+ot,ht=E+nt+(ut>>>0<H>>>0?1:0);ht+=W+((ut+=X)>>>0<X>>>0?1:0),ht+=at+((ut+=st)>>>0<st>>>0?1:0);var ft=it+et;E=L,H=z,L=M,z=j,M=C,j=R,C=x+(ht+=r+((ut=ut+i|0)>>>0<i>>>0?1:0))+((R=P+ut|0)>>>0<P>>>0?1:0)|0,x=I,P=O,I=S,O=T,S=U,T=B,U=ht+(rt+tt+(ft>>>0<it>>>0?1:0))+((B=ut+ft|0)>>>0<ut>>>0?1:0)|0}u=o[1]=u+B|0,o[0]=s+U+(u>>>0<B>>>0?1:0)|0,f=o[3]=f+T|0,o[2]=h+S+(f>>>0<T>>>0?1:0)|0,l=o[5]=l+O|0,o[4]=c+I+(l>>>0<O>>>0?1:0)|0,d=o[7]=d+P|0,o[6]=p+x+(d>>>0<P>>>0?1:0)|0,b=o[9]=b+R|0,o[8]=g+C+(b>>>0<R>>>0?1:0)|0,v=o[11]=v+j|0,o[10]=y+M+(v>>>0<j>>>0?1:0)|0,m=o[13]=m+z|0,o[12]=_+L+(m>>>0<z>>>0?1:0)|0,k=o[15]=k+H|0,o[14]=w+E+(k>>>0<H>>>0?1:0)|0}},o.misc.hmac=function(t,e){this._hash=e=e||o.hash.sha256;var r,i=[[],[]],n=e.prototype.blockSize/32 ;for(this._baseHash=[new e,new e],t.length>n&&(t=e.hash(t)),r=0;r<n;r++)i[0][r]=909522486^t[r],i[1][r]=1549556828^t[r];this._baseHash[0].update(i[0]),this._baseHash[1].update(i[1]),this._resultHash=new e(this._baseHash[0])},o.misc.hmac.prototype.encrypt=o.misc.hmac.prototype.mac=function(t){if(this._updated)throw new o.exception.invalid("encrypt on already updated hmac called!");return this.update(t),this.digest(t)},o.misc.hmac.prototype.reset=function(){this._resultHash=new this._hash(this._baseHash[0]),this._updated=!1},o.misc.hmac.prototype.update=function(t){this._updated=!0,this._resultHash.update(t)},o.misc.hmac.prototype.digest=function(){var t=this._resultHash.finalize(),e=new this._hash(this._baseHash[1]).update(t).finalize();return this.reset(),e};var a,s,u=o;if(n.a.isNode){var h,f,c=n.a.globalThis.Buffer,l=n.a.nodeRequire("crypto");h="function"==typeof c.from?c.from:function(t){for(var e=new c(t.byteLength),r=new Uint8Array(t),i=0;i<r.length;i++)e[i]=r[i];return e}, f=c.prototype instanceof Uint8Array?function(t){return t.buffer}:function(t){for(var e=new Uint8Array(t.length),r=0;r<e.length;r++)e[r]=t[r];return e.buffer},a=function(t){var e=l.randomBytes(t);return f(e)},s=function(t,e,r){var i=l.createHmac(t,h(e));return i.update(h(r)),f(i.digest())}}else{var p,d=n.a.globalThis.crypto||n.a.globalThis.msCrypto;void 0!==d&&"function"==typeof d.getRandomValues?p=function(t){d.getRandomValues(t)}:(n.a.console.warn("Cryptography API not available, falling back to 'Math.random'..."),p=function(t){for(var e=0;e<t.length;e++)t[e]=Math.floor(256*Math.random())}),a=function(t){var e=new Uint8Array(t);return p(e),e.buffer},s=function(t,e,r){var i=u.hash[t.toLowerCase()];if(void 0===i)throw new TypeError("Unknown hash function");var n=new u.misc.hmac(u.codec.arrayBuffer.toBits(e),i);return n.update(u.codec.arrayBuffer.toBits(r)),u.codec.arrayBuffer.fromBits(n.digest(),!1)}}var g={randomBytes:a,hmacDigest:s};function b(t,e){for(var r=0;r<e.length;r++){var i=e[r] ;i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var y=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.buffer,i=e.size,n=void 0===i?20:i;!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),this.buffer=void 0===r?g.randomBytes(n):r}var e,r,i;return e=t,i=[{key:"fromRaw",value:function(e){return new t({buffer:n.b.raw.toBuf(e)})}},{key:"fromB32",value:function(e){return new t({buffer:n.b.b32.toBuf(e)})}},{key:"fromHex",value:function(e){return new t({buffer:n.b.hex.toBuf(e)})}}],(r=[{key:"raw",get:function(){return Object.defineProperty(this,"raw",{enumerable:!0,configurable:!0,writable:!0,value:n.b.raw.fromBuf(this.buffer)}),this.raw}},{key:"b32",get:function(){return Object.defineProperty(this,"b32",{enumerable:!0,configurable:!0,writable:!0,value:n.b.b32.fromBuf(this.buffer)}),this.b32}},{key:"hex",get:function(){return Object.defineProperty(this,"hex",{ enumerable:!0,configurable:!0,writable:!0,value:n.b.hex.fromBuf(this.buffer)}),this.hex}}])&&b(e.prototype,r),i&&b(e,i),t}();function v(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,i.key,i)}}var _=new RegExp("^otpauth:\\/\\/([ht]otp)\\/(.+)\\?((?:&?(?:".concat(["issuer","secret","algorithm","digits","counter","period"].join("|"),")=[^&]+)+)$"),"i"),m=/^[2-7A-Z]+=*$/i,w=/^SHA(?:1|256|512)$/i,k=/^[+-]?\d+$/,A=/^\+?[1-9]\d*$/,U=function(){function t(){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t)}var e,r,i;return e=t,i=[{key:"parse",value:function(t){var e;try{e=t.match(_)}catch(t){}if(!Array.isArray(e))throw new URIError("Invalid URI format");var r,i=e[1].toLowerCase(),o=e[2].split(/:(.+)/,2).map(decodeURIComponent),a=e[3].split("&").reduce((function(t,e){var r=e.split(/=(.+)/,2).map(decodeURIComponent),i=r[0].toLowerCase(),n=r[1],o=t;return o[i]=n,o }),{}),s={};if("hotp"===i){if(r=O,void 0===a.counter||!k.test(a.counter))throw new TypeError("Missing or invalid 'counter' parameter");s.counter=parseInt(a.counter,10)}else{if("totp"!==i)throw new TypeError("Unknown OTP type");if(r=x,void 0!==a.period){if(!A.test(a.period))throw new TypeError("Invalid 'period' parameter");s.period=parseInt(a.period,10)}}if(2===o.length)if(s.label=o[1],void 0===a.issuer)s.issuer=o[0];else{if(a.issuer!==o[0])throw new TypeError("Invalid 'issuer' parameter");s.issuer=a.issuer}else s.label=o[0],void 0!==a.issuer&&(s.issuer=a.issuer);if(void 0===a.secret||!m.test(a.secret))throw new TypeError("Missing or invalid 'secret' parameter");if(s.secret=new y({buffer:n.b.b32.toBuf(a.secret)}),void 0!==a.algorithm){if(!w.test(a.algorithm))throw new TypeError("Invalid 'algorithm' parameter");s.algorithm=a.algorithm}if(void 0!==a.digits){if(!A.test(a.digits))throw new TypeError("Invalid 'digits' parameter");s.digits=parseInt(a.digits,10)}return new r(s)}},{key:"stringify",value:function(t){ var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=e.legacyIssuer,i=void 0===r||r,n=t instanceof O,o=t instanceof x;if(!n&&!o)throw new TypeError("Invalid 'HOTP/TOTP' object");var a="otpauth://";return a+="".concat(o?"totp":"hotp","/"),t.issuer.length>0?(i&&(a+="".concat(encodeURIComponent(t.issuer),":")),a+="".concat(encodeURIComponent(t.label),"?"),a+="issuer=".concat(encodeURIComponent(t.issuer),"&")):a+="".concat(encodeURIComponent(t.label),"?"),a+="secret=".concat(encodeURIComponent(t.secret.b32))+"&algorithm=".concat(encodeURIComponent(t.algorithm))+"&digits=".concat(encodeURIComponent(t.digits)),a+=o?"&period=".concat(encodeURIComponent(t.period)):"&counter=".concat(encodeURIComponent(t.counter))}}],(r=null)&&v(e.prototype,r),i&&v(e,i),t}();function B(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function S(t,e){for(var r=0;r<e.length;r++){var i=e[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0), Object.defineProperty(t,i.key,i)}}function T(t,e,r){return e&&S(t.prototype,e),r&&S(t,r),t}var I={issuer:"",label:"OTPAuth",algorithm:"SHA1",digits:6,counter:0,period:30,window:1},O=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.issuer,i=void 0===r?I.issuer:r,n=e.label,o=void 0===n?I.label:n,a=e.secret,s=void 0===a?new y:a,u=e.algorithm,h=void 0===u?I.algorithm:u,f=e.digits,c=void 0===f?I.digits:f,l=e.counter,p=void 0===l?I.counter:l;B(this,t),this.issuer=i,this.label=o,this.secret="string"==typeof s?y.fromB32(s):s,this.algorithm=h,this.digits=c,this.counter=p}return T(t,[{key:"generate",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.counter,i=void 0===r?this.counter++:r;return t.generate({secret:this.secret,algorithm:this.algorithm,digits:this.digits,counter:i})}},{key:"validate",value:function(e){var r=e.token,i=e.counter,o=void 0===i?this.counter:i,a=e.window;return t.validate({token:n.b.pad(r,this.digits), secret:this.secret,algorithm:this.algorithm,counter:o,window:a})}},{key:"toString",value:function(){return U.stringify(this)}}],[{key:"generate",value:function(t){var e=t.secret,r=t.algorithm,i=void 0===r?I.algorithm:r,o=t.digits,a=void 0===o?I.digits:o,s=t.counter,u=void 0===s?I.counter:s,h=new Uint8Array(g.hmacDigest(i,e.buffer,n.b.uint.toBuf(u))),f=15&h[h.byteLength-1],c=((127&h[f])<<24|(255&h[f+1])<<16|(255&h[f+2])<<8|255&h[f+3])%Math.pow(10,a);return n.b.pad(c,a)}},{key:"validate",value:function(e){for(var r=e.token,i=e.secret,n=e.algorithm,o=e.counter,a=void 0===o?I.counter:o,s=e.window,u=void 0===s?I.window:s,h=a-u;h<=a+u;++h){if(r===t.generate({secret:i,algorithm:n,digits:r.length,counter:h}))return h-a}return null}}]),t}(),x=function(){function t(){ var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.issuer,i=void 0===r?I.issuer:r,n=e.label,o=void 0===n?I.label:n,a=e.secret,s=void 0===a?new y:a,u=e.algorithm,h=void 0===u?I.algorithm:u,f=e.digits,c=void 0===f?I.digits:f,l=e.period,p=void 0===l?I.period:l;B(this,t),this.issuer=i,this.label=o,this.secret="string"==typeof s?y.fromB32(s):s,this.algorithm=h,this.digits=c,this.period=p}return T(t,[{key:"generate",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.timestamp,i=void 0===r?Date.now():r;return t.generate({secret:this.secret,algorithm:this.algorithm,digits:this.digits,period:this.period,timestamp:i})}},{key:"validate",value:function(e){var r=e.token,i=e.timestamp,o=e.window;return t.validate({token:n.b.pad(r,this.digits),secret:this.secret,algorithm:this.algorithm,period:this.period,timestamp:i,window:o})}},{key:"toString",value:function(){return U.stringify(this)}}],[{key:"generate",value:function(t){ var e=t.secret,r=t.algorithm,i=t.digits,n=t.period,o=void 0===n?I.period:n,a=t.timestamp,s=void 0===a?Date.now():a;return O.generate({secret:e,algorithm:r,digits:i,counter:Math.floor(s/1e3/o)})}},{key:"validate",value:function(t){var e=t.token,r=t.secret,i=t.algorithm,n=t.period,o=void 0===n?I.period:n,a=t.timestamp,s=void 0===a?Date.now():a,u=t.window;return O.validate({token:e,secret:r,algorithm:i,counter:Math.floor(s/1e3/o),window:u})}}]),t}();r.d(e,"HOTP",(function(){return O})),r.d(e,"TOTP",(function(){return x})),r.d(e,"URI",(function(){return U})),r.d(e,"Secret",(function(){return y})),r.d(e,"Utils",(function(){return n.b})),r.d(e,"version",(function(){return"5.0.3"}));e.default={HOTP:O,TOTP:x,URI:U,Secret:y,Utils:n.b,version:"5.0.3"}}])})); //# sourceMappingURL=otpauth.min.js.map