UNPKG

otpauth

Version:

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

31 lines (30 loc) 26.2 kB
/*! otpauth v6.0.0 | (c) Héctor Molinero Fernández <hector@molinero.dev> | MIT | https://github.com/hectorm/otpauth */ /*! sjcl v1.0.8 | (c) bitwiseshiftleft | (BSD-2-Clause OR GPL-2.0-only) | https://github.com/bitwiseshiftleft/sjcl */ "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)}function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(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 _createClass(t,e,r){return e&&_defineProperties(t.prototype,e),r&&_defineProperties(t,r),t}function _unsupportedIterableToArray(t,e){if(t){if("string"==typeof t)return _arrayLikeToArray(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(t,e):void 0}}function _arrayLikeToArray(t,e){ (null==e||e>t.length)&&(e=t.length);for(var r=0,i=new Array(e);r<e;r++)i[r]=t[r];return i}function _createForOfIteratorHelper(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=_unsupportedIterableToArray(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var i=0,n=function(){};return{s:n,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:n}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){r=t[Symbol.iterator]()},n:function(){var t=r.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw o}}}}Object.defineProperty(exports,"__esModule",{value:!0});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 void 0===t&&("undefined"!=typeof self?t=self:"undefined"!=typeof window?t=window:"undefined"!=typeof global&&(t=global)),Object.defineProperty(this,"globalThis",{enumerable:!0,value:t}),this.globalThis},get console(){ var t={},e=["assert","clear","context","count","countReset","debug","dir","dirxml","error","exception","group","groupCollapsed","groupEnd","info","log","profile","profileEnd","table","time","timeEnd","timeLog","timeStamp","trace","warn"];if("object"===_typeof(InternalUtils.globalThis.console)){var r,i=_createForOfIteratorHelper(e);try{for(i.s();!(r=i.n()).done;){var n=r.value;t[n]="function"==typeof InternalUtils.globalThis.console[n]?InternalUtils.globalThis.console[n]:function(){}}}catch(t){i.e(t)}finally{i.f()}}else{var o,a=_createForOfIteratorHelper(e);try{for(a.s();!(o=a.n()).done;){t[o.value]=function(){}}}catch(t){a.e(t)}finally{a.f()}}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},get nodeRequire(){var _nodeRequire=InternalUtils.isNode?eval("require"):function(){} ;return Object.defineProperty(this,"nodeRequire",{enumerable:!0,value:_nodeRequire}),this.nodeRequire}},sjcl={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}}},globals,randomBytes,hmacDigest;if(sjcl.bitArray={bitSlice:function(t,e,r){return t=sjcl.bitArray._shiftRight(t.slice(e/32),32-(31&e)).slice(1),void 0===r?t:sjcl.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=sjcl.bitArray.getPartial(r) ;return 32===i?t.concat(e):sjcl.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)+sjcl.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]=sjcl.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(sjcl.bitArray.bitLength(t)!==sjcl.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,o,a;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 o=t.length?t[t.length-1]:0,a=sjcl.bitArray.getPartial(o),i.push(sjcl.bitArray.partial(e+a&31,e+a>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&&(globals=void 0,globals.ArrayBuffer=function(){},globals.DataView=function(){}),sjcl.codec.arrayBuffer={fromBits:function(t,e,r){var i,n,o,a,s;if(e=null==e||e,r=r||8,0===t.length)return new ArrayBuffer(0);if(o=sjcl.bitArray.bitLength(t)/8,sjcl.bitArray.bitLength(t)%8!=0)throw new sjcl.exception.invalid("Invalid bit size, must be divisble by 8 to fit in an arraybuffer correctly");for(e&&o%r!=0&&(o+=r-o%r),a=new DataView(new ArrayBuffer(4*t.length)),n=0;n<t.length;n++)a.setUint32(4*n,t[n]<<32);if((i=new DataView(new ArrayBuffer(o))).byteLength===a.byteLength)return a.buffer;for(s=a.byteLength<i.byteLength?a.byteLength:i.byteLength,n=0;n<s;n++)i.setUint8(n,a.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 o=0;o<e;o+=4)n.push(r.getUint32(o));if(r.byteLength%4!=0){ i=new DataView(new ArrayBuffer(4));o=0;for(var a=r.byteLength%4;o<a;o++)i.setUint8(o+4-a,r.getUint8(e+o));n.push(sjcl.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())}},sjcl.hash.sha1=function(t){t?(this._h=t._h.slice(0),this._buffer=t._buffer.slice(0),this._length=t._length):this.reset()},sjcl.hash.sha1.hash=function(t){return(new sjcl.hash.sha1).update(t).finalize()},sjcl.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=sjcl.codec.utf8String.toBits(t));var e,r=this._buffer=sjcl.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+sjcl.bitArray.bitLength(t) ;if(n>9007199254740991)throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var o=new Uint32Array(r),a=0;for(e=this.blockSize+i-(this.blockSize+i&this.blockSize-1);e<=n;e+=this.blockSize)this._block(o.subarray(16*a,16*(a+1))),a+=1;r.splice(0,16*a)}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=sjcl.bitArray.concat(e,[sjcl.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,l,h=this._h ;if("undefined"!=typeof Uint32Array){l=Array(80);for(var u=0;u<16;u++)l[u]=t[u]}else l=t;for(i=h[0],n=h[1],o=h[2],a=h[3],s=h[4],e=0;e<=79;e++)e>=16&&(l[e]=this._S(1,l[e-3]^l[e-8]^l[e-14]^l[e-16])),r=this._S(5,i)+this._f(e,n,o,a)+s+l[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}},sjcl.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()},sjcl.hash.sha256.hash=function(t){return(new sjcl.hash.sha256).update(t).finalize()},sjcl.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=sjcl.codec.utf8String.toBits(t));var e,r=this._buffer=sjcl.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+sjcl.bitArray.bitLength(t) ;if(n>9007199254740991)throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var o=new Uint32Array(r),a=0;for(e=512+i-(512+i&511);e<=n;e+=512)this._block(o.subarray(16*a,16*(a+1))),a+=1;r.splice(0,16*a)}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=sjcl.bitArray.concat(e,[sjcl.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],l=o[1],h=o[2],u=o[3],f=o[4],c=o[5],d=o[6],p=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+p+(f>>>6^f>>>11^f>>>25^f<<26^f<<21^f<<7)+(d^f&(c^d))+a[e],p=d,d=c,c=f,f=u+r|0,u=h,h=l,s=r+((l=s)&h^u&(l^h))+(l>>>2^l>>>13^l>>>22^l<<30^l<<19^l<<10)|0;o[0]=o[0]+s|0,o[1]=o[1]+l|0,o[2]=o[2]+h|0,o[3]=o[3]+u|0,o[4]=o[4]+f|0,o[5]=o[5]+c|0,o[6]=o[6]+d|0,o[7]=o[7]+p|0}},sjcl.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()},sjcl.hash.sha512.hash=function(t){return(new sjcl.hash.sha512).update(t).finalize()},sjcl.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=sjcl.codec.utf8String.toBits(t));var e,r=this._buffer=sjcl.bitArray.concat(this._buffer,t),i=this._length,n=this._length=i+sjcl.bitArray.bitLength(t) ;if(n>9007199254740991)throw new sjcl.exception.invalid("Cannot hash more than 2^53 - 1 bits");if("undefined"!=typeof Uint32Array){var o=new Uint32Array(r),a=0;for(e=1024+i-(1024+i&1023);e<=n;e+=1024)this._block(o.subarray(32*a,32*(a+1))),a+=1;r.splice(0,32*a)}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=sjcl.bitArray.concat(e,[sjcl.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],l=o[1],h=o[2],u=o[3],f=o[4],c=o[5],d=o[6],p=o[7],g=o[8],y=o[9],b=o[10],v=o[11],m=o[12],_=o[13],w=o[14],A=o[15];if("undefined"!=typeof Uint32Array){n=Array(160);for(var j=0;j<32;j++)n[j]=t[j]}else n=t;var U=s,T=l,k=h,B=u,S=f,I=c,E=d,R=p,C=g,O=y,P=b,L=v,M=m,x=_,H=w,G=A;for(e=0;e<80;e++){if(e<16)r=n[2*e],i=n[2*e+1];else{var z=n[2*(e-15)],N=n[2*(e-15)+1],D=(N<<31|z>>>1)^(N<<24|z>>>8)^z>>>7,V=(z<<31|N>>>1)^(z<<24|N>>>8)^(z<<25|N>>>7),X=n[2*(e-2)],q=n[2*(e-2)+1],F=(q<<13|X>>>19)^(X<<3|q>>>29)^X>>>6,$=(X<<13|q>>>19)^(q<<3|X>>>29)^(X<<26|q>>>6),Y=n[2*(e-7)],Z=n[2*(e-7)+1],J=n[2*(e-16)],K=n[2*(e-16)+1];r=D+Y+((i=V+Z)>>>0<V>>>0?1:0),r+=F+((i+=$)>>>0<$>>>0?1:0),r+=J+((i+=K)>>>0<K>>>0?1:0)}n[2*e]=r|=0,n[2*e+1]=i|=0 ;var Q=C&P^~C&M,W=O&L^~O&x,tt=U&k^U&S^k&S,et=T&B^T&I^B&I,rt=(T<<4|U>>>28)^(U<<30|T>>>2)^(U<<25|T>>>7),it=(U<<4|T>>>28)^(T<<30|U>>>2)^(T<<25|U>>>7),nt=(O<<18|C>>>14)^(O<<14|C>>>18)^(C<<23|O>>>9),ot=(C<<18|O>>>14)^(C<<14|O>>>18)^(O<<23|C>>>9),at=a[2*e],st=a[2*e+1],lt=G+ot,ht=H+nt+(lt>>>0<G>>>0?1:0);ht+=Q+((lt+=W)>>>0<W>>>0?1:0),ht+=at+((lt+=st)>>>0<st>>>0?1:0);var ut=it+et;H=M,G=x,M=P,x=L,P=C,L=O,C=E+(ht+=r+((lt=lt+i|0)>>>0<i>>>0?1:0))+((O=R+lt|0)>>>0<R>>>0?1:0)|0,E=S,R=I,S=k,I=B,k=U,B=T,U=ht+(rt+tt+(ut>>>0<it>>>0?1:0))+((T=lt+ut|0)>>>0<lt>>>0?1:0)|0}l=o[1]=l+T|0,o[0]=s+U+(l>>>0<T>>>0?1:0)|0,u=o[3]=u+B|0,o[2]=h+k+(u>>>0<B>>>0?1:0)|0,c=o[5]=c+I|0,o[4]=f+S+(c>>>0<I>>>0?1:0)|0,p=o[7]=p+R|0,o[6]=d+E+(p>>>0<R>>>0?1:0)|0,y=o[9]=y+O|0,o[8]=g+C+(y>>>0<O>>>0?1:0)|0,v=o[11]=v+L|0,o[10]=b+P+(v>>>0<L>>>0?1:0)|0,_=o[13]=_+x|0,o[12]=m+M+(_>>>0<x>>>0?1:0)|0,A=o[15]=A+G|0,o[14]=w+H+(A>>>0<G>>>0?1:0)|0}},sjcl.misc.hmac=function(t,e){this._hash=e=e||sjcl.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])},sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(t){if(this._updated)throw new sjcl.exception.invalid("encrypt on already updated hmac called!");return this.update(t),this.digest(t)},sjcl.misc.hmac.prototype.reset=function(){this._resultHash=new this._hash(this._baseHash[0]),this._updated=!1},sjcl.misc.hmac.prototype.update=function(t){this._updated=!0,this._resultHash.update(t)},sjcl.misc.hmac.prototype.digest=function(){var t=this._resultHash.finalize(),e=new this._hash(this._baseHash[1]).update(t).finalize();return this.reset(),e},InternalUtils.isNode){var NodeBuffer=InternalUtils.globalThis.Buffer,NodeCrypto=InternalUtils.nodeRequire("crypto"),nodeBufferFromArrayBuffer,nodeBufferToArrayBuffer ;nodeBufferFromArrayBuffer="function"==typeof NodeBuffer.from?NodeBuffer.from:function(t){for(var e=new NodeBuffer(t.byteLength),r=new Uint8Array(t),i=0;i<r.length;i++)e[i]=r[i];return e},nodeBufferToArrayBuffer=NodeBuffer.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},randomBytes=function(t){var e=NodeCrypto.randomBytes(t);return nodeBufferToArrayBuffer(e)},hmacDigest=function(t,e,r){var i=NodeCrypto.createHmac(t,nodeBufferFromArrayBuffer(e));return i.update(nodeBufferFromArrayBuffer(r)),nodeBufferToArrayBuffer(i.digest())}}else{var BrowserCrypto=InternalUtils.globalThis.crypto||InternalUtils.globalThis.msCrypto,getRandomValues;void 0!==BrowserCrypto&&"function"==typeof BrowserCrypto.getRandomValues?getRandomValues=function(t){BrowserCrypto.getRandomValues(t)}:(InternalUtils.console.warn("Cryptography API not available, falling back to 'Math.random'..."),getRandomValues=function(t){ for(var e=0;e<t.length;e++)t[e]=Math.floor(256*Math.random())}),randomBytes=function(t){var e=new Uint8Array(t);return getRandomValues(e),e.buffer},hmacDigest=function(t,e,r){var i=sjcl.hash[t.toLowerCase()];if(void 0===i)throw new TypeError("Unknown hash function");var n=new sjcl.misc.hmac(sjcl.codec.arrayBuffer.toBits(e),i);return n.update(sjcl.codec.arrayBuffer.toBits(r)),sjcl.codec.arrayBuffer.fromBits(n.digest(),!1)}}var Crypto={randomBytes:randomBytes,hmacDigest:hmacDigest},Secret=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;_classCallCheck(this,t),this.buffer=void 0===r?Crypto.randomBytes(n):r}return _createClass(t,[{key:"raw",get:function(){return Object.defineProperty(this,"raw",{enumerable:!0,configurable:!0,writable:!0,value:Utils.raw.fromBuf(this.buffer)}),this.raw}},{key:"b32",get:function(){return Object.defineProperty(this,"b32",{enumerable:!0,configurable:!0,writable:!0,value:Utils.b32.fromBuf(this.buffer)}), this.b32}},{key:"hex",get:function(){return Object.defineProperty(this,"hex",{enumerable:!0,configurable:!0,writable:!0,value:Utils.hex.fromBuf(this.buffer)}),this.hex}}],[{key:"fromRaw",value:function(e){return new t({buffer:Utils.raw.toBuf(e)})}},{key:"fromB32",value:function(e){return new t({buffer:Utils.b32.toBuf(e)})}},{key:"fromHex",value:function(e){return new t({buffer:Utils.hex.toBuf(e)})}}]),t}(),defaults={issuer:"",label:"OTPAuth",algorithm:"SHA1",digits:6,counter:0,period:30,window:1},HOTP=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.issuer,i=void 0===r?defaults.issuer:r,n=e.label,o=void 0===n?defaults.label:n,a=e.secret,s=void 0===a?new Secret:a,l=e.algorithm,h=void 0===l?defaults.algorithm:l,u=e.digits,f=void 0===u?defaults.digits:u,c=e.counter,d=void 0===c?defaults.counter:c;_classCallCheck(this,t),this.issuer=i,this.label=o,this.secret="string"==typeof s?Secret.fromB32(s):s,this.algorithm=h,this.digits=f,this.counter=d}return _createClass(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,n=void 0===i?this.counter:i,o=e.window;return t.validate({token:Utils.pad(r,this.digits),secret:this.secret,algorithm:this.algorithm,counter:n,window:o})}},{key:"toString",value:function(){var t=encodeURIComponent;return"otpauth://hotp/"+"".concat(this.issuer.length>0?"".concat(t(this.issuer),":").concat(t(this.label),"?issuer=").concat(t(this.issuer),"&"):"".concat(t(this.label),"?"))+"secret=".concat(t(this.secret.b32),"&")+"algorithm=".concat(t(this.algorithm),"&")+"digits=".concat(t(this.digits),"&")+"counter=".concat(t(this.counter))}}],[{key:"generate",value:function(t){ var e=t.secret,r=t.algorithm,i=void 0===r?defaults.algorithm:r,n=t.digits,o=void 0===n?defaults.digits:n,a=t.counter,s=void 0===a?defaults.counter:a,l=new Uint8Array(Crypto.hmacDigest(i,e.buffer,Utils.uint.toBuf(s))),h=15&l[l.byteLength-1],u=((127&l[h])<<24|(255&l[h+1])<<16|(255&l[h+2])<<8|255&l[h+3])%Math.pow(10,o);return Utils.pad(u,o)}},{key:"validate",value:function(e){for(var r=e.token,i=e.secret,n=e.algorithm,o=e.counter,a=void 0===o?defaults.counter:o,s=e.window,l=void 0===s?defaults.window:s,h=a-l;h<=a+l;++h){if(r===t.generate({secret:i,algorithm:n,digits:r.length,counter:h}))return h-a}return null}}]),t}(),TOTP=function(){function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e.issuer,i=void 0===r?defaults.issuer:r,n=e.label,o=void 0===n?defaults.label:n,a=e.secret,s=void 0===a?new Secret:a,l=e.algorithm,h=void 0===l?defaults.algorithm:l,u=e.digits,f=void 0===u?defaults.digits:u,c=e.period,d=void 0===c?defaults.period:c;_classCallCheck(this,t),this.issuer=i,this.label=o, this.secret="string"==typeof s?Secret.fromB32(s):s,this.algorithm=h,this.digits=f,this.period=d}return _createClass(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,n=e.window;return t.validate({token:Utils.pad(r,this.digits),secret:this.secret,algorithm:this.algorithm,period:this.period,timestamp:i,window:n})}},{key:"toString",value:function(){var t=encodeURIComponent;return"otpauth://totp/"+"".concat(this.issuer.length>0?"".concat(t(this.issuer),":").concat(t(this.label),"?issuer=").concat(t(this.issuer),"&"):"".concat(t(this.label),"?"))+"secret=".concat(t(this.secret.b32),"&")+"algorithm=".concat(t(this.algorithm),"&")+"digits=".concat(t(this.digits),"&")+"period=".concat(t(this.period))}}],[{key:"generate",value:function(t){ var e=t.secret,r=t.algorithm,i=t.digits,n=t.period,o=void 0===n?defaults.period:n,a=t.timestamp,s=void 0===a?Date.now():a;return HOTP.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?defaults.period:n,a=t.timestamp,s=void 0===a?Date.now():a,l=t.window;return HOTP.validate({token:e,secret:r,algorithm:i,counter:Math.floor(s/1e3/o),window:l})}}]),t}(),OTPURI_PARAMS=["issuer","secret","algorithm","digits","counter","period"],OTPURI_REGEX=new RegExp("^otpauth:\\/\\/([ht]otp)\\/(.+)\\?((?:&?(?:".concat(OTPURI_PARAMS.join("|"),")=[^&]+)+)$"),"i"),SECRET_REGEX=/^[2-7A-Z]+=*$/i,ALGORITHM_REGEX=/^SHA(?:1|256|512)$/i,INTEGER_REGEX=/^[+-]?\d+$/,POSITIVE_INTEGER_REGEX=/^\+?[1-9]\d*$/,URI=function(){function t(){_classCallCheck(this,t)}return _createClass(t,null,[{key:"parse",value:function(t){var e;try{e=t.match(OTPURI_REGEX)}catch(t){}if(!Array.isArray(e))throw new URIError("Invalid URI format") ;var r,i=e[1].toLowerCase(),n=e[2].split(/:(.+)/,2).map(decodeURIComponent),o=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}),{}),a={};if("hotp"===i){if(r=HOTP,void 0===o.counter||!INTEGER_REGEX.test(o.counter))throw new TypeError("Missing or invalid 'counter' parameter");a.counter=parseInt(o.counter,10)}else{if("totp"!==i)throw new TypeError("Unknown OTP type");if(r=TOTP,void 0!==o.period){if(!POSITIVE_INTEGER_REGEX.test(o.period))throw new TypeError("Invalid 'period' parameter");a.period=parseInt(o.period,10)}}if(2===n.length)if(a.label=n[1],void 0===o.issuer)a.issuer=n[0];else{if(o.issuer!==n[0])throw new TypeError("Invalid 'issuer' parameter");a.issuer=o.issuer}else a.label=n[0],void 0!==o.issuer&&(a.issuer=o.issuer);if(void 0===o.secret||!SECRET_REGEX.test(o.secret))throw new TypeError("Missing or invalid 'secret' parameter");if(a.secret=new Secret({buffer:Utils.b32.toBuf(o.secret)}),void 0!==o.algorithm){ if(!ALGORITHM_REGEX.test(o.algorithm))throw new TypeError("Invalid 'algorithm' parameter");a.algorithm=o.algorithm}if(void 0!==o.digits){if(!POSITIVE_INTEGER_REGEX.test(o.digits))throw new TypeError("Invalid 'digits' parameter");a.digits=parseInt(o.digits,10)}return new r(a)}},{key:"stringify",value:function(t){if(t instanceof HOTP||t instanceof TOTP)return t.toString();throw new TypeError("Invalid 'HOTP/TOTP' object")}}]),t}(),version="6.0.0";exports.HOTP=HOTP,exports.Secret=Secret,exports.TOTP=TOTP,exports.URI=URI,exports.Utils=Utils,exports.version=version; //# sourceMappingURL=otpauth.cjs.min.js.map