Files
GDevelop/Extensions/Leaderboards/sha256.js

21 lines
11 KiB
JavaScript

/**
* A JavaScript implementation of the SHA family of hashes - defined in FIPS PUB 180-4, FIPS PUB 202,
* and SP 800-185 - as well as the corresponding HMAC implementation as defined in FIPS PUB 198-1.
*
* Copyright 2008-2021 Brian Turek, 1998-2009 Paul Johnston & Contributors
* Distributed under the BSD License
* See http://caligatio.github.com/jsSHA/ for more information
*
* Two ECMAScript polyfill functions carry the following license:
*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
* MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
*/
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(t="undefined"!=typeof globalThis?globalThis:t||self).jsSHA=r()}(this,function(){"use strict";var t=function(r,n){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])})(r,n)},r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n="ARRAYBUFFER not supported by this environment",e="UINT8ARRAY not supported by this environment";function o(t,r,n,e){var o,i,u,s=r||[0],h=(n=n||0)>>>3,a=-1===e?3:0;for(o=0;o<t.length;o+=1)i=(u=o+h)>>>2,s.length<=i&&s.push(0),s[i]|=t[o]<<8*(a+e*(u%4));return{value:s,binLen:8*t.length+n}}function i(t,i,u){switch(i){case"UTF8":case"UTF16BE":case"UTF16LE":break;default:throw new Error("encoding must be UTF8, UTF16BE, or UTF16LE")}switch(t){case"HEX":return function(t,r,n){return function(t,r,n,e){var o,i,u,s;if(0!=t.length%2)throw new Error("String of HEX type must be in byte increments");var h=r||[0],a=(n=n||0)>>>3,f=-1===e?3:0;for(o=0;o<t.length;o+=2){if(i=parseInt(t.substr(o,2),16),isNaN(i))throw new Error("String of HEX type contains invalid characters");for(u=(s=(o>>>1)+a)>>>2;h.length<=u;)h.push(0);h[u]|=i<<8*(f+e*(s%4))}return{value:h,binLen:4*t.length+n}}(t,r,n,u)};case"TEXT":return function(t,r,n){return function(t,r,n,e,o){var i,u,s,h,a,f,c,p,l=0,w=n||[0],d=(e=e||0)>>>3;if("UTF8"===r)for(c=-1===o?3:0,s=0;s<t.length;s+=1)for(u=[],128>(i=t.charCodeAt(s))?u.push(i):2048>i?(u.push(192|i>>>6),u.push(128|63&i)):55296>i||57344<=i?u.push(224|i>>>12,128|i>>>6&63,128|63&i):(s+=1,i=65536+((1023&i)<<10|1023&t.charCodeAt(s)),u.push(240|i>>>18,128|i>>>12&63,128|i>>>6&63,128|63&i)),h=0;h<u.length;h+=1){for(a=(f=l+d)>>>2;w.length<=a;)w.push(0);w[a]|=u[h]<<8*(c+o*(f%4)),l+=1}else for(c=-1===o?2:0,p="UTF16LE"===r&&1!==o||"UTF16LE"!==r&&1===o,s=0;s<t.length;s+=1){for(i=t.charCodeAt(s),!0===p&&(i=(h=255&i)<<8|i>>>8),a=(f=l+d)>>>2;w.length<=a;)w.push(0);w[a]|=i<<8*(c+o*(f%4)),l+=2}return{value:w,binLen:8*l+e}}(t,i,r,n,u)};case"B64":return function(t,n,e){return function(t,n,e,o){var i,u,s,h,a,f,c=0,p=n||[0],l=(e=e||0)>>>3,w=-1===o?3:0,d=t.indexOf("=");if(-1===t.search(/^[a-zA-Z0-9=+/]+$/))throw new Error("Invalid character in base-64 string");if(t=t.replace(/=/g,""),-1!==d&&d<t.length)throw new Error("Invalid '=' found in base-64 string");for(i=0;i<t.length;i+=4){for(h=t.substr(i,4),s=0,u=0;u<h.length;u+=1)s|=r.indexOf(h.charAt(u))<<18-6*u;for(u=0;u<h.length-1;u+=1){for(a=(f=c+l)>>>2;p.length<=a;)p.push(0);p[a]|=(s>>>16-8*u&255)<<8*(w+o*(f%4)),c+=1}}return{value:p,binLen:8*c+e}}(t,n,e,u)};case"BYTES":return function(t,r,n){return function(t,r,n,e){var o,i,u,s,h=r||[0],a=(n=n||0)>>>3,f=-1===e?3:0;for(i=0;i<t.length;i+=1)o=t.charCodeAt(i),u=(s=i+a)>>>2,h.length<=u&&h.push(0),h[u]|=o<<8*(f+e*(s%4));return{value:h,binLen:8*t.length+n}}(t,r,n,u)};case"ARRAYBUFFER":try{new ArrayBuffer(0)}catch(t){throw new Error(n)}return function(t,r,n){return function(t,r,n,e){return o(new Uint8Array(t),r,n,e)}(t,r,n,u)};case"UINT8ARRAY":try{new Uint8Array(0)}catch(t){throw new Error(e)}return function(t,r,n){return o(t,r,n,u)};default:throw new Error("format must be HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY")}}function u(t,o,i,u){switch(t){case"HEX":return function(t){return function(t,r,n,e){var o,i,u="0123456789abcdef",s="",h=r/8,a=-1===n?3:0;for(o=0;o<h;o+=1)i=t[o>>>2]>>>8*(a+n*(o%4)),s+=u.charAt(i>>>4&15)+u.charAt(15&i);return e.outputUpper?s.toUpperCase():s}(t,o,i,u)};case"B64":return function(t){return function(t,n,e,o){var i,u,s,h,a,f="",c=n/8,p=-1===e?3:0;for(i=0;i<c;i+=3)for(h=i+1<c?t[i+1>>>2]:0,a=i+2<c?t[i+2>>>2]:0,s=(t[i>>>2]>>>8*(p+e*(i%4))&255)<<16|(h>>>8*(p+e*((i+1)%4))&255)<<8|a>>>8*(p+e*((i+2)%4))&255,u=0;u<4;u+=1)f+=8*i+6*u<=n?r.charAt(s>>>6*(3-u)&63):o.b64Pad;return f}(t,o,i,u)};case"BYTES":return function(t){return function(t,r,n){var e,o,i="",u=r/8,s=-1===n?3:0;for(e=0;e<u;e+=1)o=t[e>>>2]>>>8*(s+n*(e%4))&255,i+=String.fromCharCode(o);return i}(t,o,i)};case"ARRAYBUFFER":try{new ArrayBuffer(0)}catch(t){throw new Error(n)}return function(t){return function(t,r,n){var e,o=r/8,i=new ArrayBuffer(o),u=new Uint8Array(i),s=-1===n?3:0;for(e=0;e<o;e+=1)u[e]=t[e>>>2]>>>8*(s+n*(e%4))&255;return i}(t,o,i)};case"UINT8ARRAY":try{new Uint8Array(0)}catch(t){throw new Error(e)}return function(t){return function(t,r,n){var e,o=r/8,i=-1===n?3:0,u=new Uint8Array(o);for(e=0;e<o;e+=1)u[e]=t[e>>>2]>>>8*(i+n*(e%4))&255;return u}(t,o,i)};default:throw new Error("format must be HEX, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY")}}var s=[1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298],h=[3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428],a=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225];function f(t){var r={outputUpper:!1,b64Pad:"=",outputLen:-1},n=t||{},e="Output length must be a multiple of 8";if(r.outputUpper=n.outputUpper||!1,n.b64Pad&&(r.b64Pad=n.b64Pad),n.outputLen){if(n.outputLen%8!=0)throw new Error(e);r.outputLen=n.outputLen}else if(n.shakeLen){if(n.shakeLen%8!=0)throw new Error(e);r.outputLen=n.shakeLen}if("boolean"!=typeof r.outputUpper)throw new Error("Invalid outputUpper formatting option");if("string"!=typeof r.b64Pad)throw new Error("Invalid b64Pad formatting option");return r}function c(t,r){return t>>>r|t<<32-r}function p(t,r){return t>>>r}function l(t,r,n){return t&r^~t&n}function w(t,r,n){return t&r^t&n^r&n}function d(t){return c(t,2)^c(t,13)^c(t,22)}function v(t,r){var n=(65535&t)+(65535&r);return(65535&(t>>>16)+(r>>>16)+(n>>>16))<<16|65535&n}function A(t,r,n,e){var o=(65535&t)+(65535&r)+(65535&n)+(65535&e);return(65535&(t>>>16)+(r>>>16)+(n>>>16)+(e>>>16)+(o>>>16))<<16|65535&o}function g(t,r,n,e,o){var i=(65535&t)+(65535&r)+(65535&n)+(65535&e)+(65535&o);return(65535&(t>>>16)+(r>>>16)+(n>>>16)+(e>>>16)+(o>>>16)+(i>>>16))<<16|65535&i}function E(t){return c(t,7)^c(t,18)^p(t,3)}function y(t){return c(t,6)^c(t,11)^c(t,25)}function m(t){return"SHA-224"==t?h.slice():a.slice()}function R(t,r){var n,e,o,i,u,h,a,f,m,R,U,b,T=[];for(n=r[0],e=r[1],o=r[2],i=r[3],u=r[4],h=r[5],a=r[6],f=r[7],U=0;U<64;U+=1)T[U]=U<16?t[U]:A(c(b=T[U-2],17)^c(b,19)^p(b,10),T[U-7],E(T[U-15]),T[U-16]),m=g(f,y(u),l(u,h,a),s[U],T[U]),R=v(d(n),w(n,e,o)),f=a,a=h,h=u,u=v(i,m),i=o,o=e,e=n,n=v(m,R);return r[0]=v(n,r[0]),r[1]=v(e,r[1]),r[2]=v(o,r[2]),r[3]=v(i,r[3]),r[4]=v(u,r[4]),r[5]=v(h,r[5]),r[6]=v(a,r[6]),r[7]=v(f,r[7]),r}return function(r){function n(t,n,e){var o=this;if("SHA-224"!==t&&"SHA-256"!==t)throw new Error("Chosen SHA variant is not supported");var u=e||{};return(o=r.call(this,t,n,e)||this).t=o.i,o.o=!0,o.u=-1,o.h=i(o.v,o.l,o.u),o.A=R,o.p=function(t){return t.slice()},o.U=m,o.T=function(r,n,e,o){return function(t,r,n,e,o){for(var i,u=15+(r+65>>>9<<4),s=r+n;t.length<=u;)t.push(0);for(t[r>>>5]|=128<<24-r%32,t[u]=4294967295&s,t[u-1]=s/4294967296|0,i=0;i<t.length;i+=16)e=R(t.slice(i,i+16),e);return"SHA-224"===o?[e[0],e[1],e[2],e[3],e[4],e[5],e[6]]:e}(r,n,e,o,t)},o.R=m(t),o.m=512,o.F="SHA-224"===t?224:256,o.g=!1,u.hmacKey&&o.B(function(t,r,n,e){var o="hmacKey must include a value and format";if(!r)throw new Error(o);if(void 0===r.value||!r.format)throw new Error(o);return i(r.format,r.encoding||"UTF8",n)(r.value)}(0,u.hmacKey,o.u)),o}return function(r,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function e(){this.constructor=r}t(r,n),r.prototype=null===n?Object.create(n):(e.prototype=n.prototype,new e)}(n,r),n}(function(){function t(t,r,n){var e=n||{};if(this.v=r,this.l=e.encoding||"UTF8",this.numRounds=e.numRounds||1,isNaN(this.numRounds)||this.numRounds!==parseInt(this.numRounds,10)||1>this.numRounds)throw new Error("numRounds must a integer >= 1");this.H=t,this.S=[],this.Y=0,this.C=!1,this.I=0,this.L=!1,this.N=[],this.X=[]}return t.prototype.update=function(t){var r,n=0,e=this.m>>>5,o=this.h(t,this.S,this.Y),i=o.binLen,u=o.value,s=i>>>5;for(r=0;r<s;r+=e)n+this.m<=i&&(this.R=this.A(u.slice(r,r+e),this.R),n+=this.m);this.I+=n,this.S=u.slice(n>>>5),this.Y=i%this.m,this.C=!0},t.prototype.getHash=function(t,r){var n,e,o=this.F,i=f(r);if(this.g){if(-1===i.outputLen)throw new Error("Output length must be specified in options");o=i.outputLen}var s=u(t,o,this.u,i);if(this.L&&this.t)return s(this.t(i));for(e=this.T(this.S.slice(),this.Y,this.I,this.p(this.R),o),n=1;n<this.numRounds;n+=1)this.g&&o%32!=0&&(e[e.length-1]&=16777215>>>24-o%32),e=this.T(e,o,0,this.U(this.H),o);return s(e)},t.prototype.setHMACKey=function(t,r,n){if(!this.o)throw new Error("Variant does not support HMAC");if(this.C)throw new Error("Cannot set MAC key after calling update");var e=i(r,(n||{}).encoding||"UTF8",this.u);this.B(e(t))},t.prototype.B=function(t){var r,n=this.m>>>3,e=n/4-1;if(1!==this.numRounds)throw new Error("Cannot set numRounds with MAC");if(this.L)throw new Error("MAC key already set");for(n<t.binLen/8&&(t.value=this.T(t.value,t.binLen,0,this.U(this.H),this.F));t.value.length<=e;)t.value.push(0);for(r=0;r<=e;r+=1)this.N[r]=909522486^t.value[r],this.X[r]=1549556828^t.value[r];this.R=this.A(this.N,this.R),this.I=this.m,this.L=!0},t.prototype.getHMAC=function(t,r){var n=f(r);return u(t,this.F,this.u,n)(this.i())},t.prototype.i=function(){var t;if(!this.L)throw new Error("Cannot call getHMAC without first setting MAC key");var r=this.T(this.S.slice(),this.Y,this.I,this.p(this.R),this.F);return t=this.A(this.X,this.U(this.H)),this.T(r,this.F,this.m,t,this.F)},t}())});