/* A JavaScript implementation of the SHA family of hashes, as defined in FIPS PUB 180-4 and FIPS PUB 202, as well as the corresponding HMAC implementation as defined in FIPS PUB 198a Copyright Brian Turek 2008-2016 Distributed under the BSD License See http://caligatio.github.com/jsSHA/ for more information Several functions taken from Paul Johnston */ 'use strict';(function(Y){function C(b,a,c){var g=0,d=[],n=0,h,l,e,f,m,q,u,r,I=!1,v=[],w=[],t,y=!1,A=!1,x=-1;c=c||{};h=c.encoding||"UTF8";t=c.numRounds||1;if(t!==parseInt(t,10)||1>t)throw Error("numRounds must a integer >= 1");if("SHA-1"===b)m=512,q=K,u=Z,f=160,r=function(a){return a.slice()};else if(0===b.lastIndexOf("SHA-",0))if(q=function(a,c){return L(a,c,b)},u=function(a,c,g,d){var k,f;if("SHA-224"===b||"SHA-256"===b)k=(c+65>>>9<<4)+15,f=16;else if("SHA-384"===b||"SHA-512"===b)k=(c+129>>>10<< 5)+31,f=32;else throw Error("Unexpected error in SHA-2 implementation");for(;a.length<=k;)a.push(0);a[c>>>5]|=128<<24-c%32;c=c+g;a[k]=c&4294967295;a[k-1]=c/4294967296|0;g=a.length;for(c=0;cd;d+=1)b[d]=a[d].slice();return b};x=1;if("SHA3-224"=== b)m=1152,f=224;else if("SHA3-256"===b)m=1088,f=256;else if("SHA3-384"===b)m=832,f=384;else if("SHA3-512"===b)m=576,f=512;else if("SHAKE128"===b)m=1344,f=-1,F=31,A=!0;else if("SHAKE256"===b)m=1088,f=-1,F=31,A=!0;else throw Error("Chosen SHA variant is not supported");u=function(a,b,d,c,g){d=m;var k=F,f,h=[],n=d>>>5,l=0,e=b>>>5;for(f=0;f=d;f+=n)c=D(a.slice(f,f+n),c),b-=d;a=a.slice(f);for(b%=d;a.length>>3;a[f>>2]^=k<=g)break;h.push(a.a);l+=1;0===64*l%d&&D(null,c)}return h}}else throw Error("Chosen SHA variant is not supported");e=M(a,h,x);l=B(b);this.setHMACKey=function(a,d,c){var k;if(!0===I)throw Error("HMAC key already set");if(!0===y)throw Error("Cannot set HMAC key after calling update");if(!0===A)throw Error("SHAKE is not supported for HMAC");h=(c||{}).encoding||"UTF8";d=M(d,h,x)(a);a=d.binLen;d=d.value;k=m>>>3;c=k/4-1;if(ka/8){for(;d.length<=c;)d.push(0);d[c]&=4294967040}for(a=0;a<=c;a+=1)v[a]=d[a]^909522486,w[a]=d[a]^1549556828;l=q(v,l);g=m;I=!0};this.update=function(a){var b,c,k,f=0,h=m>>>5;b=e(a,d,n);a=b.binLen;c=b.value;b=a>>>5;for(k=0;k>>5);n=a%m;y=!0};this.getHash=function(a,c){var k,h,e,m;if(!0===I)throw Error("Cannot call getHash after setting HMAC key");e=N(c);if(!0===A){if(-1===e.shakeLen)throw Error("shakeLen must be specified in options"); f=e.shakeLen}switch(a){case "HEX":k=function(a){return O(a,f,x,e)};break;case "B64":k=function(a){return P(a,f,x,e)};break;case "BYTES":k=function(a){return Q(a,f,x)};break;case "ARRAYBUFFER":try{h=new ArrayBuffer(0)}catch(p){throw Error("ARRAYBUFFER not supported by this environment");}k=function(a){return R(a,f,x)};break;default:throw Error("format must be HEX, B64, BYTES, or ARRAYBUFFER");}m=u(d.slice(),n,g,r(l),f);for(h=1;h>>24-f%32),m=u(m,f, 0,B(b),f);return k(m)};this.getHMAC=function(a,c){var k,h,e,p;if(!1===I)throw Error("Cannot call getHMAC without first setting HMAC key");e=N(c);switch(a){case "HEX":k=function(a){return O(a,f,x,e)};break;case "B64":k=function(a){return P(a,f,x,e)};break;case "BYTES":k=function(a){return Q(a,f,x)};break;case "ARRAYBUFFER":try{k=new ArrayBuffer(0)}catch(v){throw Error("ARRAYBUFFER not supported by this environment");}k=function(a){return R(a,f,x)};break;default:throw Error("outputFormat must be HEX, B64, BYTES, or ARRAYBUFFER"); }h=u(d.slice(),n,g,r(l),f);p=q(w,B(b));p=u(h,f,m,p,f);return k(p)}}function c(b,a){this.a=b;this.b=a}function O(b,a,c,g){var d="";a/=8;var n,h,e;e=-1===c?3:0;for(n=0;n>>2]>>>8*(e+n%4*c),d+="0123456789abcdef".charAt(h>>>4&15)+"0123456789abcdef".charAt(h&15);return g.outputUpper?d.toUpperCase():d}function P(b,a,c,g){var d="",n=a/8,h,e,p,f;f=-1===c?3:0;for(h=0;h>>2]:0,p=h+2>>2]:0,p=(b[h>>>2]>>>8*(f+h%4*c)&255)<<16|(e>>>8*(f+(h+1)%4*c)&255)<<8|p>>>8*(f+ (h+2)%4*c)&255,e=0;4>e;e+=1)8*h+6*e<=a?d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(p>>>6*(3-e)&63):d+=g.b64Pad;return d}function Q(b,a,c){var g="";a/=8;var d,e,h;h=-1===c?3:0;for(d=0;d>>2]>>>8*(h+d%4*c)&255,g+=String.fromCharCode(e);return g}function R(b,a,c){a/=8;var g,d=new ArrayBuffer(a),e;e=-1===c?3:0;for(g=0;g>>2]>>>8*(e+g%4*c)&255;return d}function N(b){var a={outputUpper:!1,b64Pad:"=",shakeLen:-1};b=b||{};a.outputUpper=b.outputUpper|| !1;!0===b.hasOwnProperty("b64Pad")&&(a.b64Pad=b.b64Pad);if(!0===b.hasOwnProperty("shakeLen")){if(0!==b.shakeLen%8)throw Error("shakeLen must be a multiple of 8");a.shakeLen=b.shakeLen}if("boolean"!==typeof a.outputUpper)throw Error("Invalid outputUpper formatting option");if("string"!==typeof a.b64Pad)throw Error("Invalid b64Pad formatting option");return a}function M(b,a,c){switch(a){case "UTF8":case "UTF16BE":case "UTF16LE":break;default:throw Error("encoding must be UTF8, UTF16BE, or UTF16LE"); }switch(b){case "HEX":b=function(a,b,e){var h=a.length,l,p,f,m,q,u;if(0!==h%2)throw Error("String of HEX type must be in byte increments");b=b||[0];e=e||0;q=e>>>3;u=-1===c?3:0;for(l=0;l>>1)+q;for(f=m>>>2;b.length<=f;)b.push(0);b[f]|=p<<8*(u+m%4*c)}return{value:b,binLen:4*h+e}};break;case "TEXT":b=function(b,d,e){var h,l,p=0,f,m,q,u,r,t;d=d||[0];e=e||0;q=e>>>3;if("UTF8"===a)for(t=-1=== c?3:0,f=0;fh?l.push(h):2048>h?(l.push(192|h>>>6),l.push(128|h&63)):55296>h||57344<=h?l.push(224|h>>>12,128|h>>>6&63,128|h&63):(f+=1,h=65536+((h&1023)<<10|b.charCodeAt(f)&1023),l.push(240|h>>>18,128|h>>>12&63,128|h>>>6&63,128|h&63)),m=0;m>>2;d.length<=u;)d.push(0);d[u]|=l[m]<<8*(t+r%4*c);p+=1}else if("UTF16BE"===a||"UTF16LE"===a)for(t=-1===c?2:0,f=0;f>>8);r= p+q;for(u=r>>>2;d.length<=u;)d.push(0);d[u]|=h<<8*(t+r%4*c);p+=2}return{value:d,binLen:8*p+e}};break;case "B64":b=function(a,b,e){var h=0,l,p,f,m,q,u,r,t;if(-1===a.search(/^[a-zA-Z0-9=+\/]+$/))throw Error("Invalid character in base-64 string");p=a.indexOf("=");a=a.replace(/\=/g,"");if(-1!==p&&p