@tmsfe/tms-core 0.0.169 → 0.0.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/aes.js +84 -0
- package/src/encrypt.js +327 -0
- package/src/index.js +2 -1
- package/src/jsencrypt.min.js +2 -0
- package/src/md5.js +1 -1
- package/src/report/helper.ts +8 -1
- package/src/report/types.ts +4 -0
- package/src/request.js +175 -60
- package/src/runtime/login.ts +2 -0
- package/src/traceUtils.js +23 -0
- package/src/encrypt/encrypt-util.ts +0 -1
- package/src/encrypt/index.ts +0 -1
- package/src/encrypt/md5.js +0 -2
- package/src/encrypt/nacl-util.min.js +0 -2
- package/src/encrypt/nacl.min.js +0 -2
- package/src/encrypt/traceUtils.js +0 -7
package/package.json
CHANGED
package/src/aes.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CryptoJS v3.1.2
|
|
3
|
+
code.google.com/p/crypto-js
|
|
4
|
+
(c) 2009-2013 by Jeff Mott. All rights reserved.
|
|
5
|
+
code.google.com/p/crypto-js/wiki/License
|
|
6
|
+
*/
|
|
7
|
+
/* eslint-disable */
|
|
8
|
+
var CryptoJS=CryptoJS||function(u,p){var d={},l=d.lib={},s=function(){},t=l.Base={extend:function(a){s.prototype=this;var c=new s;a&&c.mixIn(a);c.hasOwnProperty("init")||(c.init=function(){c.$super.init.apply(this,arguments)});c.init.prototype=c;c.$super=this;return c},create:function(){var a=this.extend();a.init.apply(a,arguments);return a},init:function(){},mixIn:function(a){for(var c in a)a.hasOwnProperty(c)&&(this[c]=a[c]);a.hasOwnProperty("toString")&&(this.toString=a.toString)},clone:function(){return this.init.prototype.extend(this)}},
|
|
9
|
+
r=l.WordArray=t.extend({init:function(a,c){a=this.words=a||[];this.sigBytes=c!=p?c:4*a.length},toString:function(a){return(a||v).stringify(this)},concat:function(a){var c=this.words,e=a.words,j=this.sigBytes;a=a.sigBytes;this.clamp();if(j%4)for(var k=0;k<a;k++)c[j+k>>>2]|=(e[k>>>2]>>>24-8*(k%4)&255)<<24-8*((j+k)%4);else if(65535<e.length)for(k=0;k<a;k+=4)c[j+k>>>2]=e[k>>>2];else c.push.apply(c,e);this.sigBytes+=a;return this},clamp:function(){var a=this.words,c=this.sigBytes;a[c>>>2]&=4294967295<<
|
|
10
|
+
32-8*(c%4);a.length=u.ceil(c/4)},clone:function(){var a=t.clone.call(this);a.words=this.words.slice(0);return a},random:function(a){for(var c=[],e=0;e<a;e+=4)c.push(4294967296*u.random()|0);return new r.init(c,a)}}),w=d.enc={},v=w.Hex={stringify:function(a){var c=a.words;a=a.sigBytes;for(var e=[],j=0;j<a;j++){var k=c[j>>>2]>>>24-8*(j%4)&255;e.push((k>>>4).toString(16));e.push((k&15).toString(16))}return e.join("")},parse:function(a){for(var c=a.length,e=[],j=0;j<c;j+=2)e[j>>>3]|=parseInt(a.substr(j,
|
|
11
|
+
2),16)<<24-4*(j%8);return new r.init(e,c/2)}},b=w.Latin1={stringify:function(a){var c=a.words;a=a.sigBytes;for(var e=[],j=0;j<a;j++)e.push(String.fromCharCode(c[j>>>2]>>>24-8*(j%4)&255));return e.join("")},parse:function(a){for(var c=a.length,e=[],j=0;j<c;j++)e[j>>>2]|=(a.charCodeAt(j)&255)<<24-8*(j%4);return new r.init(e,c)}},x=w.Utf8={stringify:function(a){try{return decodeURIComponent(escape(b.stringify(a)))}catch(c){throw Error("Malformed UTF-8 data");}},parse:function(a){return b.parse(unescape(encodeURIComponent(a)))}},
|
|
12
|
+
q=l.BufferedBlockAlgorithm=t.extend({reset:function(){this._data=new r.init;this._nDataBytes=0},_append:function(a){"string"==typeof a&&(a=x.parse(a));this._data.concat(a);this._nDataBytes+=a.sigBytes},_process:function(a){var c=this._data,e=c.words,j=c.sigBytes,k=this.blockSize,b=j/(4*k),b=a?u.ceil(b):u.max((b|0)-this._minBufferSize,0);a=b*k;j=u.min(4*a,j);if(a){for(var q=0;q<a;q+=k)this._doProcessBlock(e,q);q=e.splice(0,a);c.sigBytes-=j}return new r.init(q,j)},clone:function(){var a=t.clone.call(this);
|
|
13
|
+
a._data=this._data.clone();return a},_minBufferSize:0});l.Hasher=q.extend({cfg:t.extend(),init:function(a){this.cfg=this.cfg.extend(a);this.reset()},reset:function(){q.reset.call(this);this._doReset()},update:function(a){this._append(a);this._process();return this},finalize:function(a){a&&this._append(a);return this._doFinalize()},blockSize:16,_createHelper:function(a){return function(b,e){return(new a.init(e)).finalize(b)}},_createHmacHelper:function(a){return function(b,e){return(new n.HMAC.init(a,
|
|
14
|
+
e)).finalize(b)}}});var n=d.algo={};return d}(Math);
|
|
15
|
+
(function(){var u=CryptoJS,p=u.lib.WordArray;u.enc.Base64={stringify:function(d){var l=d.words,p=d.sigBytes,t=this._map;d.clamp();d=[];for(var r=0;r<p;r+=3)for(var w=(l[r>>>2]>>>24-8*(r%4)&255)<<16|(l[r+1>>>2]>>>24-8*((r+1)%4)&255)<<8|l[r+2>>>2]>>>24-8*((r+2)%4)&255,v=0;4>v&&r+0.75*v<p;v++)d.push(t.charAt(w>>>6*(3-v)&63));if(l=t.charAt(64))for(;d.length%4;)d.push(l);return d.join("")},parse:function(d){var l=d.length,s=this._map,t=s.charAt(64);t&&(t=d.indexOf(t),-1!=t&&(l=t));for(var t=[],r=0,w=0;w<
|
|
16
|
+
l;w++)if(w%4){var v=s.indexOf(d.charAt(w-1))<<2*(w%4),b=s.indexOf(d.charAt(w))>>>6-2*(w%4);t[r>>>2]|=(v|b)<<24-8*(r%4);r++}return p.create(t,r)},_map:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="}})();
|
|
17
|
+
(function(u){function p(b,n,a,c,e,j,k){b=b+(n&a|~n&c)+e+k;return(b<<j|b>>>32-j)+n}function d(b,n,a,c,e,j,k){b=b+(n&c|a&~c)+e+k;return(b<<j|b>>>32-j)+n}function l(b,n,a,c,e,j,k){b=b+(n^a^c)+e+k;return(b<<j|b>>>32-j)+n}function s(b,n,a,c,e,j,k){b=b+(a^(n|~c))+e+k;return(b<<j|b>>>32-j)+n}for(var t=CryptoJS,r=t.lib,w=r.WordArray,v=r.Hasher,r=t.algo,b=[],x=0;64>x;x++)b[x]=4294967296*u.abs(u.sin(x+1))|0;r=r.MD5=v.extend({_doReset:function(){this._hash=new w.init([1732584193,4023233417,2562383102,271733878])},
|
|
18
|
+
_doProcessBlock:function(q,n){for(var a=0;16>a;a++){var c=n+a,e=q[c];q[c]=(e<<8|e>>>24)&16711935|(e<<24|e>>>8)&4278255360}var a=this._hash.words,c=q[n+0],e=q[n+1],j=q[n+2],k=q[n+3],z=q[n+4],r=q[n+5],t=q[n+6],w=q[n+7],v=q[n+8],A=q[n+9],B=q[n+10],C=q[n+11],u=q[n+12],D=q[n+13],E=q[n+14],x=q[n+15],f=a[0],m=a[1],g=a[2],h=a[3],f=p(f,m,g,h,c,7,b[0]),h=p(h,f,m,g,e,12,b[1]),g=p(g,h,f,m,j,17,b[2]),m=p(m,g,h,f,k,22,b[3]),f=p(f,m,g,h,z,7,b[4]),h=p(h,f,m,g,r,12,b[5]),g=p(g,h,f,m,t,17,b[6]),m=p(m,g,h,f,w,22,b[7]),
|
|
19
|
+
f=p(f,m,g,h,v,7,b[8]),h=p(h,f,m,g,A,12,b[9]),g=p(g,h,f,m,B,17,b[10]),m=p(m,g,h,f,C,22,b[11]),f=p(f,m,g,h,u,7,b[12]),h=p(h,f,m,g,D,12,b[13]),g=p(g,h,f,m,E,17,b[14]),m=p(m,g,h,f,x,22,b[15]),f=d(f,m,g,h,e,5,b[16]),h=d(h,f,m,g,t,9,b[17]),g=d(g,h,f,m,C,14,b[18]),m=d(m,g,h,f,c,20,b[19]),f=d(f,m,g,h,r,5,b[20]),h=d(h,f,m,g,B,9,b[21]),g=d(g,h,f,m,x,14,b[22]),m=d(m,g,h,f,z,20,b[23]),f=d(f,m,g,h,A,5,b[24]),h=d(h,f,m,g,E,9,b[25]),g=d(g,h,f,m,k,14,b[26]),m=d(m,g,h,f,v,20,b[27]),f=d(f,m,g,h,D,5,b[28]),h=d(h,f,
|
|
20
|
+
m,g,j,9,b[29]),g=d(g,h,f,m,w,14,b[30]),m=d(m,g,h,f,u,20,b[31]),f=l(f,m,g,h,r,4,b[32]),h=l(h,f,m,g,v,11,b[33]),g=l(g,h,f,m,C,16,b[34]),m=l(m,g,h,f,E,23,b[35]),f=l(f,m,g,h,e,4,b[36]),h=l(h,f,m,g,z,11,b[37]),g=l(g,h,f,m,w,16,b[38]),m=l(m,g,h,f,B,23,b[39]),f=l(f,m,g,h,D,4,b[40]),h=l(h,f,m,g,c,11,b[41]),g=l(g,h,f,m,k,16,b[42]),m=l(m,g,h,f,t,23,b[43]),f=l(f,m,g,h,A,4,b[44]),h=l(h,f,m,g,u,11,b[45]),g=l(g,h,f,m,x,16,b[46]),m=l(m,g,h,f,j,23,b[47]),f=s(f,m,g,h,c,6,b[48]),h=s(h,f,m,g,w,10,b[49]),g=s(g,h,f,m,
|
|
21
|
+
E,15,b[50]),m=s(m,g,h,f,r,21,b[51]),f=s(f,m,g,h,u,6,b[52]),h=s(h,f,m,g,k,10,b[53]),g=s(g,h,f,m,B,15,b[54]),m=s(m,g,h,f,e,21,b[55]),f=s(f,m,g,h,v,6,b[56]),h=s(h,f,m,g,x,10,b[57]),g=s(g,h,f,m,t,15,b[58]),m=s(m,g,h,f,D,21,b[59]),f=s(f,m,g,h,z,6,b[60]),h=s(h,f,m,g,C,10,b[61]),g=s(g,h,f,m,j,15,b[62]),m=s(m,g,h,f,A,21,b[63]);a[0]=a[0]+f|0;a[1]=a[1]+m|0;a[2]=a[2]+g|0;a[3]=a[3]+h|0},_doFinalize:function(){var b=this._data,n=b.words,a=8*this._nDataBytes,c=8*b.sigBytes;n[c>>>5]|=128<<24-c%32;var e=u.floor(a/
|
|
22
|
+
4294967296);n[(c+64>>>9<<4)+15]=(e<<8|e>>>24)&16711935|(e<<24|e>>>8)&4278255360;n[(c+64>>>9<<4)+14]=(a<<8|a>>>24)&16711935|(a<<24|a>>>8)&4278255360;b.sigBytes=4*(n.length+1);this._process();b=this._hash;n=b.words;for(a=0;4>a;a++)c=n[a],n[a]=(c<<8|c>>>24)&16711935|(c<<24|c>>>8)&4278255360;return b},clone:function(){var b=v.clone.call(this);b._hash=this._hash.clone();return b}});t.MD5=v._createHelper(r);t.HmacMD5=v._createHmacHelper(r)})(Math);
|
|
23
|
+
(function(){var u=CryptoJS,p=u.lib,d=p.Base,l=p.WordArray,p=u.algo,s=p.EvpKDF=d.extend({cfg:d.extend({keySize:4,hasher:p.MD5,iterations:1}),init:function(d){this.cfg=this.cfg.extend(d)},compute:function(d,r){for(var p=this.cfg,s=p.hasher.create(),b=l.create(),u=b.words,q=p.keySize,p=p.iterations;u.length<q;){n&&s.update(n);var n=s.update(d).finalize(r);s.reset();for(var a=1;a<p;a++)n=s.finalize(n),s.reset();b.concat(n)}b.sigBytes=4*q;return b}});u.EvpKDF=function(d,l,p){return s.create(p).compute(d,
|
|
24
|
+
l)}})();
|
|
25
|
+
CryptoJS.lib.Cipher||function(u){var p=CryptoJS,d=p.lib,l=d.Base,s=d.WordArray,t=d.BufferedBlockAlgorithm,r=p.enc.Base64,w=p.algo.EvpKDF,v=d.Cipher=t.extend({cfg:l.extend(),createEncryptor:function(e,a){return this.create(this._ENC_XFORM_MODE,e,a)},createDecryptor:function(e,a){return this.create(this._DEC_XFORM_MODE,e,a)},init:function(e,a,b){this.cfg=this.cfg.extend(b);this._xformMode=e;this._key=a;this.reset()},reset:function(){t.reset.call(this);this._doReset()},process:function(e){this._append(e);return this._process()},
|
|
26
|
+
finalize:function(e){e&&this._append(e);return this._doFinalize()},keySize:4,ivSize:4,_ENC_XFORM_MODE:1,_DEC_XFORM_MODE:2,_createHelper:function(e){return{encrypt:function(b,k,d){return("string"==typeof k?c:a).encrypt(e,b,k,d)},decrypt:function(b,k,d){return("string"==typeof k?c:a).decrypt(e,b,k,d)}}}});d.StreamCipher=v.extend({_doFinalize:function(){return this._process(!0)},blockSize:1});var b=p.mode={},x=function(e,a,b){var c=this._iv;c?this._iv=u:c=this._prevBlock;for(var d=0;d<b;d++)e[a+d]^=
|
|
27
|
+
c[d]},q=(d.BlockCipherMode=l.extend({createEncryptor:function(e,a){return this.Encryptor.create(e,a)},createDecryptor:function(e,a){return this.Decryptor.create(e,a)},init:function(e,a){this._cipher=e;this._iv=a}})).extend();q.Encryptor=q.extend({processBlock:function(e,a){var b=this._cipher,c=b.blockSize;x.call(this,e,a,c);b.encryptBlock(e,a);this._prevBlock=e.slice(a,a+c)}});q.Decryptor=q.extend({processBlock:function(e,a){var b=this._cipher,c=b.blockSize,d=e.slice(a,a+c);b.decryptBlock(e,a);x.call(this,
|
|
28
|
+
e,a,c);this._prevBlock=d}});b=b.CBC=q;q=(p.pad={}).Pkcs7={pad:function(a,b){for(var c=4*b,c=c-a.sigBytes%c,d=c<<24|c<<16|c<<8|c,l=[],n=0;n<c;n+=4)l.push(d);c=s.create(l,c);a.concat(c)},unpad:function(a){a.sigBytes-=a.words[a.sigBytes-1>>>2]&255}};d.BlockCipher=v.extend({cfg:v.cfg.extend({mode:b,padding:q}),reset:function(){v.reset.call(this);var a=this.cfg,b=a.iv,a=a.mode;if(this._xformMode==this._ENC_XFORM_MODE)var c=a.createEncryptor;else c=a.createDecryptor,this._minBufferSize=1;this._mode=c.call(a,
|
|
29
|
+
this,b&&b.words)},_doProcessBlock:function(a,b){this._mode.processBlock(a,b)},_doFinalize:function(){var a=this.cfg.padding;if(this._xformMode==this._ENC_XFORM_MODE){a.pad(this._data,this.blockSize);var b=this._process(!0)}else b=this._process(!0),a.unpad(b);return b},blockSize:4});var n=d.CipherParams=l.extend({init:function(a){this.mixIn(a)},toString:function(a){return(a||this.formatter).stringify(this)}}),b=(p.format={}).OpenSSL={stringify:function(a){var b=a.ciphertext;a=a.salt;return(a?s.create([1398893684,
|
|
30
|
+
1701076831]).concat(a).concat(b):b).toString(r)},parse:function(a){a=r.parse(a);var b=a.words;if(1398893684==b[0]&&1701076831==b[1]){var c=s.create(b.slice(2,4));b.splice(0,4);a.sigBytes-=16}return n.create({ciphertext:a,salt:c})}},a=d.SerializableCipher=l.extend({cfg:l.extend({format:b}),encrypt:function(a,b,c,d){d=this.cfg.extend(d);var l=a.createEncryptor(c,d);b=l.finalize(b);l=l.cfg;return n.create({ciphertext:b,key:c,iv:l.iv,algorithm:a,mode:l.mode,padding:l.padding,blockSize:a.blockSize,formatter:d.format})},
|
|
31
|
+
decrypt:function(a,b,c,d){d=this.cfg.extend(d);b=this._parse(b,d.format);return a.createDecryptor(c,d).finalize(b.ciphertext)},_parse:function(a,b){return"string"==typeof a?b.parse(a,this):a}}),p=(p.kdf={}).OpenSSL={execute:function(a,b,c,d){d||(d=s.random(8));a=w.create({keySize:b+c}).compute(a,d);c=s.create(a.words.slice(b),4*c);a.sigBytes=4*b;return n.create({key:a,iv:c,salt:d})}},c=d.PasswordBasedCipher=a.extend({cfg:a.cfg.extend({kdf:p}),encrypt:function(b,c,d,l){l=this.cfg.extend(l);d=l.kdf.execute(d,
|
|
32
|
+
b.keySize,b.ivSize);l.iv=d.iv;b=a.encrypt.call(this,b,c,d.key,l);b.mixIn(d);return b},decrypt:function(b,c,d,l){l=this.cfg.extend(l);c=this._parse(c,l.format);d=l.kdf.execute(d,b.keySize,b.ivSize,c.salt);l.iv=d.iv;return a.decrypt.call(this,b,c,d.key,l)}})}();
|
|
33
|
+
(function(){for(var u=CryptoJS,p=u.lib.BlockCipher,d=u.algo,l=[],s=[],t=[],r=[],w=[],v=[],b=[],x=[],q=[],n=[],a=[],c=0;256>c;c++)a[c]=128>c?c<<1:c<<1^283;for(var e=0,j=0,c=0;256>c;c++){var k=j^j<<1^j<<2^j<<3^j<<4,k=k>>>8^k&255^99;l[e]=k;s[k]=e;var z=a[e],F=a[z],G=a[F],y=257*a[k]^16843008*k;t[e]=y<<24|y>>>8;r[e]=y<<16|y>>>16;w[e]=y<<8|y>>>24;v[e]=y;y=16843009*G^65537*F^257*z^16843008*e;b[k]=y<<24|y>>>8;x[k]=y<<16|y>>>16;q[k]=y<<8|y>>>24;n[k]=y;e?(e=z^a[a[a[G^z]]],j^=a[a[j]]):e=j=1}var H=[0,1,2,4,8,
|
|
34
|
+
16,32,64,128,27,54],d=d.AES=p.extend({_doReset:function(){for(var a=this._key,c=a.words,d=a.sigBytes/4,a=4*((this._nRounds=d+6)+1),e=this._keySchedule=[],j=0;j<a;j++)if(j<d)e[j]=c[j];else{var k=e[j-1];j%d?6<d&&4==j%d&&(k=l[k>>>24]<<24|l[k>>>16&255]<<16|l[k>>>8&255]<<8|l[k&255]):(k=k<<8|k>>>24,k=l[k>>>24]<<24|l[k>>>16&255]<<16|l[k>>>8&255]<<8|l[k&255],k^=H[j/d|0]<<24);e[j]=e[j-d]^k}c=this._invKeySchedule=[];for(d=0;d<a;d++)j=a-d,k=d%4?e[j]:e[j-4],c[d]=4>d||4>=j?k:b[l[k>>>24]]^x[l[k>>>16&255]]^q[l[k>>>
|
|
35
|
+
8&255]]^n[l[k&255]]},encryptBlock:function(a,b){this._doCryptBlock(a,b,this._keySchedule,t,r,w,v,l)},decryptBlock:function(a,c){var d=a[c+1];a[c+1]=a[c+3];a[c+3]=d;this._doCryptBlock(a,c,this._invKeySchedule,b,x,q,n,s);d=a[c+1];a[c+1]=a[c+3];a[c+3]=d},_doCryptBlock:function(a,b,c,d,e,j,l,f){for(var m=this._nRounds,g=a[b]^c[0],h=a[b+1]^c[1],k=a[b+2]^c[2],n=a[b+3]^c[3],p=4,r=1;r<m;r++)var q=d[g>>>24]^e[h>>>16&255]^j[k>>>8&255]^l[n&255]^c[p++],s=d[h>>>24]^e[k>>>16&255]^j[n>>>8&255]^l[g&255]^c[p++],t=
|
|
36
|
+
d[k>>>24]^e[n>>>16&255]^j[g>>>8&255]^l[h&255]^c[p++],n=d[n>>>24]^e[g>>>16&255]^j[h>>>8&255]^l[k&255]^c[p++],g=q,h=s,k=t;q=(f[g>>>24]<<24|f[h>>>16&255]<<16|f[k>>>8&255]<<8|f[n&255])^c[p++];s=(f[h>>>24]<<24|f[k>>>16&255]<<16|f[n>>>8&255]<<8|f[g&255])^c[p++];t=(f[k>>>24]<<24|f[n>>>16&255]<<16|f[g>>>8&255]<<8|f[h&255])^c[p++];n=(f[n>>>24]<<24|f[g>>>16&255]<<16|f[h>>>8&255]<<8|f[k&255])^c[p++];a[b]=q;a[b+1]=s;a[b+2]=t;a[b+3]=n},keySize:8});u.AES=p._createHelper(d)})();
|
|
37
|
+
CryptoJS.mode.CBC = (function () {
|
|
38
|
+
var CBC = CryptoJS.lib.BlockCipherMode.extend();
|
|
39
|
+
|
|
40
|
+
CBC.Encryptor = CBC.extend({
|
|
41
|
+
processBlock: function (words, offset) {
|
|
42
|
+
var cipher = this._cipher;
|
|
43
|
+
var blockSize = cipher.blockSize;
|
|
44
|
+
|
|
45
|
+
xorBlock.call(this, words, offset, blockSize);
|
|
46
|
+
cipher.encryptBlock(words, offset);
|
|
47
|
+
|
|
48
|
+
this._prevBlock = words.slice(offset, offset + blockSize);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
CBC.Decryptor = CBC.extend({
|
|
53
|
+
processBlock: function (words, offset) {
|
|
54
|
+
var cipher = this._cipher;
|
|
55
|
+
var blockSize = cipher.blockSize;
|
|
56
|
+
|
|
57
|
+
var thisBlock = words.slice(offset, offset + blockSize);
|
|
58
|
+
cipher.decryptBlock(words, offset);
|
|
59
|
+
xorBlock.call(this, words, offset, blockSize);
|
|
60
|
+
|
|
61
|
+
this._prevBlock = thisBlock;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
function xorBlock(words, offset, blockSize) {
|
|
66
|
+
var block;
|
|
67
|
+
|
|
68
|
+
if (this._iv) {
|
|
69
|
+
block = this._iv;
|
|
70
|
+
this._iv = undefined;
|
|
71
|
+
} else {
|
|
72
|
+
block = this._prevBlock;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (var i = 0; i < blockSize; i++) {
|
|
76
|
+
words[offset + i] ^= block[i];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return CBC;
|
|
81
|
+
}());
|
|
82
|
+
module.exports = {
|
|
83
|
+
CryptoJS: CryptoJS
|
|
84
|
+
}
|
package/src/encrypt.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @desc: 请求加密
|
|
3
|
+
* jsencrypt用于非对称公钥加密
|
|
4
|
+
* aes目前只保留ecb模式,用于对称加密
|
|
5
|
+
* -------------------------------
|
|
6
|
+
* 1. 获取登录接口拿到的公钥
|
|
7
|
+
* 2. 随机生成一个私钥,保存在整个runtime中
|
|
8
|
+
* 3. 使用私钥加密请求参数,请求header(按需)
|
|
9
|
+
* 4. 使用公钥加密私钥
|
|
10
|
+
* 5. 使用私钥解密返回内容(按需)
|
|
11
|
+
*/
|
|
12
|
+
import JSEncrypt from './jsencrypt.min.js';
|
|
13
|
+
const funAes = require('./aes.js');
|
|
14
|
+
// 加密模式
|
|
15
|
+
const ENCRYPT_MODE_ENUM = {
|
|
16
|
+
ASYMMETRIC_ENCRYPT_1: '1', // 非对称加密
|
|
17
|
+
NO_ENCRYPT_0: '0', // 不加密
|
|
18
|
+
ENCRYPT_COMPRESS: '1', // 加密压缩
|
|
19
|
+
ENCRYPT_NOCOMPRESS_2: '2', // 加密不压缩
|
|
20
|
+
NOENCRYPT_NOCOMPRESS_0: '0', // 不加密不压缩
|
|
21
|
+
};
|
|
22
|
+
// -----------------------------------------------工具函数定义开始-----------------------------------------------------
|
|
23
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
// 生成随机的 AES 密钥
|
|
26
|
+
function _generateRandomKey() {
|
|
27
|
+
const keyLength = 16; // AES-128 使用 16 字节的密钥长度
|
|
28
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
29
|
+
let key = '';
|
|
30
|
+
|
|
31
|
+
for (let i = 0; i < keyLength; i++) {
|
|
32
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
|
33
|
+
key += characters.charAt(randomIndex);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 使用公钥加密 AES 密钥
|
|
40
|
+
function _encryptWithPublicKey(aesKey, eccPublicKey) {
|
|
41
|
+
const encryptor = new JSEncrypt();
|
|
42
|
+
encryptor.setPublicKey(eccPublicKey);
|
|
43
|
+
const encryptedAesKey = encryptor.encrypt(aesKey)
|
|
44
|
+
.toString(funAes.CryptoJS.enc.Base64)
|
|
45
|
+
.replace(/\+/g, '-')
|
|
46
|
+
.replace(/\//g, '_')
|
|
47
|
+
.replace(/=+$/, '');
|
|
48
|
+
|
|
49
|
+
return encryptedAesKey;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 当前运行时的公钥的管理
|
|
53
|
+
function _getEccPublicKey() {
|
|
54
|
+
return wx.$_publicKey && wx.$_publicKey.publicKey;
|
|
55
|
+
}
|
|
56
|
+
function _getEccPublicId() {
|
|
57
|
+
return wx.$_publicKey && wx.$_publicKey.id;
|
|
58
|
+
}
|
|
59
|
+
// 检查path是否符合下发的路由前缀
|
|
60
|
+
function _checkPathInEnablePrefix(path) {
|
|
61
|
+
if (!_getEccPublicKey()) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (wx.$_publicKey && wx.$_publicKey.path === '*') {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
const prefixArr = wx.$_publicKey && wx.$_publicKey.path ? wx.$_publicKey.path.split(',').map(item => item.trim()) : [];
|
|
68
|
+
for (let i = 0, len = prefixArr.length; i < len; i++) {
|
|
69
|
+
if (path.indexOf(prefixArr[i]) > -1) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 获取私钥
|
|
77
|
+
let privateKeyInfo = null;
|
|
78
|
+
const _getPrivateKeyInfo = (enforceUpdate) => {
|
|
79
|
+
if (!privateKeyInfo || enforceUpdate || privateKeyInfo.publicKeyId !== _getEccPublicId()) {
|
|
80
|
+
const aesKey = _generateRandomKey();
|
|
81
|
+
const encryptAesKey = _encryptWithPublicKey(aesKey, _getEccPublicKey());
|
|
82
|
+
privateKeyInfo = {
|
|
83
|
+
aesKey,
|
|
84
|
+
encryptAesKey,
|
|
85
|
+
publicKeyId: _getEccPublicId(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return privateKeyInfo;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// 使用 AES 密钥加密 header 和 body
|
|
92
|
+
function _aesEncrypt(word, aesKey) {
|
|
93
|
+
// 使用 _generateRandomKey 生成随机的 IV
|
|
94
|
+
let iv = _generateRandomKey();
|
|
95
|
+
const key = funAes.CryptoJS.enc.Utf8.parse(aesKey);
|
|
96
|
+
iv = funAes.CryptoJS.enc.Utf8.parse(iv);
|
|
97
|
+
const srcs = funAes.CryptoJS.enc.Utf8.parse(word);
|
|
98
|
+
const encrypted = funAes.CryptoJS.AES.encrypt(srcs, key, {
|
|
99
|
+
iv, mode: funAes.CryptoJS.mode.CBC, padding: funAes.CryptoJS.pad.Pkcs7,
|
|
100
|
+
});
|
|
101
|
+
const combined = iv.concat(encrypted.ciphertext);
|
|
102
|
+
return combined.toString(funAes.CryptoJS.enc.Base64)
|
|
103
|
+
.replace(/\+/g, '-')
|
|
104
|
+
.replace(/\//g, '_')
|
|
105
|
+
.replace(/=+$/, '');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 解密
|
|
109
|
+
const _aesDecrypt = (cipherText, aesKey) => {
|
|
110
|
+
function decodeBase64URL(input) {
|
|
111
|
+
// 添加适当的填充
|
|
112
|
+
const paddedInput = input + '='.repeat((4 - input.length % 4) % 4);
|
|
113
|
+
// 使用Base64URL解码
|
|
114
|
+
return funAes.CryptoJS.enc.Base64.parse(paddedInput.replace(/-/g, '+').replace(/_/g, '/'));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const key = funAes.CryptoJS.enc.Utf8.parse(aesKey);
|
|
118
|
+
// 解析Base64URL格式的密文
|
|
119
|
+
const cipherBytes = decodeBase64URL(cipherText);
|
|
120
|
+
// 分离IV和密文
|
|
121
|
+
const blockSize = 16; // AES的块大小是16字节
|
|
122
|
+
const iv = funAes.CryptoJS.lib.WordArray.create(cipherBytes.words.slice(0, blockSize / 4));
|
|
123
|
+
const encryptedBytes = funAes.CryptoJS.lib.WordArray.create(cipherBytes.words.slice(blockSize / 4));
|
|
124
|
+
|
|
125
|
+
// 使用CBC模式和PKCS7填充解密
|
|
126
|
+
const decrypted = funAes.CryptoJS.AES.decrypt({ ciphertext: encryptedBytes }, key, {
|
|
127
|
+
iv,
|
|
128
|
+
mode: funAes.CryptoJS.mode.CBC,
|
|
129
|
+
padding: funAes.CryptoJS.pad.Pkcs7,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// 去除PKCS7填充
|
|
133
|
+
const decryptedText = decrypted.toString(funAes.CryptoJS.enc.Utf8);
|
|
134
|
+
return decryptedText;
|
|
135
|
+
};
|
|
136
|
+
// -----------------------------------------------------------------------------------------------------------------
|
|
137
|
+
// -----------------------------------------------工具函数定义结束-----------------------------------------------------
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 加密请求内容
|
|
141
|
+
* @param {String} method 请求方式,同wx.request
|
|
142
|
+
* @param {string/object/ArrayBuffer} params 请求参数
|
|
143
|
+
* @param {Object} header 用户自定义请求header
|
|
144
|
+
* @param {String} encryptedResponseHeaderName 期望加密的返回响应头,多个Header用,分隔
|
|
145
|
+
* @returns {header: Object, data: String}
|
|
146
|
+
*/
|
|
147
|
+
const reqEncrypt = (method, params = {}, header = {}, encryptedResponseHeaderName = '') => {
|
|
148
|
+
let finalData = params;
|
|
149
|
+
const { aesKey, encryptAesKey, publicKeyId } = _getPrivateKeyInfo();
|
|
150
|
+
// 1. 处理请求参数
|
|
151
|
+
if (method.toUpperCase() === 'GET') {
|
|
152
|
+
const searchParams = Object.keys(params)
|
|
153
|
+
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
|
154
|
+
.join('&');
|
|
155
|
+
const encodedParams = _aesEncrypt(searchParams, aesKey);
|
|
156
|
+
finalData = { tmsec: encodedParams };
|
|
157
|
+
} else {
|
|
158
|
+
finalData = _aesEncrypt(JSON.stringify(params), aesKey);
|
|
159
|
+
}
|
|
160
|
+
// 2. 处理Headers
|
|
161
|
+
const encodedHeaders = _aesEncrypt(JSON.stringify(header), aesKey);
|
|
162
|
+
return {
|
|
163
|
+
header: {
|
|
164
|
+
'X-Encrypt-Mode': ENCRYPT_MODE_ENUM.ASYMMETRIC_ENCRYPT_1,
|
|
165
|
+
'X-Compress-Mode': 'none',
|
|
166
|
+
'X-Encrypted-Headers': encodedHeaders,
|
|
167
|
+
'X-Encrypt-Pub': publicKeyId,
|
|
168
|
+
'X-Encrypt-Key': encryptAesKey,
|
|
169
|
+
'X-Encrypt-Response': ENCRYPT_MODE_ENUM.ENCRYPT_NOCOMPRESS_2,
|
|
170
|
+
'X-Response-Header-Name': encryptedResponseHeaderName,
|
|
171
|
+
},
|
|
172
|
+
data: finalData,
|
|
173
|
+
aesKey,
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// 解密请求结果
|
|
178
|
+
const resDecrypt = (aesKey, header, data) => {
|
|
179
|
+
const formatHeader = {};
|
|
180
|
+
Object.keys(header).forEach((key) => {
|
|
181
|
+
formatHeader[key.toLocaleLowerCase()] = header[key];
|
|
182
|
+
});
|
|
183
|
+
const {
|
|
184
|
+
// 'x-encrypt-key': encryptKey, // 使用ECC公钥加密后的AES秘钥
|
|
185
|
+
'x-encrypt-response': encryptResponse, // 期望的响应加密模式1: 加密、压缩 2: 加密、不压缩
|
|
186
|
+
'x-response-headfer-name': encryptedResponseHeaderName, // 需要加密的响应Header: X-Header1,X-Header2
|
|
187
|
+
'x-encrypted-headers': encryptedHeaders, // 加密后的Header信息
|
|
188
|
+
} = formatHeader;
|
|
189
|
+
if (!encryptResponse || encryptResponse === ENCRYPT_MODE_ENUM.NOENCRYPT_NOCOMPRESS_0) {
|
|
190
|
+
// 不需要解密,直接返回
|
|
191
|
+
return {
|
|
192
|
+
success: true,
|
|
193
|
+
errMsg: 'success',
|
|
194
|
+
header,
|
|
195
|
+
data,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
let decryptedHeaders = {};
|
|
200
|
+
if (encryptedResponseHeaderName) {
|
|
201
|
+
// 解密响应Header
|
|
202
|
+
decryptedHeaders = JSON.parse(_aesDecrypt(encryptedHeaders, aesKey));
|
|
203
|
+
}
|
|
204
|
+
// 解密响应Body
|
|
205
|
+
const decryptedBody = _aesDecrypt(data, aesKey);
|
|
206
|
+
return {
|
|
207
|
+
success: true,
|
|
208
|
+
errMsg: 'success',
|
|
209
|
+
header: {
|
|
210
|
+
...header,
|
|
211
|
+
...decryptedHeaders,
|
|
212
|
+
},
|
|
213
|
+
data: JSON.parse(decryptedBody),
|
|
214
|
+
};
|
|
215
|
+
} catch (err) {
|
|
216
|
+
return {
|
|
217
|
+
success: false,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 更新秘钥信息:更新公钥,同时生成新的私钥
|
|
224
|
+
* @param {String: publicKey, Number: expireDate, String: id } param0
|
|
225
|
+
*/
|
|
226
|
+
const updateDecryptKey = (publicKeyInfo) => {
|
|
227
|
+
if (!publicKeyInfo) {
|
|
228
|
+
wx.$_publicKey = null;
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const { publicKey, expireDate, id, path } = publicKeyInfo;
|
|
232
|
+
// 1. 存储新的公钥
|
|
233
|
+
wx.$_publicKey = {
|
|
234
|
+
publicKey,
|
|
235
|
+
expireDate,
|
|
236
|
+
id,
|
|
237
|
+
path,
|
|
238
|
+
};
|
|
239
|
+
// 2. 生成新的私钥
|
|
240
|
+
_getPrivateKeyInfo(true);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
// 不参与加密的请求路径规则
|
|
244
|
+
const encryptPathRule = {
|
|
245
|
+
'tim.map.qq.com': ['^/user/login', '^/api/getClientConfigs', '^/basic/crypto/lastkey'],
|
|
246
|
+
'tim.sparta.html5.qq.com': [
|
|
247
|
+
'^/user/login',
|
|
248
|
+
'^/cnabroad', '^~/ReChargeCard/', '^/gasolinerecharge/v2/', '^/gasolinerecharge/rechargecard/',
|
|
249
|
+
'^/tde', '^/basic/crypto/lastkey',
|
|
250
|
+
],
|
|
251
|
+
};
|
|
252
|
+
function needsEncryption(url, params) {
|
|
253
|
+
// 判断是否属于加密白名单, 如果不符合,直接返回false, 如果符合,则走下面的逻辑
|
|
254
|
+
const enablePathPrefix = _checkPathInEnablePrefix(url);
|
|
255
|
+
if (!enablePathPrefix) {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
// 如果是日志上报接口,需要过滤性能日志,不需要加密
|
|
259
|
+
if (url.indexOf('basic/event/upload') > -1) {
|
|
260
|
+
if (params.batch?.length === 1 && params.batch[0][31] === 'tms-performance-log') {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
// 使用正则表达式解析URL
|
|
266
|
+
const urlPattern = /^(https?:\/\/)?([^/?#]+)([/?#].*)?$/;
|
|
267
|
+
const matches = url.match(urlPattern);
|
|
268
|
+
|
|
269
|
+
if (!matches) {
|
|
270
|
+
console.error('Invalid URL:', url);
|
|
271
|
+
return false; // 如果URL无效,默认返回false
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const domain = matches[2];
|
|
275
|
+
const path = matches[3] || '/';
|
|
276
|
+
|
|
277
|
+
if (encryptPathRule[domain]) {
|
|
278
|
+
const pathRules = encryptPathRule[domain];
|
|
279
|
+
for (const rule of pathRules) {
|
|
280
|
+
const regex = new RegExp(rule);
|
|
281
|
+
if (regex.test(path)) {
|
|
282
|
+
return false; // 匹配到规则,不需要加密
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return true; // 此域名下,默认需要加密
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return false; // 没有匹配到规则,不需要加密
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// 判断是否是加密相关的错误类型(因为rawresponse的配置,需要errcode和errmessage共同来确定加密错误类型)
|
|
292
|
+
const ErrCodeType = {
|
|
293
|
+
pubKeyIdInvalid: '11_Invalid public key!',
|
|
294
|
+
pubKeyExpired: '12_Expired public key!',
|
|
295
|
+
aesCipherInvalid: '13_Invalid AES cipher!',
|
|
296
|
+
decryptError: '14_Decrypt error!',
|
|
297
|
+
cryptoDisabled: '15_Crypto disabled!',
|
|
298
|
+
};
|
|
299
|
+
const reqErrType = {
|
|
300
|
+
pubKeyInvalid: 'pubKeyInvalid',
|
|
301
|
+
decryptError: 'decryptError',
|
|
302
|
+
cryptoDisabled: 'cryptoDisabled',
|
|
303
|
+
};
|
|
304
|
+
const getErrcodeType = (errCode, errMsg) => {
|
|
305
|
+
const errType = `${errCode}_${errMsg}`;
|
|
306
|
+
if (ErrCodeType.decryptError === errType) {
|
|
307
|
+
return reqErrType.decryptError;
|
|
308
|
+
}
|
|
309
|
+
if (ErrCodeType.cryptoDisabled === errType) {
|
|
310
|
+
return reqErrType.cryptoDisabled;
|
|
311
|
+
}
|
|
312
|
+
if ([ErrCodeType.pubKeyIdInvalid, ErrCodeType.pubKeyExpired, ErrCodeType.aesCipherInvalid].indexOf(errType) > -1) {
|
|
313
|
+
return reqErrType.pubKeyInvalid;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const encryptObj = {
|
|
318
|
+
reqEncrypt,
|
|
319
|
+
resDecrypt,
|
|
320
|
+
updateDecryptKey,
|
|
321
|
+
needsEncryption,
|
|
322
|
+
getErrcodeType,
|
|
323
|
+
reqErrType,
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
export default encryptObj;
|
package/src/index.js
CHANGED
|
@@ -110,6 +110,7 @@ const getLocationBaseClass = () => LocationBase;
|
|
|
110
110
|
const init = (options = {}) => {
|
|
111
111
|
const {
|
|
112
112
|
appVersion, wxAppId, secretKey = '', client, defaultHost, cloudEnvId, appEnv, appPagePaths, homePage,
|
|
113
|
+
requestEncryptOpen,
|
|
113
114
|
} = options;
|
|
114
115
|
const envInfo = {
|
|
115
116
|
wxAppId,
|
|
@@ -151,7 +152,7 @@ const init = (options = {}) => {
|
|
|
151
152
|
|
|
152
153
|
Request.defaultHost = defaultHost;
|
|
153
154
|
Request.defaultSecretKey = secretKey;
|
|
154
|
-
Request.
|
|
155
|
+
Request.requestEncryptOpen = requestEncryptOpen;
|
|
155
156
|
// 初始化云环境
|
|
156
157
|
wx.cloud.init({ env: cloudEnvId });
|
|
157
158
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r(t.JSEncrypt={})}(this,(function(t){"use strict";function r(t){return"0123456789abcdefghijklmnopqrstuvwxyz".charAt(t)}var i="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var e,o=function(t,r){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var i in r)r.hasOwnProperty(i)&&(t[i]=r[i])})(t,r)},n={decode:function(t){var r;if(void 0===e){var i="= \f\n\r\t \u2028\u2029";for(e=Object.create(null),r=0;r<64;++r)e["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(r)]=r;for(r=0;r<9;++r)e[i.charAt(r)]=-1}var o=[],n=0,s=0;for(r=0;r<t.length;++r){var h=t.charAt(r);if("="==h)break;if(-1!=(h=e[h])){if(void 0===h)throw new Error("ico"+r);n|=h,4<=++s?(o[o.length]=n>>16,o[o.length]=n>>8&255,o[o.length]=255&n,s=n=0):n<<=6}}switch(s){case 1:throw new Error("bee");case 2:o[o.length]=n>>10;break;case 3:o[o.length]=n>>16,o[o.length]=n>>8&255}return o},re:/-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/,unarmor:function(t){var r=n.re.exec(t);if(r)if(r[1])t=r[1];else{if(!r[2])throw new Error("ros");t=r[2]}return n.decode(t)}};var s=function(){function t(r,i){this.hexDigits="0123456789ABCDEF",r instanceof t?(this.enc=r.enc,this.pos=r.pos):(this.enc=r,this.pos=i)}return t.prototype.get=function(t){if(void 0===t&&(t=this.pos++),t>=this.enc.length)throw new Error("Requesting byte offset "+t+" on a stream of length "+this.enc.length);return"string"==typeof this.enc?this.enc.charCodeAt(t):this.enc[t]},t.prototype.hexByte=function(t){return this.hexDigits.charAt(t>>4&15)+this.hexDigits.charAt(15&t)},t.prototype.hexDump=function(t,r,i){for(var e="",o=t;o<r;++o)if(e+=this.hexByte(this.get(o)),!0!==i)switch(15&o){case 7:e+=" ";break;case 15:e+="\n";break;default:e+=" "}return e},t}(),h=function(){function t(t,r,i,e,o){if(!(e instanceof u))throw new Error("iv");this.stream=t,this.header=r,this.length=i,this.tag=e,this.sub=o}return t.prototype.posStart=function(){return this.stream.pos},t.prototype.posEnd=function(){return this.stream.pos+this.header+Math.abs(this.length)},t.prototype.toHexString=function(){return this.stream.hexDump(this.posStart(),this.posEnd(),!0)},t.decodeLength=function(t){var r=t.get(),i=127&r;if(i==r)return i;if(6<i)throw new Error("lns"+(t.pos-1));if(0===i)return null;for(var e=r=0;e<i;++e)r=256*r+t.get();return r},t.prototype.getHexStringValue=function(){var t=this.toHexString(),r=2*this.header,i=2*this.length;return t.substr(r,i)},t.decode=function(r){var i;i=r instanceof s?r:new s(r,0);var e=new s(i),o=new u(i),n=t.decodeLength(i),h=i.pos,f=h-e.pos,a=null,p=function(){var r=[];if(null!==n){for(var e=h+n;i.pos<e;)r[r.length]=t.decode(i);if(i.pos!=e)throw new Error("Content size is not correct for container starting at offset "+h)}else try{for(;;){var o=t.decode(i);if(o.tag.isEOC())break;r[r.length]=o}n=h-i.pos}catch(r){throw new Error("Exception while decoding undefined length content: "+r)}return r};if(o.tagConstructed)a=p();else if(o.isUniversal()&&(3==o.tagNumber||4==o.tagNumber))try{if(3==o.tagNumber&&0!=i.get())throw new Error("bsu");a=p();for(var c=0;c<a.length;++c)if(a[c].tag.isEOC())throw new Error("EOC")}catch(r){a=null}if(null===a){if(null===n)throw new Error("a"+h);i.pos=h+Math.abs(n)}return new t(e,f,n,o,a)},t}(),u=function(){function t(t){var r=t.get();if(this.tagClass=r>>6,this.tagConstructed=!!(32&r),this.tagNumber=31&r,31==this.tagNumber){for(var i=new y;r=t.get(),i.mulAdd(128,127&r),128&r;);this.tagNumber=i.simplify()}}return t.prototype.isUniversal=function(){return 0===this.tagClass},t.prototype.isEOC=function(){return 0===this.tagClass&&0===this.tagNumber},t}(),f=function(){function t(t,r,i){null!=t&&("number"==typeof t?this.fromNumber(t,r,i):null==r&&"string"!=typeof t?this.fromString(t,256):this.fromString(t,r))}return t.prototype.toString=function(t){if(this.s<0)return"-"+this.negate().toString(t);var i;if(16==t)i=4;else if(8==t)i=3;else if(2==t)i=1;else if(32==t)i=5;else{if(4!=t)return this.toRadix(t);i=2}var e,o=(1<<i)-1,n=!1,s="",h=this.t,u=this.DB-h*this.DB%i;if(0<h--)for(u<this.DB&&0<(e=this[h]>>u)&&(n=!0,s=r(e));0<=h;)u<i?(e=(this[h]&(1<<u)-1)<<i-u,e|=this[--h]>>(u+=this.DB-i)):(e=this[h]>>(u-=i)&o,u<=0&&(u+=this.DB,--h)),0<e&&(n=!0),n&&(s+=r(e));return n?s:"0"},t.prototype.abs=function(){return this.s<0?this.negate():this},t.prototype.compareTo=function(t){var r=this.s-t.s;if(0!=r)return r;var i=this.t;if(0!=(r=i-t.t))return this.s<0?-r:r;for(;0<=--i;)if(0!=(r=this[i]-t[i]))return r;return 0},t.prototype.bitLength=function(){return this.t<=0?0:this.DB*(this.t-1)+S(this[this.t-1]^this.s&this.DM)},t.prototype.modPowInt=function(t,r){var i;return i=t<256||r.isEven()?new a(r):new p(r),this.exp(t,i)},t.prototype.copyTo=function(t){for(var r=this.t-1;0<=r;--r)t[r]=this[r];t.t=this.t,t.s=this.s},t.prototype.fromInt=function(t){this.t=1,this.s=t<0?-1:0,0<t?this[0]=t:t<-1?this[0]=t+this.DV:this.t=0},t.prototype.fromString=function(r,i){var e;if(16==i)e=4;else if(8==i)e=3;else if(256==i)e=8;else if(2==i)e=1;else if(32==i)e=5;else{if(4!=i)return void this.fromRadix(r,i);e=2}this.t=0,this.s=0;for(var o=r.length,n=!1,s=0;0<=--o;){var h=8==e?255&+r[o]:b(r,o);h<0?"-"==r.charAt(o)&&(n=!0):(n=!1,0==s?this[this.t++]=h:s+e>this.DB?(this[this.t-1]|=(h&(1<<this.DB-s)-1)<<s,this[this.t++]=h>>this.DB-s):this[this.t-1]|=h<<s,(s+=e)>=this.DB&&(s-=this.DB))}8==e&&!!(128&+r[0])&&(this.s=-1,0<s&&(this[this.t-1]|=(1<<this.DB-s)-1<<s)),this.clamp(),n&&t.ZERO.subTo(this,this)},t.prototype.clamp=function(){for(var t=this.s&this.DM;0<this.t&&this[this.t-1]==t;)--this.t},t.prototype.dlShiftTo=function(t,r){var i;for(i=this.t-1;0<=i;--i)r[i+t]=this[i];for(i=t-1;0<=i;--i)r[i]=0;r.t=this.t+t,r.s=this.s},t.prototype.drShiftTo=function(t,r){for(var i=t;i<this.t;++i)r[i-t]=this[i];r.t=Math.max(this.t-t,0),r.s=this.s},t.prototype.lShiftTo=function(t,r){for(var i=t%this.DB,e=this.DB-i,o=(1<<e)-1,n=Math.floor(t/this.DB),s=this.s<<i&this.DM,h=this.t-1;0<=h;--h)r[h+n+1]=this[h]>>e|s,s=(this[h]&o)<<i;for(h=n-1;0<=h;--h)r[h]=0;r[n]=s,r.t=this.t+n+1,r.s=this.s,r.clamp()},t.prototype.rShiftTo=function(t,r){r.s=this.s;var i=Math.floor(t/this.DB);if(i>=this.t)r.t=0;else{var e=t%this.DB,o=this.DB-e,n=(1<<e)-1;r[0]=this[i]>>e;for(var s=i+1;s<this.t;++s)r[s-i-1]|=(this[s]&n)<<o,r[s-i]=this[s]>>e;0<e&&(r[this.t-i-1]|=(this.s&n)<<o),r.t=this.t-i,r.clamp()}},t.prototype.subTo=function(t,r){for(var i=0,e=0,o=Math.min(t.t,this.t);i<o;)e+=this[i]-t[i],r[i++]=e&this.DM,e>>=this.DB;if(t.t<this.t){for(e-=t.s;i<this.t;)e+=this[i],r[i++]=e&this.DM,e>>=this.DB;e+=this.s}else{for(e+=this.s;i<t.t;)e-=t[i],r[i++]=e&this.DM,e>>=this.DB;e-=t.s}r.s=e<0?-1:0,e<-1?r[i++]=this.DV+e:0<e&&(r[i++]=e),r.t=i,r.clamp()},t.prototype.multiplyTo=function(r,i){var e=this.abs(),o=r.abs(),n=e.t;for(i.t=n+o.t;0<=--n;)i[n]=0;for(n=0;n<o.t;++n)i[n+e.t]=e.am(0,o[n],i,n,0,e.t);i.s=0,i.clamp(),this.s!=r.s&&t.ZERO.subTo(i,i)},t.prototype.squareTo=function(t){for(var r=this.abs(),i=t.t=2*r.t;0<=--i;)t[i]=0;for(i=0;i<r.t-1;++i){var e=r.am(i,r[i],t,2*i,0,1);(t[i+r.t]+=r.am(i+1,2*r[i],t,2*i+1,e,r.t-i-1))>=r.DV&&(t[i+r.t]-=r.DV,t[i+r.t+1]=1)}0<t.t&&(t[t.t-1]+=r.am(i,r[i],t,2*i,0,1)),t.s=0,t.clamp()},t.prototype.divRemTo=function(r,i,e){var o=r.abs();if(!(o.t<=0)){var n=this.abs();if(n.t<o.t)return null!=i&&i.fromInt(0),void(null!=e&&this.copyTo(e));null==e&&(e=c());var s=c(),h=this.s,u=r.s,f=this.DB-S(o[o.t-1]);0<f?(o.lShiftTo(f,s),n.lShiftTo(f,e)):(o.copyTo(s),n.copyTo(e));var a=s.t,p=s[a-1];if(0!=p){var l=p*(1<<this.F1)+(1<a?s[a-2]>>this.F2:0),y=this.FV/l,m=(1<<this.F1)/l,g=1<<this.F2,v=e.t,d=v-a,b=null==i?c():i;for(s.dlShiftTo(d,b),0<=e.compareTo(b)&&(e[e.t++]=1,e.subTo(b,e)),t.ONE.dlShiftTo(a,b),b.subTo(s,s);s.t<a;)s[s.t++]=0;for(;0<=--d;){var T=e[--v]==p?this.DM:Math.floor(e[v]*y+(e[v-1]+g)*m);if((e[v]+=s.am(0,T,e,d,0,a))<T)for(s.dlShiftTo(d,b),e.subTo(b,e);e[v]<--T;)e.subTo(b,e)}null!=i&&(e.drShiftTo(a,i),h!=u&&t.ZERO.subTo(i,i)),e.t=a,e.clamp(),0<f&&e.rShiftTo(f,e),h<0&&t.ZERO.subTo(e,e)}}},t.prototype.invDigit=function(){if(this.t<1)return 0;var t=this[0];if(!(1&t))return 0;var r=3&t;return 0<(r=(r=(r=(r=r*(2-(15&t)*r)&15)*(2-(255&t)*r)&255)*(2-((65535&t)*r&65535))&65535)*(2-t*r%this.DV)%this.DV)?this.DV-r:-r},t.prototype.isEven=function(){return 0==(0<this.t?1&this[0]:this.s)},t.prototype.exp=function(r,i){if(4294967295<r||r<1)return t.ONE;var e=c(),o=c(),n=i.convert(this),s=S(r)-1;for(n.copyTo(e);0<=--s;)if(i.sqrTo(e,o),0<(r&1<<s))i.mulTo(o,n,e);else{var h=e;e=o,o=h}return i.revert(e)},t}(),a=(function(){function t(){}t.prototype.convert=function(t){return t},t.prototype.revert=function(t){return t},t.prototype.mulTo=function(t,r,i){t.multiplyTo(r,i)},t.prototype.sqrTo=function(t,r){t.squareTo(r)}}(),function(){function t(t){this.m=t}return t.prototype.convert=function(t){return t.s<0||0<=t.compareTo(this.m)?t.mod(this.m):t},t.prototype.revert=function(t){return t},t.prototype.reduce=function(t){t.divRemTo(this.m,null,t)},t.prototype.mulTo=function(t,r,i){t.multiplyTo(r,i),this.reduce(i)},t.prototype.sqrTo=function(t,r){t.squareTo(r),this.reduce(r)},t}()),p=function(){function t(t){this.m=t,this.mp=t.invDigit(),this.mpl=32767&this.mp,this.mph=this.mp>>15,this.um=(1<<t.DB-15)-1,this.mt2=2*t.t}return t.prototype.convert=function(t){var r=c();return t.abs().dlShiftTo(this.m.t,r),r.divRemTo(this.m,null,r),t.s<0&&0<r.compareTo(f.ZERO)&&this.m.subTo(r,r),r},t.prototype.revert=function(t){var r=c();return t.copyTo(r),this.reduce(r),r},t.prototype.reduce=function(t){for(;t.t<=this.mt2;)t[t.t++]=0;for(var r=0;r<this.m.t;++r){var i=32767&t[r],e=i*this.mpl+((i*this.mph+(t[r]>>15)*this.mpl&this.um)<<15)&t.DM;for(t[i=r+this.m.t]+=this.m.am(0,e,t,r,0,this.m.t);t[i]>=t.DV;)t[i]-=t.DV,t[++i]++}t.clamp(),t.drShiftTo(this.m.t,t),0<=t.compareTo(this.m)&&t.subTo(this.m,t)},t.prototype.mulTo=function(t,r,i){t.multiplyTo(r,i),this.reduce(i)},t.prototype.sqrTo=function(t,r){t.squareTo(r),this.reduce(r)},t}();!function(){function t(t){this.m=t,this.r2=c(),this.q3=c(),f.ONE.dlShiftTo(2*t.t,this.r2),this.mu=this.r2.divide(t)}t.prototype.convert=function(t){if(t.s<0||t.t>2*this.m.t)return t.mod(this.m);if(t.compareTo(this.m)<0)return t;var r=c();return t.copyTo(r),this.reduce(r),r},t.prototype.revert=function(t){return t},t.prototype.reduce=function(t){for(t.drShiftTo(this.m.t-1,this.r2),t.t>this.m.t+1&&(t.t=this.m.t+1,t.clamp()),this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3),this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);t.compareTo(this.r2)<0;)t.dAddOffset(1,this.m.t+1);for(t.subTo(this.r2,t);0<=t.compareTo(this.m);)t.subTo(this.m,t)},t.prototype.mulTo=function(t,r,i){t.multiplyTo(r,i),this.reduce(i)},t.prototype.sqrTo=function(t,r){t.squareTo(r),this.reduce(r)}}();function c(){return new f(null)}function l(t,r){return new f(t,r)}f.prototype.am=function(t,r,i,e,o,n){for(var s=16383&r,h=r>>14;0<=--n;){var u=16383&this[t],f=this[t++]>>14,a=h*u+f*s;o=((u=s*u+((16383&a)<<14)+i[e]+o)>>28)+(a>>14)+h*f,i[e++]=268435455&u}return o},f.prototype.DB=28,f.prototype.DM=268435455,f.prototype.DV=1<<28,f.prototype.FV=Math.pow(2,52),f.prototype.F1=24,f.prototype.F2=4;var m,g,v=[];for(m="0".charCodeAt(0),g=0;g<=9;++g)v[m++]=g;for(m="a".charCodeAt(0),g=10;g<36;++g)v[m++]=g;for(m="A".charCodeAt(0),g=10;g<36;++g)v[m++]=g;function b(t,r){var i=v[t.charCodeAt(r)];return null==i?-1:i}function T(t){var r=c();return r.fromInt(t),r}function S(t){var r,i=1;return 0!=(r=t>>>16)&&(t=r,i+=16),0!=(r=t>>8)&&(t=r,i+=8),0!=(r=t>>4)&&(t=r,i+=4),0!=(r=t>>2)&&(t=r,i+=2),0!=(r=t>>1)&&(t=r,i+=1),i}f.ZERO=T(0),f.ONE=T(1);var w,D,x=function(){function t(){this.i=0,this.j=0,this.S=[]}return t.prototype.init=function(t){var r,i,e;for(r=0;r<256;++r)this.S[r]=r;for(r=i=0;r<256;++r)i=i+this.S[r]+t[r%t.length]&255,e=this.S[r],this.S[r]=this.S[i],this.S[i]=e;this.i=0,this.j=0},t.prototype.next=function(){var t;return this.i=this.i+1&255,this.j=this.j+this.S[this.i]&255,t=this.S[this.i],this.S[this.i]=this.S[this.j],this.S[this.j]=t,this.S[t+this.S[this.i]&255]},t}(),E=256,A=null;if(null==A){A=[];D=0;!function(t){for(var r=0,i=t.length;r<i;r++)t[r]=Math.floor(256*Math.random())}(E=new Uint32Array(256))}function B(){if(null==w){for(w=new x;D<E;){var t=Math.floor(65536*Math.random());A[D++]=255&t}for(w.init(A),D=0;D<A.length;++D)A[D]=0;D=0}return w.next()}var _=function(){function t(){}return t.prototype.nextBytes=function(t){for(var r=0;r<t.length;++r)t[r]=B()},t}(),M=function(){function t(){this.n=null,this.e=0,this.d=null,this.p=null,this.q=null,this.dmp1=null,this.dmq1=null,this.coeff=null}return t.prototype.doPublic=function(t){return t.modPowInt(this.e,this.n)},t.prototype.setPublic=function(t,r){null!=t&&null!=r&&0<t.length&&0<r.length?(this.n=l(t,16),this.e=parseInt(r,16)):console.error("Invalid RSA public key")},t.prototype.encrypt=function(t){var r=function(t,r){if(r<t.length+11)return console.error("Message too long for RSA"),null;for(var i=[],e=t.length-1;0<=e&&0<r;){var o=t.charCodeAt(e--);o<128?i[--r]=o:127<o&&o<2048?(i[--r]=63&o|128,i[--r]=o>>6|192):(i[--r]=63&o|128,i[--r]=o>>6&63|128,i[--r]=o>>12|224)}i[--r]=0;for(var n=new _,s=[];2<r;){for(s[0]=0;0==s[0];)n.nextBytes(s);i[--r]=s[0]}return i[--r]=2,i[--r]=0,new f(i)}(t,this.n.bitLength()+7>>3);if(null==r)return null;var i=this.doPublic(r);if(null==i)return null;var e=i.toString(16);return 1&e.length?"0"+e:e},t}(),O=function(t){function r(i){var e=t.call(this)||this;return i&&("string"==typeof i?e.parseKey(i):r.hasPublicKeyProperty(i)&&e.parsePropertiesFrom(i)),e}return function(t,r){function i(){this.constructor=t}o(t,r),t.prototype=null===r?Object.create(r):(i.prototype=r.prototype,new i)}(r,t),r.prototype.parseKey=function(t){try{var r=0,i=0,e=/^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/.test(t)?d(t):n.unarmor(t),o=h.decode(e);if(3===o.sub.length&&(o=o.sub[2].sub[0]),9===o.sub.length){r=o.sub[1].getHexStringValue(),this.n=l(r,16),i=o.sub[2].getHexStringValue(),this.e=parseInt(i,16);var s=o.sub[3].getHexStringValue();this.d=l(s,16);var u=o.sub[4].getHexStringValue();this.p=l(u,16);var f=o.sub[5].getHexStringValue();this.q=l(f,16);var a=o.sub[6].getHexStringValue();this.dmp1=l(a,16);var p=o.sub[7].getHexStringValue();this.dmq1=l(p,16);var c=o.sub[8].getHexStringValue();this.coeff=l(c,16)}else{if(2!==o.sub.length)return!1;var y=o.sub[1].sub[0];r=y.sub[0].getHexStringValue(),this.n=l(r,16),i=y.sub[1].getHexStringValue(),this.e=parseInt(i,16)}return!0}catch(t){return!1}},r.wordwrap=function(t,r){if(!t)return t;var i="(.{1,"+(r=r||64)+"})( +|$\n?)|(.{1,"+r+"})";return t.match(RegExp(i,"g")).join("\n")},r.hasPublicKeyProperty=function(t){return(t=t||{}).hasOwnProperty("n")&&t.hasOwnProperty("e")},r}(M),V=function(){function t(t){t=t||{},this.default_key_size=parseInt(t.default_key_size,10)||1024,this.default_public_exponent=t.default_public_exponent||"010001",this.log=t.log||!1,this.key=null}return t.prototype.setKey=function(t){this.log&&this.key&&console.warn("a"),this.key=new O(t)},t.prototype.setPublicKey=function(t){this.setKey(t)},t.prototype.encrypt=function(t){try{return function(t){var r,e,o="";for(r=0;r+3<=t.length;r+=3)e=parseInt(t.substring(r,r+3),16),o+=i.charAt(e>>6)+i.charAt(63&e);for(r+1==t.length?(e=parseInt(t.substring(r,r+1),16),o+=i.charAt(e<<2)):r+2==t.length&&(e=parseInt(t.substring(r,r+2),16),o+=i.charAt(e>>2)+i.charAt((3&e)<<4));0<(3&o.length);)o+="=";return o}(this.getKey().encrypt(t))}catch(t){return!1}},t.prototype.getKey=function(t){if(!this.key){if(this.key=new O,t&&"[object Function]"==={}.toString.call(t))return void this.key.generateAsync(this.default_key_size,this.default_public_exponent,t);this.key.generate(this.default_key_size,this.default_public_exponent)}return this.key},t}();t.JSEncrypt=V,t.default=V,Object.defineProperty(t,"__esModule",{value:!0})}));
|
package/src/md5.js
CHANGED
|
@@ -356,7 +356,7 @@ function hexHMACMD5(k, d) {
|
|
|
356
356
|
*
|
|
357
357
|
* @param {string} string Input string
|
|
358
358
|
* @param {string} [key] HMAC key
|
|
359
|
-
* @param {
|
|
359
|
+
* @param {boolean} [raw] Raw output switch
|
|
360
360
|
* @returns {string} MD5 output
|
|
361
361
|
*/
|
|
362
362
|
function md5(string, key, raw) {
|
package/src/report/helper.ts
CHANGED
|
@@ -45,7 +45,14 @@ function getSystemInfo(): ISystemInfo {
|
|
|
45
45
|
const system = wx.getSystemInfoSync() as any;
|
|
46
46
|
// eslint-disable-next-line
|
|
47
47
|
const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host } = system;
|
|
48
|
-
systemInfo = {
|
|
48
|
+
systemInfo = {
|
|
49
|
+
model,
|
|
50
|
+
wxVersion,
|
|
51
|
+
platform,
|
|
52
|
+
SDKVersion,
|
|
53
|
+
host,
|
|
54
|
+
open_app_id: initOptions?.openAppId || '',
|
|
55
|
+
};
|
|
49
56
|
}
|
|
50
57
|
return systemInfo;
|
|
51
58
|
}
|
package/src/report/types.ts
CHANGED
|
@@ -4,6 +4,8 @@ export type DataItem = any;
|
|
|
4
4
|
* 初始化参数对象
|
|
5
5
|
*/
|
|
6
6
|
export interface IInitOptions {
|
|
7
|
+
// 外输应用key
|
|
8
|
+
openAppId?: string;
|
|
7
9
|
// 小程序项目名,如:sinan、mycar等
|
|
8
10
|
client: string,
|
|
9
11
|
// 小程序版本号
|
|
@@ -48,6 +50,8 @@ export interface ISystemInfo {
|
|
|
48
50
|
SDKVersion: string,
|
|
49
51
|
// 开发环境下为空
|
|
50
52
|
host: object,
|
|
53
|
+
// 外输应用key, 玉爽就要下划线格式不要驼峰
|
|
54
|
+
open_app_id: string,
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
export interface ILocation {
|
package/src/request.js
CHANGED
|
@@ -11,10 +11,12 @@
|
|
|
11
11
|
import md5 from './md5';
|
|
12
12
|
import { getEnvInfo, getAuthInfo } from './env';
|
|
13
13
|
import { safeJsonParse } from './objUtils';
|
|
14
|
-
import
|
|
14
|
+
import encryptObj from './encrypt';
|
|
15
15
|
import reporter from './report/index';
|
|
16
|
+
import { genTraceparent } from './traceUtils';
|
|
16
17
|
|
|
17
18
|
const logger = wx.getLogManager({});
|
|
19
|
+
let refreshPromise = null;
|
|
18
20
|
|
|
19
21
|
/**
|
|
20
22
|
* 用于序列化需要签名的参数
|
|
@@ -159,16 +161,7 @@ export default class Request {
|
|
|
159
161
|
*/
|
|
160
162
|
static defaultSecretKey = '';
|
|
161
163
|
|
|
162
|
-
static
|
|
163
|
-
// 加密模块初始化
|
|
164
|
-
encryptObjInit({
|
|
165
|
-
composeParamsFunc: async (data) => {
|
|
166
|
-
const params = await composeParam(data, true, {});
|
|
167
|
-
return sign(params, secretKey);
|
|
168
|
-
},
|
|
169
|
-
report: (...args) => reporter.reportPerformance(...args),
|
|
170
|
-
});
|
|
171
|
-
}
|
|
164
|
+
static requestEncryptOpen = true;
|
|
172
165
|
|
|
173
166
|
static getInstance() {
|
|
174
167
|
if (reqInstance === null) {
|
|
@@ -355,6 +348,94 @@ export default class Request {
|
|
|
355
348
|
return Promise.resolve({ url });
|
|
356
349
|
}
|
|
357
350
|
|
|
351
|
+
// wx请求封装成promise
|
|
352
|
+
async wxRequest(path, method, header = {}, data, needReport, seqId) {
|
|
353
|
+
if (needReport && path.indexOf('basic/event/upload') < 0) {
|
|
354
|
+
logger.log(
|
|
355
|
+
'tms-performance-log',
|
|
356
|
+
'request_encrypt_log', 'main', 'send_unencrypt_request', seqId,
|
|
357
|
+
!!wx.$_publicKey, Request.requestEncryptOpen, path,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
// 如果是获取pubKey的接口,但是没带userId, 则需要取消这次请求
|
|
361
|
+
if (path.indexOf('basic/crypto/lastkey') > -1 && !data.userId) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
return new Promise((resolve, reject) => {
|
|
365
|
+
wx.request({
|
|
366
|
+
url: path,
|
|
367
|
+
header: { ...header, 'X-Seq-Id': seqId, Traceparent: genTraceparent() },
|
|
368
|
+
method,
|
|
369
|
+
data,
|
|
370
|
+
enableHttp2: true,
|
|
371
|
+
success: (res) => {
|
|
372
|
+
resolve(res);
|
|
373
|
+
},
|
|
374
|
+
fail: (err) => {
|
|
375
|
+
reject(err);
|
|
376
|
+
},
|
|
377
|
+
});
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async refreshEncryptKey() {
|
|
382
|
+
if (wx.$_encrypt_refreshDisable) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
if (!refreshPromise) {
|
|
386
|
+
refreshPromise = new Promise(async (resolve) => {
|
|
387
|
+
try {
|
|
388
|
+
const requestParam = await composeParam({}, true, {});
|
|
389
|
+
const data = sign(requestParam, this.secretKey);
|
|
390
|
+
const finalUrl = this.makeUrl('basic/crypto/lastkey');
|
|
391
|
+
const res = await this.wxRequest(finalUrl, 'GET', {}, data, true, data.seqId);
|
|
392
|
+
const success = res.data.errCode === 0;
|
|
393
|
+
if (!success) {
|
|
394
|
+
// 如果请求失败,则停止刷新加密秘钥,运行时永久关闭
|
|
395
|
+
wx.$_encrypt_refreshDisable = true;
|
|
396
|
+
}
|
|
397
|
+
encryptObj.updateDecryptKey(res.data.resData);
|
|
398
|
+
resolve(success);
|
|
399
|
+
} catch (e) {
|
|
400
|
+
encryptObj.updateDecryptKey(null);
|
|
401
|
+
resolve(false);
|
|
402
|
+
}
|
|
403
|
+
refreshPromise = null;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
return refreshPromise;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// 如果全局请求加密开关关闭 || 当前请求内容不满足加密规则(publickey不为空且走sinan网关且非性能上报埋点)
|
|
410
|
+
checkIfNeedEncrypt(url, data) {
|
|
411
|
+
if (!Request.requestEncryptOpen || !encryptObj.needsEncryption(url, data)) {
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async dealEncryptionSwitch(resHeader, forceRefresh = false) {
|
|
418
|
+
if (!forceRefresh && (!resHeader || this.dealEncryptionSwitching)) {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
this.dealEncryptionSwitching = true;
|
|
422
|
+
const formatHeader = {};
|
|
423
|
+
Object.keys(resHeader).forEach(key => formatHeader[key.toLowerCase()] = resHeader[key]);
|
|
424
|
+
const encryptionAvaliable = formatHeader['x-encryption-available'] === '0';
|
|
425
|
+
if (encryptionAvaliable) {
|
|
426
|
+
// 关闭接下来请求的加密开关
|
|
427
|
+
wx.$_publicKey = null;
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
let refreshSuccess = true;
|
|
431
|
+
// 打开开关,如果当前是关闭状态,则需要刷新加密key
|
|
432
|
+
if (forceRefresh || !wx.$_publicKey) {
|
|
433
|
+
refreshSuccess = await this.refreshEncryptKey();
|
|
434
|
+
}
|
|
435
|
+
this.dealEncryptionSwitching = false;
|
|
436
|
+
return refreshSuccess;
|
|
437
|
+
}
|
|
438
|
+
|
|
358
439
|
/**
|
|
359
440
|
* 创建发送请求任务
|
|
360
441
|
* @memberof Request
|
|
@@ -364,61 +445,95 @@ export default class Request {
|
|
|
364
445
|
* @param {object} header 自定义的请求头
|
|
365
446
|
* @returns {Promise} 接口返回结果
|
|
366
447
|
*/
|
|
367
|
-
async createRequestTask(path, param = {}, method = 'POST', header = {}) {
|
|
448
|
+
async createRequestTask(path, param = {}, method = 'POST', header = {}, doEncrypt = true, retryTimes = 0) {
|
|
368
449
|
const requestParam = await composeParam(param, this.withAuth, this.baseParam);
|
|
369
450
|
const data = sign(requestParam, this.secretKey);
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
// 打车、代驾日志不截断,方便用于回放
|
|
379
|
-
const fullApi = path.indexOf('/takecar/') !== -1 || path.indexOf('dd/api/v2/order') !== -1;
|
|
451
|
+
const finalUrl = this.makeUrl(path);
|
|
452
|
+
const requestTime = Date.now();
|
|
453
|
+
const printLog = (isSuccess, res) => {
|
|
454
|
+
// 埋点已经单独打日志了,接口请求数据日志太长影响分析
|
|
455
|
+
if (path.indexOf('basic/event/upload') !== -1) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
380
458
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
result = `${result.substring(0, 500)} 内容太长被截断`;
|
|
384
|
-
}
|
|
459
|
+
// 打车、代驾日志不截断,方便用于回放
|
|
460
|
+
const fullApi = path.indexOf('/takecar/') !== -1 || path.indexOf('dd/api/v2/order') !== -1;
|
|
385
461
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
params: JSON.stringify(data),
|
|
391
|
-
duration: Date.now() - requestTime,
|
|
392
|
-
};
|
|
393
|
-
if (isSuccess) {
|
|
394
|
-
obj.res = result;
|
|
395
|
-
} else {
|
|
396
|
-
obj.err = result;
|
|
397
|
-
}
|
|
462
|
+
let result = JSON.stringify(res);
|
|
463
|
+
if (isSuccess && !fullApi && result.length > 500) {
|
|
464
|
+
result = `${result.substring(0, 500)} 内容太长被截断`;
|
|
465
|
+
}
|
|
398
466
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
467
|
+
const obj = {
|
|
468
|
+
path,
|
|
469
|
+
method,
|
|
470
|
+
header: JSON.stringify(header),
|
|
471
|
+
params: JSON.stringify(data),
|
|
472
|
+
duration: Date.now() - requestTime,
|
|
405
473
|
};
|
|
474
|
+
if (isSuccess) {
|
|
475
|
+
obj.res = result;
|
|
476
|
+
} else {
|
|
477
|
+
obj.err = result;
|
|
478
|
+
}
|
|
406
479
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
480
|
+
const str = JSON.stringify(obj, null, 2).replace(/\\"/ig, '\'');
|
|
481
|
+
if (isSuccess) {
|
|
482
|
+
logger.log(`接口请求成功:\n${str}`);
|
|
483
|
+
} else {
|
|
484
|
+
logger.warn(`接口请求失败:\n${str}`);
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
const { seqId } = data;
|
|
488
|
+
try {
|
|
489
|
+
// 当前请求不加密 || 不满足加密前提
|
|
490
|
+
if (!doEncrypt || !this.checkIfNeedEncrypt(finalUrl, data)) {
|
|
491
|
+
const result = await this.wxRequest(finalUrl, method, header, data, true, seqId);
|
|
492
|
+
const { header: resHeader, data: resData } = result;
|
|
493
|
+
// 根据返回的响应头来控制接下来运行时的加密开关
|
|
494
|
+
this.dealEncryptionSwitch(resHeader);
|
|
495
|
+
printLog(true, resData);
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
// 1. 加密请求
|
|
499
|
+
const {
|
|
500
|
+
header: encryptHeader, data: encryptData, aesKey,
|
|
501
|
+
} = encryptObj.reqEncrypt(method, data, header, '');
|
|
502
|
+
// 2. 发送请求
|
|
503
|
+
encryptHeader['content-type'] = 'text/plain';
|
|
504
|
+
const result = await this.wxRequest(finalUrl, method, encryptHeader, encryptData, false, seqId);
|
|
505
|
+
const { header: resHeader, data: resData } = result;
|
|
506
|
+
// 3. 解密响应
|
|
507
|
+
const { success, header: decryptHeader, data: decryptData } = encryptObj.resDecrypt(aesKey, resHeader, resData);
|
|
508
|
+
if (!success) {
|
|
509
|
+
reporter.reportPerformance('request_encrypt_log', 'main', 'local_response_decrypt_fail', seqId);
|
|
510
|
+
return this.createRequestTask(path, param, method, header, false);
|
|
511
|
+
}
|
|
512
|
+
// 4. 处理解密失败的响应
|
|
513
|
+
const errCodeType = encryptObj.getErrcodeType(decryptData.errCode, decryptData.errMsg);
|
|
514
|
+
if (errCodeType === encryptObj.reqErrType.pubKeyInvalid) { // 秘钥失效
|
|
515
|
+
const encryptSwitch = await this.dealEncryptionSwitch(resHeader, true);
|
|
516
|
+
if (retryTimes > 2) {
|
|
517
|
+
throw new Error('解密失败,请重试');
|
|
518
|
+
}
|
|
519
|
+
return this.createRequestTask(path, param, method, header, encryptSwitch, retryTimes + 1);
|
|
520
|
+
}
|
|
521
|
+
if (errCodeType === encryptObj.reqErrType.decryptError) { // 解密失败
|
|
522
|
+
return this.createRequestTask(path, param, method, header, false);
|
|
523
|
+
}
|
|
524
|
+
if (errCodeType === encryptObj.reqErrType.cryptoDisabled) { // 加密关闭
|
|
525
|
+
wx.$_publicKey = null;
|
|
526
|
+
return this.createRequestTask(path, param, method, header, false);
|
|
527
|
+
}
|
|
528
|
+
result.header = decryptHeader;
|
|
529
|
+
result.data = decryptData;
|
|
530
|
+
printLog(true, result.data);
|
|
531
|
+
// 根据返回的响应头来控制接下来运行时的加密开关
|
|
532
|
+
this.dealEncryptionSwitch(resHeader);
|
|
533
|
+
return result;
|
|
534
|
+
} catch (err) {
|
|
535
|
+
printLog(false, err);
|
|
536
|
+
throw err;
|
|
537
|
+
}
|
|
423
538
|
}
|
|
424
539
|
}
|
package/src/runtime/login.ts
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 生成traceparent Id
|
|
3
|
+
* 用途:用于生成version - trace-id - parent-id/span-id - trace-flags规则的id,方便链路追踪
|
|
4
|
+
*/
|
|
5
|
+
import { CryptoJS } from './aes';
|
|
6
|
+
|
|
7
|
+
// 生成随机的16字节trace-id和8字节parent-id/span-id
|
|
8
|
+
function generateRandomHex(size) {
|
|
9
|
+
return CryptoJS.lib.WordArray.random(size).toString(CryptoJS.enc.Hex);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 生成traceparent ID
|
|
13
|
+
function genTraceparent() {
|
|
14
|
+
const version = '00';
|
|
15
|
+
const traceId = generateRandomHex(16); // 16字节的trace-id
|
|
16
|
+
const parentId = generateRandomHex(8); // 8字节的parent-id/span-id
|
|
17
|
+
const traceFlags = '01'; // 示例标志,表示采样
|
|
18
|
+
return `${version}-${traceId}-${parentId}-${traceFlags}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
genTraceparent,
|
|
23
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import md5 from'./md5';const ecc=require('./nacl.min.js');const base64Util=require('./nacl-util.min.js');const logger=wx.getLogManager({});interface BaseResp<T>{success:boolean,msg:string,res:T,}interface CryptoKeyInfo{sharedByte:Uint8Array,clientPublicKey:string,serverPubId:string,}const SERVER_HOST_MAP={production:'https://tim.map.qq.com',test:'https://tim.sparta.html5.qq.com',development:'https://tim.sparta.html5.qq.com/dev',predist:'https://tim.sparta.html5.qq.com/pre',mock:'http://localhost:8003',};const baseUtil={_isObject:(obj:any):boolean=>Object.prototype.toString.call(obj)==='[object Object]',_formatLog(args:any[]):any[]{const time=new Date().toISOString().replace('T',' ').substring(0,19).replace(/-/g,'-').replace(/:/g,':');args.unshift(time);return args},logInfo:(...args)=>{args.unshift('request_encrypt_log');const items=baseUtil._formatLog(args);logger.log(...items)},encUrl:(input:Uint8Array):string=>{let base64=base64Util.encode(input);base64=base64 .replace(/\+/g,'-').replace(/\//g,'_').replace(/=+$/,'');return base64},decUrl:(input:string):Uint8Array=>{let base64=input.replace(/-/g,'+').replace(/_/g,'/');while(base64.length%4){base64+='='}return base64Util.decode(base64)},formatHeader:(header):any=>{if(!header||!baseUtil._isObject(header)){return{}}const formatHeader={};Object.keys(header).forEach((key)=>{formatHeader[key.toLocaleLowerCase()]=header[key]});return formatHeader},formatGetData:(params:any):string=>{if(!params||!baseUtil._isObject(params)){return''}return Object.keys(params).map(key=>`${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&')},getSinanHost:():string=>{if(wx.$_encryptEnvInfo){return SERVER_HOST_MAP[wx.$_encryptEnvInfo.envName]}return''},getClientEncryptOpen:():boolean=>wx.$_encryptEnvInfo?.requestEncryptOpen,BaseRespFac:class BaseRespFac<T>implements BaseResp<T>{success:boolean;msg:string;res:T;constructor(res:T,success?:boolean,msg?:string){this.success=success===undefined?true:success;this.msg=msg===undefined?'':msg;this.res=res}},};let composeParamsFunc=null;const eccUtil={_refreshPromise:null,_refreshPubKeyInfo:async(forceRefresh:boolean):Promise<boolean>=>{if(!composeParamsFunc){return false}if(!wx.$_publicKey||forceRefresh){eccUtil._refreshPromise=new Promise(async(resolve)=>{const url=`${baseUtil.getSinanHost()}/basic/crypto/lastkey2`;const data=await composeParamsFunc({});wx.request({url,method:'POST',data,enableHttp2:true,success:()=>{resolve(true)},fail:()=>{resolve(false)},})})}return eccUtil._refreshPromise},_updateGlobalPublicKeyInfo:(clear:boolean,resHeader?:{[key:string]:any}):BaseResp<boolean>=>{if(clear){wx.$_publicKey=null;return}const header=baseUtil.formatHeader(resHeader);const{'x-gateway-code':code,'x-crypto-pub-id':pubId,'x-crypto-pub-key':pubKey,'x-crypto-pub-exp':pubExp,'x-crypto-path':pathRule,}=header;const success=!code&&pubId;wx.$_publicKey=success?{pubId,pubKey,pubExp,pathRule}:null;return new baseUtil.BaseRespFac(success)},_privateKeyInfo:null,getPrivateKeyInfo:(forceUpdate=false):CryptoKeyInfo=>{if(!wx.$_publicKey)return null;const serverPubInfo=wx.$_publicKey;if(forceUpdate||!eccUtil._privateKeyInfo||eccUtil._privateKeyInfo.serverPubId!==serverPubInfo.pubId){const keyPair=ecc.box.keyPair();eccUtil._privateKeyInfo={serverPubId:serverPubInfo.pubId,sharedByte:ecc.box.before(baseUtil.decUrl(serverPubInfo.pubKey),keyPair.secretKey),clientPublicKey:baseUtil.encUrl(keyPair.publicKey),}}return eccUtil._privateKeyInfo},resolveGwCode:async(codeStr:string):Promise<{retry:boolean,success:boolean}>=>{if(!codeStr)return{retry:false,success:true};const code=parseInt(codeStr,10);switch(code){case 0:return{retry:false,success:true};case 11305:case 11306:{eccUtil._refreshPubKeyInfo(true).then((genPubSuccess)=>{genPubSuccess&&eccUtil.getPrivateKeyInfo()});return{retry:true,success:false}}case 11308:{eccUtil.getPrivateKeyInfo(true);return{retry:true,success:false}}case 11300:case 11301:case 11302:case 11303:case 11304:case 11307:return{retry:true,success:false};case 11309:return{retry:false,success:false};default:return{retry:false,success:true}}},_getSignSharedByte:()=>baseUtil.decUrl('mEufQpM1n5J8-OZZoJE7ucYMC2suTjfsHUq_6z5cyh8'),getClientCryptoSign:(data={},header={},sharedByte):string=>{const obj=baseUtil.formatHeader(Object.assign({},data,header));const str=Object.keys(obj).filter(item=>obj[item]).sort().reduce((pre,cur)=>{pre.push(`${cur}=${obj[cur]}`);return pre},[]).join('&');baseUtil.logInfo('---客户端签名---:before',str);const md5Str=md5(str);const nonce=ecc.randomBytes(ecc.box.nonceLength);const encrypted=ecc.box.after(md5Str,nonce,sharedByte);const combined=new Uint8Array(nonce.length+encrypted.length);combined.set(nonce);combined.set(encrypted,nonce.length);return baseUtil.encUrl(combined)},verifyServerCryptoSign:(traceId:string,resHeader={}):boolean=>{try{const formatHeader=baseUtil.formatHeader(resHeader);const signStr=formatHeader['x-crypto-sign'];if(!signStr){return false}const obj={'x-encrypt-key':formatHeader['x-encrypt-key'],'x-encrypt-response':formatHeader['x-encrypt-response'],'x-response-header-name':formatHeader['x-response-header-name'],'x-encrypted-headers':formatHeader['x-encrypted-headers'],'x-crypto-enable':formatHeader['x-crypto-enable'],'x-gateway-code':formatHeader['x-gateway-code'],'x-crypto-pub-id':formatHeader['x-crypto-pub-id'],'x-crypto-pub-key':formatHeader['x-crypto-pub-key'],'x-crypto-pub-exp':formatHeader['x-crypto-pub-exp'],'x-crypto-path':formatHeader['x-crypto-path'],'x-trace-id':traceId,};const msg=baseUtil.decUrl(signStr);const decrypted=ecc.sign.open(msg,eccUtil._getSignSharedByte());const str=Object.keys(obj).filter(item=>obj[item]).sort().reduce((pre,cur)=>{pre.push(`${cur}=${obj[cur]}`);return pre},[]).join('&');baseUtil.logInfo('---验证服务端的客户端签名---:before',str,traceId);const preHashArr=md5(str);const verified=preHashArr.length===decrypted.length&&preHashArr.every((v,i)=>v===decrypted[i]);return verified}catch(e){console.error('verifyServerCryptoSign error',e);return false}},execEncrypt:(input:string,ignoreNull=false):BaseResp<{cryptoKeyInfo:CryptoKeyInfo,encryptedContent:any}|null>=>{try{const eccKeyInfo=eccUtil.getPrivateKeyInfo();if(!eccKeyInfo){return new baseUtil.BaseRespFac(null,false,'加密失败:无加密秘钥信息')}const cryptoKeyInfo=Object.assign({},eccKeyInfo);if(ignoreNull&&(!input||input==='{}')){return new baseUtil.BaseRespFac({cryptoKeyInfo,encryptedContent:''})}const{sharedByte}=cryptoKeyInfo;const nonce=ecc.randomBytes(ecc.box.nonceLength);const encrypted=ecc.box.after(base64Util.decodeUTF8(input),nonce,sharedByte);const combined=new Uint8Array(nonce.length+encrypted.length);combined.set(nonce);combined.set(encrypted,nonce.length);const encryptedStr=baseUtil.encUrl(combined);return new baseUtil.BaseRespFac({cryptoKeyInfo,encryptedContent:encryptedStr,})}catch(e){return new baseUtil.BaseRespFac(null,false,`execEncrypt失败:${JSON.stringify(e)}`)}},execDecrypt:(msg:Uint8Array,cryptoKeyInfo:CryptoKeyInfo):BaseResp<string>=>{try{const{sharedByte}=cryptoKeyInfo;const nonce=msg.slice(0,ecc.box.nonceLength);const encrypted=msg.slice(ecc.box.nonceLength);const decrypted=ecc.box.open.after(encrypted,nonce,sharedByte);const decryptedStr=base64Util.encodeUTF8(decrypted);return new baseUtil.BaseRespFac(decryptedStr)}catch(err){return new baseUtil.BaseRespFac('',false,`execDecrypt失败:${JSON.stringify(err)}`)}},checkCryptoOpen:():boolean=>!!eccUtil._privateKeyInfo,closeCrypto:()=>{eccUtil._privateKeyInfo=null;eccUtil._updateGlobalPublicKeyInfo(true)},openCrypto:async():Promise<boolean>=>{const genPubSuccess=await eccUtil._refreshPubKeyInfo(false);if(!genPubSuccess)return false;eccUtil.getPrivateKeyInfo();return true},};const cryptRuleUtil={isServerOpen:():boolean=>!!wx.$_publicKey,pathInEnablePrefix:(path:string):boolean=>{if(!wx.$_publicKey){return false}const{pathRule}=wx.$_publicKey;if(pathRule==='*'){return true}const prefixArr=pathRule.split(',').map(item=>item.trim());for(let i=0,len=prefixArr.length;i<len;i++){if(path.indexOf(prefixArr[i])>-1){return true}}return false},isPerformanceReport:(path:string,params:any):boolean=>{if(path.indexOf('basic/event/upload')>-1){if(params.batch?.length===1&¶ms.batch[0]?.[31]==='tms-performance-log'){return true}return false}return false},_encryptPathRule:{'tim.map.qq.com':['^/user/login','^/api/getClientConfigs','^/basic/crypto/lastkey2'],'tim.sparta.html5.qq.com':['^/user/login','^/cnabroad','^~/ReChargeCard/','^/gasolinerecharge/v2/','^/gasolinerecharge/rechargecard/','^/tde','^/basic/crypto/lastkey2',],},isHostValid:(url)=>{const urlPattern=/^(https?:\/\/)?([^/?#]+)([/?#].*)?$/;const matches=url.match(urlPattern);if(!matches){console.error('Invalid URL:',url);return false}const domain=matches[2];const path=matches[3]||'/';if(cryptRuleUtil._encryptPathRule[domain]){const pathRules=cryptRuleUtil._encryptPathRule[domain];for(const rule of pathRules){const regex=new RegExp(rule);if(regex.test(path)){return false}}return true}return false},};const init=(composeFunc:Function):BaseResp<null>=>{composeParamsFunc=(...args)=>composeFunc(...args);return new baseUtil.BaseRespFac(null)};const isCryptoRuleMath=(path:string,reqData:any):BaseResp<boolean>=>{if(!wx.$_encryptEnvInfo.requestEncryptOpen){return new baseUtil.BaseRespFac(false,false,'本地加密未开启')}if(!cryptRuleUtil.isServerOpen()){return new baseUtil.BaseRespFac(false,false,'服务端加密未开启')}if(!cryptRuleUtil.pathInEnablePrefix(path)){return new baseUtil.BaseRespFac(false,false,'未命中服务端加密规则')}if(cryptRuleUtil.isPerformanceReport(path,reqData)){return new baseUtil.BaseRespFac(false,false,'性能埋点')}if(!cryptRuleUtil.isHostValid(path)){return new baseUtil.BaseRespFac(false,false,'非sinan网关加密接口')}return new baseUtil.BaseRespFac(true)};const reqEncrypt=(method:string,data:any,header:{[key:string]:any},encryptedResponseHeaderName:string):BaseResp<{cryptoKeyInfo:CryptoKeyInfo,header:any,data:any,}|null>=>{const reqHeader=baseUtil.formatHeader(header);if(reqHeader.contentType){return new baseUtil.BaseRespFac(null,false,'户自定义了请求contentType')}let finalData={};if(method.toUpperCase()==='GET'){const searchParams=baseUtil.formatGetData(data);if(!searchParams){return new baseUtil.BaseRespFac(null,false,`GET请求参数不满足加密的规则:${JSON.stringify(data)}`)}const{success,msg,res}=eccUtil.execEncrypt(searchParams);if(!success){return new baseUtil.BaseRespFac(null,false,`GET请求参数加密失败:${msg}`)}finalData={tmsec:res.encryptedContent}}else{const{success,msg,res}=eccUtil.execEncrypt(JSON.stringify(data));if(!success){return new baseUtil.BaseRespFac(null,false,`${method}请求参数加密失败:${msg}`)}finalData=res.encryptedContent}const{success,msg,res}=eccUtil.execEncrypt(JSON.stringify(header),true);if(!success){return new baseUtil.BaseRespFac(null,false,`请求Header加密失败:${msg}`)}const cryptoHeader={'X-Crypto-Mode':'2','X-Encrypted-Headers':res.encryptedContent,'X-Encrypt-Pub':res.cryptoKeyInfo.serverPubId,'X-Encrypt-Key':res.cryptoKeyInfo.clientPublicKey,'X-Encrypt-Response':'3','X-Response-Header-Name':encryptedResponseHeaderName,};const cryptoSign=eccUtil.getClientCryptoSign(baseUtil._isObject(finalData)?finalData:{body:finalData,},cryptoHeader,res.cryptoKeyInfo.sharedByte);return new baseUtil.BaseRespFac({cryptoKeyInfo:res.cryptoKeyInfo,data:finalData,header:{...cryptoHeader,'Content-Type':'text/plain','X-Crypto-Sign':cryptoSign,},})};const resDecrypt=async(requestTraceId:string,header,data,cryptoKeyInfo:CryptoKeyInfo):Promise<BaseResp<{retry:boolean,header:any,data:any,}>>=>{try{const formatHeader=baseUtil.formatHeader(header);const{'x-encrypt-response':encryptResponseMode,'x-response-header-name':encryptedResponseHeaderName,'x-encrypted-headers':encryptedHeaders,'x-gateway-code':gatewayCode,'content-type':contentType,}=formatHeader;if(!encryptResponseMode||encryptResponseMode==='0'){const dataStr=base64Util.encodeUTF8(new Uint8Array(data));return new baseUtil.BaseRespFac({header,data:JSON.parse(dataStr),retry:false,})}const{retry,success:gwSuccess}=await eccUtil.resolveGwCode(gatewayCode);if(!gwSuccess){const verified=eccUtil.verifyServerCryptoSign(requestTraceId,header);if(!verified){return new baseUtil.BaseRespFac({header:null,data:null,retry:false},false,'响应被篡改')}return new baseUtil.BaseRespFac({header:null,data:null,retry},false,`网关返回错误码:${gatewayCode}`)}let decryptedHeaders={};if(encryptedResponseHeaderName){const{success,msg,res}=eccUtil.execDecrypt(baseUtil.decUrl(encryptedHeaders),cryptoKeyInfo);if(!success){return new baseUtil.BaseRespFac({header:null,data:null,retry:true},false,`解密响应Header失败:${msg}`)}decryptedHeaders=JSON.parse(res)}const needDecode=contentType.indexOf('text/plain')>-1;const cipher=needDecode?baseUtil.decUrl(data):new Uint8Array(data);const{success,msg,res}=eccUtil.execDecrypt(cipher,cryptoKeyInfo);if(!success){return new baseUtil.BaseRespFac({header:null,data:null,retry:true},false,`解密响应Body失败:${msg}`)}const decryptedBody=JSON.parse(res);return new baseUtil.BaseRespFac({retry:false,header:{...header,...decryptedHeaders,},data:decryptedBody,})}catch(e){return new baseUtil.BaseRespFac({header:null,data:null,retry:true},false,`解密响应Body失败:${JSON.stringify(e)}`)}};let dealEncryptionSwitching=false;const dealEncryptionSwitch=async(path:string,traceId:string,resHeader):Promise<void>=>{if((!resHeader||dealEncryptionSwitching)){return}dealEncryptionSwitching=true;const formatHeader=baseUtil.formatHeader(resHeader);const cryptoDisabled=formatHeader['x-crypto-enable']==='0';if((eccUtil.checkCryptoOpen()&&cryptoDisabled)){const verified=eccUtil.verifyServerCryptoSign(traceId,formatHeader);if(!verified){dealEncryptionSwitching=false;baseUtil.logInfo(`验签失败:${path}:${traceId}`);return}}if(cryptoDisabled){eccUtil.closeCrypto()}else if(formatHeader['x-crypto-enable']==='1'){await eccUtil.openCrypto()}dealEncryptionSwitching=false;return};const dealRes=(path:string,traceId:string,resHeader,reqData):BaseResp<boolean>=>{const specialPath=[`${baseUtil.getSinanHost()}/user/login`,`${baseUtil.getSinanHost()}/basic/crypto/lastkey2`,].indexOf(path)>-1;if(specialPath){const formatHeader=baseUtil.formatHeader(resHeader);const verified=eccUtil.verifyServerCryptoSign(traceId,formatHeader);if(!verified){return new baseUtil.BaseRespFac(false,false,`验签失败:${path}:${traceId}`)}eccUtil._updateGlobalPublicKeyInfo(false,resHeader)}return new baseUtil.BaseRespFac(!cryptRuleUtil.isPerformanceReport(path,reqData))};const encryptUtil={init,isCryptoRuleMath,logInfo:baseUtil.logInfo,dealRes,reqEncrypt,resDecrypt,dealEncryptionSwitch,};export default encryptUtil;
|
package/src/encrypt/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import encryptUtil from'./encrypt-util';import{genTraceparent}from'./traceUtils';const util={logInfo:(...args)=>encryptUtil.logInfo(...args),reportFunc:(...args)=>{util.logInfo('reportFunc init fail:',...args)},reqEncrypt:(obj:{url:string,method:string,data:any,header:any}):{data:any,header:any,msg:string,cryptoKeyInfo?:any}=>{const{url,method,data,header={}}=obj;const{success,msg}=encryptUtil.isCryptoRuleMath(url,data);if(!success){return{data,header,msg}}const reqEncryptRes=encryptUtil.reqEncrypt(method,data,header,'');if(!reqEncryptRes.success){return{data,header,msg}}return Object.assign({msg},reqEncryptRes.res)},};let originalRequestApi;let originalUploadFileApi;const requestInit=(utilFunc)=>{if(!wx.reqCryptoFlag){originalRequestApi=wx.request;const{report,composeParamsFunc}=utilFunc;encryptUtil.init(composeParamsFunc);util.reportFunc=(...args)=>{util.logInfo(...args);report('request_encrypt_log',...args)};proxyWxRequest();wx.reqCryptoFlag=true}if(!wx.uploadFileCryptoFlag){originalUploadFileApi=wx.uploadFile;proxyWxUploadFile();wx.uploadFileCryptoFlag=true}};function proxyWxRequest():void{Object.defineProperty(wx,'request',{writable:true,enumerable:true,configurable:true,value(options:any){const{url,method,data,header={},success,fail,complete,dataType,responseType}=options;const traceparent=genTraceparent();const traceId=traceparent.split('-')[1];const originalOptions={...options};if(dataType||responseType){util.reportFunc(url,traceparent,'用户自定义了dataType和responseType');originalRequestApi.call(this,{...originalOptions,success:(res)=>{encryptUtil.dealEncryptionSwitch(url,traceId,res.header);success?.call(this,res)},header:{...header,Traceparent:traceparent},});return}const{data:formatData,header:formatHeader,msg,cryptoKeyInfo}=util.reqEncrypt({url,method,data,header});if(!cryptoKeyInfo){originalRequestApi.call(this,{...originalOptions,success:async(res)=>{const{success:dealSuccess,res:needDealHeader}=encryptUtil.dealRes(url,traceId,res.header,formatData);if(dealSuccess){needDealHeader&&encryptUtil.dealEncryptionSwitch(url,traceId,res.header);success?.call(this,res)}else{util.reportFunc(url,traceparent,`加密验签不通过:${JSON.stringify(res)}`);fail?.call(this,new Error('加密验签不通过'))}},header:{...header,Traceparent:traceparent},});return}let completeResolver;const completePromp=new Promise((resolve)=>{completeResolver=resolve});originalRequestApi.call(this,{...originalOptions,data:formatData,header:{...formatHeader,Traceparent:traceparent},dataType:'其他',responseType:'arraybuffer',success:async(result)=>{const{header:resHeader,data:resData}=result;const{success:decSuccess,msg,res}=await encryptUtil.resDecrypt(traceId,resHeader,resData,cryptoKeyInfo);if(res.retry){util.reportFunc(url,traceparent,`解密失败:${msg}`);const newTraceparent=genTraceparent();const traceId=newTraceparent.split('-')[1];originalRequestApi.call(this,{...originalOptions,success:(res)=>{encryptUtil.dealEncryptionSwitch(url,traceId,res.header);success?.call(this,res)},header:{...header,Traceparent:newTraceparent},});return}if(decSuccess){encryptUtil.dealEncryptionSwitch(url,traceId,resHeader);success?.call(this,res)}else{util.reportFunc(url,traceparent,`解密失败:${msg}`);fail?.call(this,new Error(msg))}const completeRes:any=await completePromp;completeRes.header=resHeader;completeRes.data=resData;complete?.call(this,completeRes)},fail:async(err)=>{fail?.call(this,err);const completeRes:any=await completePromp;complete?.call(this,completeRes)},complete:(res)=>{completeResolver(res)},})},})}function proxyWxUploadFile():void{Object.defineProperty(wx,'uploadFile',{writable:true,enumerable:true,configurable:true,value(options:any){originalUploadFileApi.call(this,Object.assign(options,{header:{...options.header,Traceparent:genTraceparent()},}))},})}export const encryptObjInit=(utilFunc:{composeParamsFunc:Function,report:Function,})=>{requestInit(utilFunc)};
|
package/src/encrypt/md5.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
function safeAdd(x,y){const lsw=(x&0xffff)+(y&0xffff);const msw=(x>>16)+(y>>16)+(lsw>>16);return(msw<<16)|(lsw&0xffff)}function bitRotateLeft(num,cnt){return(num<<cnt)|(num>>>(32-cnt))}function md5cmn(q,a,b,x,s,t){return safeAdd(bitRotateLeft(safeAdd(safeAdd(a,q),safeAdd(x,t)),s),b)}function md5ff(a,b,c,d,x,s,t){return md5cmn((b&c)|(~b&d),a,b,x,s,t)}function md5gg(a,b,c,d,x,s,t){return md5cmn((b&d)|(c&~d),a,b,x,s,t)}function md5hh(a,b,c,d,x,s,t){return md5cmn(b^c^d,a,b,x,s,t)}function md5ii(a,b,c,d,x,s,t){return md5cmn(c^(b|~d),a,b,x,s,t)}function binlMD5(x,len){x[len>>5]|=0x80<<len%32;x[(((len+64)>>>9)<<4)+14]=len;let a=1732584193;let b=-271733879;let c=-1732584194;let d=271733878;for(let i=0;i<x.length;i+=16){const olda=a;const oldb=b;const oldc=c;const oldd=d;a=md5ff(a,b,c,d,x[i],7,-680876936);d=md5ff(d,a,b,c,x[i+1],12,-389564586);c=md5ff(c,d,a,b,x[i+2],17,606105819);b=md5ff(b,c,d,a,x[i+3],22,-1044525330);a=md5ff(a,b,c,d,x[i+4],7,-176418897);d=md5ff(d,a,b,c,x[i+5],12,1200080426);c=md5ff(c,d,a,b,x[i+6],17,-1473231341);b=md5ff(b,c,d,a,x[i+7],22,-45705983);a=md5ff(a,b,c,d,x[i+8],7,1770035416);d=md5ff(d,a,b,c,x[i+9],12,-1958414417);c=md5ff(c,d,a,b,x[i+10],17,-42063);b=md5ff(b,c,d,a,x[i+11],22,-1990404162);a=md5ff(a,b,c,d,x[i+12],7,1804603682);d=md5ff(d,a,b,c,x[i+13],12,-40341101);c=md5ff(c,d,a,b,x[i+14],17,-1502002290);b=md5ff(b,c,d,a,x[i+15],22,1236535329);a=md5gg(a,b,c,d,x[i+1],5,-165796510);d=md5gg(d,a,b,c,x[i+6],9,-1069501632);c=md5gg(c,d,a,b,x[i+11],14,643717713);b=md5gg(b,c,d,a,x[i],20,-373897302);a=md5gg(a,b,c,d,x[i+5],5,-701558691);d=md5gg(d,a,b,c,x[i+10],9,38016083);c=md5gg(c,d,a,b,x[i+15],14,-660478335);b=md5gg(b,c,d,a,x[i+4],20,-405537848);a=md5gg(a,b,c,d,x[i+9],5,568446438);d=md5gg(d,a,b,c,x[i+14],9,-1019803690);c=md5gg(c,d,a,b,x[i+3],14,-187363961);b=md5gg(b,c,d,a,x[i+8],20,1163531501);a=md5gg(a,b,c,d,x[i+13],5,-1444681467);d=md5gg(d,a,b,c,x[i+2],9,-51403784);c=md5gg(c,d,a,b,x[i+7],14,1735328473);b=md5gg(b,c,d,a,x[i+12],20,-1926607734);a=md5hh(a,b,c,d,x[i+5],4,-378558);d=md5hh(d,a,b,c,x[i+8],11,-2022574463);c=md5hh(c,d,a,b,x[i+11],16,1839030562);b=md5hh(b,c,d,a,x[i+14],23,-35309556);a=md5hh(a,b,c,d,x[i+1],4,-1530992060);d=md5hh(d,a,b,c,x[i+4],11,1272893353);c=md5hh(c,d,a,b,x[i+7],16,-155497632);b=md5hh(b,c,d,a,x[i+10],23,-1094730640);a=md5hh(a,b,c,d,x[i+13],4,681279174);d=md5hh(d,a,b,c,x[i],11,-358537222);c=md5hh(c,d,a,b,x[i+3],16,-722521979);b=md5hh(b,c,d,a,x[i+6],23,76029189);a=md5hh(a,b,c,d,x[i+9],4,-640364487);d=md5hh(d,a,b,c,x[i+12],11,-421815835);c=md5hh(c,d,a,b,x[i+15],16,530742520);b=md5hh(b,c,d,a,x[i+2],23,-995338651);a=md5ii(a,b,c,d,x[i],6,-198630844);d=md5ii(d,a,b,c,x[i+7],10,1126891415);c=md5ii(c,d,a,b,x[i+14],15,-1416354905);b=md5ii(b,c,d,a,x[i+5],21,-57434055);a=md5ii(a,b,c,d,x[i+12],6,1700485571);d=md5ii(d,a,b,c,x[i+3],10,-1894986606);c=md5ii(c,d,a,b,x[i+10],15,-1051523);b=md5ii(b,c,d,a,x[i+1],21,-2054922799);a=md5ii(a,b,c,d,x[i+8],6,1873313359);d=md5ii(d,a,b,c,x[i+15],10,-30611744);c=md5ii(c,d,a,b,x[i+6],15,-1560198380);b=md5ii(b,c,d,a,x[i+13],21,1309151649);a=md5ii(a,b,c,d,x[i+4],6,-145523070);d=md5ii(d,a,b,c,x[i+11],10,-1120210379);c=md5ii(c,d,a,b,x[i+2],15,718787259);b=md5ii(b,c,d,a,x[i+9],21,-343485551);a=safeAdd(a,olda);b=safeAdd(b,oldb);c=safeAdd(c,oldc);d=safeAdd(d,oldd)}return[a,b,c,d]}function binl2rstr(input){let output='';const length32=input.length*32;for(let i=0;i<length32;i+=8){output+=String.fromCharCode((input[i>>5]>>>i%32)&0xff)}return output}function rstr2binl(input){const output=[];output[(input.length>>2)-1]=undefined;for(let i=0;i<output.length;i+=1){output[i]=0}const length8=input.length*8;for(let i=0;i<length8;i+=8){output[i>>5]|=(input.charCodeAt(i/8)&0xff)<<i%32}return output}function rstrMD5(s){return binl2rstr(binlMD5(rstr2binl(s),s.length*8))}function str2rstrUTF8(input){return unescape(encodeURIComponent(input))}function rawMD5(s){return rstrMD5(str2rstrUTF8(s))}function rstr2uint8array(input){const output=new Uint8Array(input.length);for(let i=0;i<input.length;i++){output[i]=input.charCodeAt(i)}return output}function md5(string){return rstr2uint8array(rawMD5(string))}export default md5;
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
const Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",encode:function(e){for(var t,r,n,h,o,i,c,a="",s=0;s<e.length;)h=(t=e[s++])>>2,o=(3&t)<<4|(r=e[s++])>>4,i=(15&r)<<2|(n=e[s++])>>6,c=63&n,isNaN(r)?i=c=64:isNaN(n)&&(c=64),a=a+this._keyStr.charAt(h)+this._keyStr.charAt(o)+this._keyStr.charAt(i)+this._keyStr.charAt(c);return a},decode:function(e){var t,r,n,h,o,i,c=[],a=0;for(e=e.replace(/[^A-Za-z0-9\+\/\=]/g,"");a<e.length;)t=this._keyStr.indexOf(e.charAt(a++))<<2|(h=this._keyStr.indexOf(e.charAt(a++)))>>4,r=(15&h)<<4|(o=this._keyStr.indexOf(e.charAt(a++)))>>2,n=(3&o)<<6|(i=this._keyStr.indexOf(e.charAt(a++))),c.push(t),64!=o&&c.push(r),64!=i&&c.push(n);return new Uint8Array(c)},encodeUTF8:function(e){var t,r=[];for(t=0;t<e.length;t++)r.push(String.fromCharCode(e[t]));return decodeURIComponent(escape(r.join("")))},decodeUTF8:function(e){if("string"!=typeof e)throw new TypeError("expected string");var t,r=unescape(encodeURIComponent(e)),n=new Uint8Array(r.length);for(t=0;t<r.length;t++)n[t]=r.charCodeAt(t);return n}};module.exports = Base64;
|
package/src/encrypt/nacl.min.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
!function(r){"use strict";var u64=function(r,e){this.hi=0|r,this.lo=0|e},gf=function(r){var e,n=new Float64Array(16);if(r)for(e=0;e<r.length;e++)n[e]=r[e];return n},randombytes=function(){throw new Error("no PRNG")},e=new Uint8Array(16),n=new Uint8Array(32);n[0]=9;var t=gf(),o=gf([1]),a=gf([56129,1]),c=gf([30883,4953,19914,30187,55467,16705,2637,112,59544,30585,16505,36039,65139,11119,27886,20995]),i=gf([61785,9906,39828,60374,45398,33411,5274,224,53552,61171,33010,6542,64743,22239,55772,9222]),y=gf([54554,36645,11616,51542,42930,38181,51040,26924,56412,64982,57905,49316,21502,52590,14035,8553]),s=gf([26200,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214,26214]),f=gf([41136,18958,6951,50414,58488,44335,6150,12099,55207,15867,153,11085,57099,20417,9344,11139]);function L32(r,e){return r<<e|r>>>32-e}function ld32(r,e){var n=255&r[e+3];return(n=(n=n<<8|255&r[e+2])<<8|255&r[e+1])<<8|255&r[e+0]}function dl64(r,e){var n=r[e]<<24|r[e+1]<<16|r[e+2]<<8|r[e+3],t=r[e+4]<<24|r[e+5]<<16|r[e+6]<<8|r[e+7];return new u64(n,t)}function st32(r,e,n){var t;for(t=0;t<4;t++)r[e+t]=255&n,n>>>=8}function ts64(r,e,n){r[e]=n.hi>>24&255,r[e+1]=n.hi>>16&255,r[e+2]=n.hi>>8&255,r[e+3]=255&n.hi,r[e+4]=n.lo>>24&255,r[e+5]=n.lo>>16&255,r[e+6]=n.lo>>8&255,r[e+7]=255&n.lo}function vn(r,e,n,t,o){var a,c=0;for(a=0;a<o;a++)c|=r[e+a]^n[t+a];return(1&c-1>>>8)-1}function crypto_verify_16(r,e,n,t){return vn(r,e,n,t,16)}function crypto_verify_32(r,e,n,t){return vn(r,e,n,t,32)}function core(r,e,n,t,o){var a,c,i,y=new Uint32Array(16),s=new Uint32Array(16),f=new Uint32Array(16),u=new Uint32Array(4);for(a=0;a<4;a++)s[5*a]=ld32(t,4*a),s[1+a]=ld32(n,4*a),s[6+a]=ld32(e,4*a),s[11+a]=ld32(n,16+4*a);for(a=0;a<16;a++)f[a]=s[a];for(a=0;a<20;a++){for(c=0;c<4;c++){for(i=0;i<4;i++)u[i]=s[(5*c+4*i)%16];for(u[1]^=L32(u[0]+u[3]|0,7),u[2]^=L32(u[1]+u[0]|0,9),u[3]^=L32(u[2]+u[1]|0,13),u[0]^=L32(u[3]+u[2]|0,18),i=0;i<4;i++)y[4*c+(c+i)%4]=u[i]}for(i=0;i<16;i++)s[i]=y[i]}if(o){for(a=0;a<16;a++)s[a]=s[a]+f[a]|0;for(a=0;a<4;a++)s[5*a]=s[5*a]-ld32(t,4*a)|0,s[6+a]=s[6+a]-ld32(e,4*a)|0;for(a=0;a<4;a++)st32(r,4*a,s[5*a]),st32(r,16+4*a,s[6+a])}else for(a=0;a<16;a++)st32(r,4*a,s[a]+f[a]|0)}function crypto_core_salsa20(r,e,n,t){return core(r,e,n,t,!1),0}function crypto_core_hsalsa20(r,e,n,t){return core(r,e,n,t,!0),0}var u=new Uint8Array([101,120,112,97,110,100,32,51,50,45,98,121,116,101,32,107]);function crypto_stream_salsa20_xor(r,e,n,t,o,a,c){var i,y,s=new Uint8Array(16),f=new Uint8Array(64);if(!o)return 0;for(y=0;y<16;y++)s[y]=0;for(y=0;y<8;y++)s[y]=a[y];for(;o>=64;){for(crypto_core_salsa20(f,s,c,u),y=0;y<64;y++)r[e+y]=(n?n[t+y]:0)^f[y];for(i=1,y=8;y<16;y++)i=i+(255&s[y])|0,s[y]=255&i,i>>>=8;o-=64,e+=64,n&&(t+=64)}if(o>0)for(crypto_core_salsa20(f,s,c,u),y=0;y<o;y++)r[e+y]=(n?n[t+y]:0)^f[y];return 0}function crypto_stream_salsa20(r,e,n,t,o){return crypto_stream_salsa20_xor(r,e,null,0,n,t,o)}function crypto_stream(r,e,n,t,o){var a=new Uint8Array(32);return crypto_core_hsalsa20(a,t,o,u),crypto_stream_salsa20(r,e,n,t.subarray(16),a)}function crypto_stream_xor(r,e,n,t,o,a,c){var i=new Uint8Array(32);return crypto_core_hsalsa20(i,a,c,u),crypto_stream_salsa20_xor(r,e,n,t,o,a.subarray(16),i)}function add1305(r,e){var n,t=0;for(n=0;n<17;n++)t=t+(r[n]+e[n]|0)|0,r[n]=255&t,t>>>=8}var _=new Uint32Array([5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,252]);function crypto_onetimeauth(r,e,n,t,o,a){var c,i,y,s,f=new Uint32Array(17),u=new Uint32Array(17),p=new Uint32Array(17),l=new Uint32Array(17),w=new Uint32Array(17);for(y=0;y<17;y++)u[y]=p[y]=0;for(y=0;y<16;y++)u[y]=a[y];for(u[3]&=15,u[4]&=252,u[7]&=15,u[8]&=252,u[11]&=15,u[12]&=252,u[15]&=15;o>0;){for(y=0;y<17;y++)l[y]=0;for(y=0;y<16&&y<o;++y)l[y]=n[t+y];for(l[y]=1,t+=y,o-=y,add1305(p,l),i=0;i<17;i++)for(f[i]=0,y=0;y<17;y++)f[i]=f[i]+p[y]*(y<=i?u[i-y]:320*u[i+17-y]|0)|0;for(i=0;i<17;i++)p[i]=f[i];for(s=0,y=0;y<16;y++)s=s+p[y]|0,p[y]=255&s,s>>>=8;for(s=s+p[16]|0,p[16]=3&s,s=5*(s>>>2)|0,y=0;y<16;y++)s=s+p[y]|0,p[y]=255&s,s>>>=8;s=s+p[16]|0,p[16]=s}for(y=0;y<17;y++)w[y]=p[y];for(add1305(p,_),c=0|-(p[16]>>>7),y=0;y<17;y++)p[y]^=c&(w[y]^p[y]);for(y=0;y<16;y++)l[y]=a[y+16];for(l[16]=0,add1305(p,l),y=0;y<16;y++)r[e+y]=p[y];return 0}function crypto_onetimeauth_verify(r,e,n,t,o,a){var c=new Uint8Array(16);return crypto_onetimeauth(c,0,n,t,o,a),crypto_verify_16(r,e,c,0)}function crypto_secretbox(r,e,n,t,o){var a;if(n<32)return-1;for(crypto_stream_xor(r,0,e,0,n,t,o),crypto_onetimeauth(r,16,r,32,n-32,r),a=0;a<16;a++)r[a]=0;return 0}function crypto_secretbox_open(r,e,n,t,o){var a,c=new Uint8Array(32);if(n<32)return-1;if(crypto_stream(c,0,32,t,o),0!==crypto_onetimeauth_verify(e,16,e,32,n-32,c))return-1;for(crypto_stream_xor(r,0,e,0,n,t,o),a=0;a<32;a++)r[a]=0;return 0}function set25519(r,e){var n;for(n=0;n<16;n++)r[n]=0|e[n]}function car25519(r){var e,n;for(n=0;n<16;n++)r[n]+=65536,e=Math.floor(r[n]/65536),r[(n+1)*(n<15?1:0)]+=e-1+37*(e-1)*(15===n?1:0),r[n]-=65536*e}function sel25519(r,e,n){for(var t,o=~(n-1),a=0;a<16;a++)t=o&(r[a]^e[a]),r[a]^=t,e[a]^=t}function pack25519(r,e){var n,t,o,a=gf(),c=gf();for(n=0;n<16;n++)c[n]=e[n];for(car25519(c),car25519(c),car25519(c),t=0;t<2;t++){for(a[0]=c[0]-65517,n=1;n<15;n++)a[n]=c[n]-65535-(a[n-1]>>16&1),a[n-1]&=65535;a[15]=c[15]-32767-(a[14]>>16&1),o=a[15]>>16&1,a[14]&=65535,sel25519(c,a,1-o)}for(n=0;n<16;n++)r[2*n]=255&c[n],r[2*n+1]=c[n]>>8}function neq25519(r,e){var n=new Uint8Array(32),t=new Uint8Array(32);return pack25519(n,r),pack25519(t,e),crypto_verify_32(n,0,t,0)}function par25519(r){var e=new Uint8Array(32);return pack25519(e,r),1&e[0]}function unpack25519(r,e){var n;for(n=0;n<16;n++)r[n]=e[2*n]+(e[2*n+1]<<8);r[15]&=32767}function A(r,e,n){var t;for(t=0;t<16;t++)r[t]=e[t]+n[t]|0}function Z(r,e,n){var t;for(t=0;t<16;t++)r[t]=e[t]-n[t]|0}function M(r,e,n){var t,o,a=new Float64Array(31);for(t=0;t<31;t++)a[t]=0;for(t=0;t<16;t++)for(o=0;o<16;o++)a[t+o]+=e[t]*n[o];for(t=0;t<15;t++)a[t]+=38*a[t+16];for(t=0;t<16;t++)r[t]=a[t];car25519(r),car25519(r)}function S(r,e){M(r,e,e)}function inv25519(r,e){var n,t=gf();for(n=0;n<16;n++)t[n]=e[n];for(n=253;n>=0;n--)S(t,t),2!==n&&4!==n&&M(t,t,e);for(n=0;n<16;n++)r[n]=t[n]}function pow2523(r,e){var n,t=gf();for(n=0;n<16;n++)t[n]=e[n];for(n=250;n>=0;n--)S(t,t),1!==n&&M(t,t,e);for(n=0;n<16;n++)r[n]=t[n]}function crypto_scalarmult(r,e,n){var t,o,c=new Uint8Array(32),i=new Float64Array(80),y=gf(),s=gf(),f=gf(),u=gf(),_=gf(),p=gf();for(o=0;o<31;o++)c[o]=e[o];for(c[31]=127&e[31]|64,c[0]&=248,unpack25519(i,n),o=0;o<16;o++)s[o]=i[o],u[o]=y[o]=f[o]=0;for(y[0]=u[0]=1,o=254;o>=0;--o)sel25519(y,s,t=c[o>>>3]>>>(7&o)&1),sel25519(f,u,t),A(_,y,f),Z(y,y,f),A(f,s,u),Z(s,s,u),S(u,_),S(p,y),M(y,f,y),M(f,s,_),A(_,y,f),Z(y,y,f),S(s,y),Z(f,u,p),M(y,f,a),A(y,y,u),M(f,f,y),M(y,u,p),M(u,s,i),S(s,_),sel25519(y,s,t),sel25519(f,u,t);for(o=0;o<16;o++)i[o+16]=y[o],i[o+32]=f[o],i[o+48]=s[o],i[o+64]=u[o];var l=i.subarray(32),w=i.subarray(16);return inv25519(l,l),M(w,w,l),pack25519(r,w),0}function crypto_scalarmult_base(r,e){return crypto_scalarmult(r,e,n)}function crypto_box_keypair(r,e){return randombytes(e,32),crypto_scalarmult_base(r,e)}function crypto_box_beforenm(r,n,t){var o=new Uint8Array(32);return crypto_scalarmult(o,t,n),crypto_core_hsalsa20(r,e,o,u)}var p=crypto_secretbox,l=crypto_secretbox_open;function add64(){var r,e,n,t=0,o=0,a=0,c=0,i=65535;for(n=0;n<arguments.length;n++)t+=(r=arguments[n].lo)&i,o+=r>>>16,a+=(e=arguments[n].hi)&i,c+=e>>>16;return new u64((a+=(o+=t>>>16)>>>16)&i|(c+=a>>>16)<<16,t&i|o<<16)}function shr64(r,e){return new u64(r.hi>>>e,r.lo>>>e|r.hi<<32-e)}function xor64(){var r,e=0,n=0;for(r=0;r<arguments.length;r++)e^=arguments[r].lo,n^=arguments[r].hi;return new u64(n,e)}function R(r,e){var n,t,o=32-e;return e<32?(n=r.hi>>>e|r.lo<<o,t=r.lo>>>e|r.hi<<o):e<64&&(n=r.lo>>>e|r.hi<<o,t=r.hi>>>e|r.lo<<o),new u64(n,t)}function Ch(r,e,n){var t=r.hi&e.hi^~r.hi&n.hi,o=r.lo&e.lo^~r.lo&n.lo;return new u64(t,o)}function Maj(r,e,n){var t=r.hi&e.hi^r.hi&n.hi^e.hi&n.hi,o=r.lo&e.lo^r.lo&n.lo^e.lo&n.lo;return new u64(t,o)}function Sigma0(r){return xor64(R(r,28),R(r,34),R(r,39))}function Sigma1(r){return xor64(R(r,14),R(r,18),R(r,41))}function sigma0(r){return xor64(R(r,1),R(r,8),shr64(r,7))}function sigma1(r){return xor64(R(r,19),R(r,61),shr64(r,6))}var w=[new u64(1116352408,3609767458),new u64(1899447441,602891725),new u64(3049323471,3964484399),new u64(3921009573,2173295548),new u64(961987163,4081628472),new u64(1508970993,3053834265),new u64(2453635748,2937671579),new u64(2870763221,3664609560),new u64(3624381080,2734883394),new u64(310598401,1164996542),new u64(607225278,1323610764),new u64(1426881987,3590304994),new u64(1925078388,4068182383),new u64(2162078206,991336113),new u64(2614888103,633803317),new u64(3248222580,3479774868),new u64(3835390401,2666613458),new u64(4022224774,944711139),new u64(264347078,2341262773),new u64(604807628,2007800933),new u64(770255983,1495990901),new u64(1249150122,1856431235),new u64(1555081692,3175218132),new u64(1996064986,2198950837),new u64(2554220882,3999719339),new u64(2821834349,766784016),new u64(2952996808,2566594879),new u64(3210313671,3203337956),new u64(3336571891,1034457026),new u64(3584528711,2466948901),new u64(113926993,3758326383),new u64(338241895,168717936),new u64(666307205,1188179964),new u64(773529912,1546045734),new u64(1294757372,1522805485),new u64(1396182291,2643833823),new u64(1695183700,2343527390),new u64(1986661051,1014477480),new u64(2177026350,1206759142),new u64(2456956037,344077627),new u64(2730485921,1290863460),new u64(2820302411,3158454273),new u64(3259730800,3505952657),new u64(3345764771,106217008),new u64(3516065817,3606008344),new u64(3600352804,1432725776),new u64(4094571909,1467031594),new u64(275423344,851169720),new u64(430227734,3100823752),new u64(506948616,1363258195),new u64(659060556,3750685593),new u64(883997877,3785050280),new u64(958139571,3318307427),new u64(1322822218,3812723403),new u64(1537002063,2003034995),new u64(1747873779,3602036899),new u64(1955562222,1575990012),new u64(2024104815,1125592928),new u64(2227730452,2716904306),new u64(2361852424,442776044),new u64(2428436474,593698344),new u64(2756734187,3733110249),new u64(3204031479,2999351573),new u64(3329325298,3815920427),new u64(3391569614,3928383900),new u64(3515267271,566280711),new u64(3940187606,3454069534),new u64(4118630271,4000239992),new u64(116418474,1914138554),new u64(174292421,2731055270),new u64(289380356,3203993006),new u64(460393269,320620315),new u64(685471733,587496836),new u64(852142971,1086792851),new u64(1017036298,365543100),new u64(1126000580,2618297676),new u64(1288033470,3409855158),new u64(1501505948,4234509866),new u64(1607167915,987167468),new u64(1816402316,1246189591)];function crypto_hashblocks(r,e,n){var t,o,a,c=[],i=[],y=[],s=[];for(o=0;o<8;o++)c[o]=y[o]=dl64(r,8*o);for(var f=0;n>=128;){for(o=0;o<16;o++)s[o]=dl64(e,8*o+f);for(o=0;o<80;o++){for(a=0;a<8;a++)i[a]=y[a];for(t=add64(y[7],Sigma1(y[4]),Ch(y[4],y[5],y[6]),w[o],s[o%16]),i[7]=add64(t,Sigma0(y[0]),Maj(y[0],y[1],y[2])),i[3]=add64(i[3],t),a=0;a<8;a++)y[(a+1)%8]=i[a];if(o%16==15)for(a=0;a<16;a++)s[a]=add64(s[a],s[(a+9)%16],sigma0(s[(a+1)%16]),sigma1(s[(a+14)%16]))}for(o=0;o<8;o++)y[o]=add64(y[o],c[o]),c[o]=y[o];f+=128,n-=128}for(o=0;o<8;o++)ts64(r,8*o,c[o]);return n}var h=new Uint8Array([106,9,230,103,243,188,201,8,187,103,174,133,132,202,167,59,60,110,243,114,254,148,248,43,165,79,245,58,95,29,54,241,81,14,82,127,173,230,130,209,155,5,104,140,43,62,108,31,31,131,217,171,251,65,189,107,91,224,205,25,19,126,33,121]);function crypto_hash(r,e,n){var t,o=new Uint8Array(64),a=new Uint8Array(256),c=n;for(t=0;t<64;t++)o[t]=h[t];for(crypto_hashblocks(o,e,n),n%=128,t=0;t<256;t++)a[t]=0;for(t=0;t<n;t++)a[t]=e[c-n+t];for(a[n]=128,a[(n=256-128*(n<112?1:0))-9]=0,ts64(a,n-8,new u64(c/536870912|0,c<<3)),crypto_hashblocks(o,a,n),t=0;t<64;t++)r[t]=o[t];return 0}function add(r,e){var n=gf(),t=gf(),o=gf(),a=gf(),c=gf(),y=gf(),s=gf(),f=gf(),u=gf();Z(n,r[1],r[0]),Z(u,e[1],e[0]),M(n,n,u),A(t,r[0],r[1]),A(u,e[0],e[1]),M(t,t,u),M(o,r[3],e[3]),M(o,o,i),M(a,r[2],e[2]),A(a,a,a),Z(c,t,n),Z(y,a,o),A(s,a,o),A(f,t,n),M(r[0],c,y),M(r[1],f,s),M(r[2],s,y),M(r[3],c,f)}function cswap(r,e,n){var t;for(t=0;t<4;t++)sel25519(r[t],e[t],n)}function pack(r,e){var n=gf(),t=gf(),o=gf();inv25519(o,e[2]),M(n,e[0],o),M(t,e[1],o),pack25519(r,t),r[31]^=par25519(n)<<7}function scalarmult(r,e,n){var a,c;for(set25519(r[0],t),set25519(r[1],o),set25519(r[2],o),set25519(r[3],t),c=255;c>=0;--c)cswap(r,e,a=n[c/8|0]>>(7&c)&1),add(e,r),add(r,r),cswap(r,e,a)}function scalarbase(r,e){var n=[gf(),gf(),gf(),gf()];set25519(n[0],y),set25519(n[1],s),set25519(n[2],o),M(n[3],y,s),scalarmult(r,n,e)}function crypto_sign_keypair(r,e,n){var t,o=new Uint8Array(64),a=[gf(),gf(),gf(),gf()];for(n||randombytes(e,32),crypto_hash(o,e,32),o[0]&=248,o[31]&=127,o[31]|=64,scalarbase(a,o),pack(r,a),t=0;t<32;t++)e[t+32]=r[t];return 0}var b=new Float64Array([237,211,245,92,26,99,18,88,214,156,247,162,222,249,222,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16]);function modL(r,e){var n,t,o,a;for(t=63;t>=32;--t){for(n=0,o=t-32,a=t-12;o<a;++o)e[o]+=n-16*e[t]*b[o-(t-32)],n=Math.floor((e[o]+128)/256),e[o]-=256*n;e[o]+=n,e[t]=0}for(n=0,o=0;o<32;o++)e[o]+=n-(e[31]>>4)*b[o],n=e[o]>>8,e[o]&=255;for(o=0;o<32;o++)e[o]-=n*b[o];for(t=0;t<32;t++)e[t+1]+=e[t]>>8,r[t]=255&e[t]}function reduce(r){var e,n=new Float64Array(64);for(e=0;e<64;e++)n[e]=r[e];for(e=0;e<64;e++)r[e]=0;modL(r,n)}function crypto_sign(r,e,n,t){var o,a,c=new Uint8Array(64),i=new Uint8Array(64),y=new Uint8Array(64),s=new Float64Array(64),f=[gf(),gf(),gf(),gf()];crypto_hash(c,t,32),c[0]&=248,c[31]&=127,c[31]|=64;var u=n+64;for(o=0;o<n;o++)r[64+o]=e[o];for(o=0;o<32;o++)r[32+o]=c[32+o];for(crypto_hash(y,r.subarray(32),n+32),reduce(y),scalarbase(f,y),pack(r,f),o=32;o<64;o++)r[o]=t[o];for(crypto_hash(i,r,n+64),reduce(i),o=0;o<64;o++)s[o]=0;for(o=0;o<32;o++)s[o]=y[o];for(o=0;o<32;o++)for(a=0;a<32;a++)s[o+a]+=i[o]*c[a];return modL(r.subarray(32),s),u}function crypto_sign_open(r,e,n,a){var i,y=new Uint8Array(32),s=new Uint8Array(64),u=[gf(),gf(),gf(),gf()],_=[gf(),gf(),gf(),gf()];if(n<64)return-1;if(function(r,e){var n=gf(),a=gf(),i=gf(),y=gf(),s=gf(),u=gf(),_=gf();return set25519(r[2],o),unpack25519(r[1],e),S(i,r[1]),M(y,i,c),Z(i,i,r[2]),A(y,r[2],y),S(s,y),S(u,s),M(_,u,s),M(n,_,i),M(n,n,y),pow2523(n,n),M(n,n,i),M(n,n,y),M(n,n,y),M(r[0],n,y),S(a,r[0]),M(a,a,y),neq25519(a,i)&&M(r[0],r[0],f),S(a,r[0]),M(a,a,y),neq25519(a,i)?-1:(par25519(r[0])===e[31]>>7&&Z(r[0],t,r[0]),M(r[3],r[0],r[1]),0)}(_,a))return-1;for(i=0;i<n;i++)r[i]=e[i];for(i=0;i<32;i++)r[i+32]=a[i];if(crypto_hash(s,r,n),reduce(s),scalarmult(u,_,s),scalarbase(_,e.subarray(32)),add(u,_),pack(y,u),n-=64,crypto_verify_32(e,0,y,0)){for(i=0;i<n;i++)r[i]=0;return-1}for(i=0;i<n;i++)r[i]=e[i+64];return n}var g=16,v=64,d=32,m=64;function checkLengths(r,e){if(32!==r.length)throw new Error("bad key size");if(24!==e.length)throw new Error("bad nonce size")}function checkArrayTypes(){for(var r=0;r<arguments.length;r++)if(!(arguments[r]instanceof Uint8Array))throw new TypeError("unexpected type, use Uint8Array")}r.lowlevel={crypto_core_hsalsa20:crypto_core_hsalsa20,crypto_stream_xor:crypto_stream_xor,crypto_stream:crypto_stream,crypto_stream_salsa20_xor:crypto_stream_salsa20_xor,crypto_stream_salsa20:crypto_stream_salsa20,crypto_onetimeauth:crypto_onetimeauth,crypto_onetimeauth_verify:crypto_onetimeauth_verify,crypto_verify_16:crypto_verify_16,crypto_verify_32:crypto_verify_32,crypto_secretbox:crypto_secretbox,crypto_secretbox_open:crypto_secretbox_open,crypto_scalarmult:crypto_scalarmult,crypto_scalarmult_base:crypto_scalarmult_base,crypto_box_beforenm:crypto_box_beforenm,crypto_box_afternm:p,crypto_box:function(r,e,n,t,o,a){var c=new Uint8Array(32);return crypto_box_beforenm(c,o,a),p(r,e,n,t,c)},crypto_box_open:function(r,e,n,t,o,a){var c=new Uint8Array(32);return crypto_box_beforenm(c,o,a),l(r,e,n,t,c)},crypto_box_keypair:crypto_box_keypair,crypto_hash:crypto_hash,crypto_sign:crypto_sign,crypto_sign_keypair:crypto_sign_keypair,crypto_sign_open:crypto_sign_open,crypto_secretbox_KEYBYTES:32,crypto_secretbox_NONCEBYTES:24,crypto_secretbox_ZEROBYTES:32,crypto_secretbox_BOXZEROBYTES:g,crypto_scalarmult_BYTES:32,crypto_scalarmult_SCALARBYTES:32,crypto_box_PUBLICKEYBYTES:32,crypto_box_SECRETKEYBYTES:32,crypto_box_BEFORENMBYTES:32,crypto_box_NONCEBYTES:24,crypto_box_ZEROBYTES:32,crypto_box_BOXZEROBYTES:16,crypto_sign_BYTES:v,crypto_sign_PUBLICKEYBYTES:d,crypto_sign_SECRETKEYBYTES:m,crypto_sign_SEEDBYTES:32,crypto_hash_BYTES:64,gf:gf,D:c,L:b,pack25519:pack25519,unpack25519:unpack25519,M:M,A:A,S:S,Z:Z,pow2523:pow2523,add:add,set25519:set25519,modL:modL,scalarmult:scalarmult,scalarbase:scalarbase},r.randomBytes=function(r){var e=new Uint8Array(r);return randombytes(e,r),e},r.secretbox=function(r,e,n){checkArrayTypes(r,e,n),checkLengths(n,e);for(var t=new Uint8Array(32+r.length),o=new Uint8Array(t.length),a=0;a<r.length;a++)t[a+32]=r[a];return crypto_secretbox(o,t,t.length,e,n),o.subarray(g)},r.secretbox.open=function(r,e,n){checkArrayTypes(r,e,n),checkLengths(n,e);for(var t=new Uint8Array(g+r.length),o=new Uint8Array(t.length),a=0;a<r.length;a++)t[a+g]=r[a];return t.length<32||0!==crypto_secretbox_open(o,t,t.length,e,n)?null:o.subarray(32)},r.secretbox.keyLength=32,r.secretbox.nonceLength=24,r.secretbox.overheadLength=g,r.scalarMult=function(r,e){if(checkArrayTypes(r,e),32!==r.length)throw new Error("bad n size");if(32!==e.length)throw new Error("bad p size");var n=new Uint8Array(32);return crypto_scalarmult(n,r,e),n},r.scalarMult.base=function(r){if(checkArrayTypes(r),32!==r.length)throw new Error("bad n size");var e=new Uint8Array(32);return crypto_scalarmult_base(e,r),e},r.scalarMult.scalarLength=32,r.scalarMult.groupElementLength=32,r.box=function(e,n,t,o){var a=r.box.before(t,o);return r.secretbox(e,n,a)},r.box.before=function(r,e){checkArrayTypes(r,e),function(r,e){if(32!==r.length)throw new Error("bad public key size");if(32!==e.length)throw new Error("bad secret key size")}(r,e);var n=new Uint8Array(32);return crypto_box_beforenm(n,r,e),n},r.box.after=r.secretbox,r.box.open=function(e,n,t,o){var a=r.box.before(t,o);return r.secretbox.open(e,n,a)},r.box.open.after=r.secretbox.open,r.box.keyPair=function(){var r=new Uint8Array(32),e=new Uint8Array(32);return crypto_box_keypair(r,e),{publicKey:r,secretKey:e}},r.box.keyPair.fromSecretKey=function(r){if(checkArrayTypes(r),32!==r.length)throw new Error("bad secret key size");var e=new Uint8Array(32);return crypto_scalarmult_base(e,r),{publicKey:e,secretKey:new Uint8Array(r)}},r.box.publicKeyLength=32,r.box.secretKeyLength=32,r.box.sharedKeyLength=32,r.box.nonceLength=24,r.box.overheadLength=r.secretbox.overheadLength,r.sign=function(r,e){if(checkArrayTypes(r,e),e.length!==m)throw new Error("bad secret key size");var n=new Uint8Array(v+r.length);return crypto_sign(n,r,r.length,e),n},r.sign.open=function(r,e){if(checkArrayTypes(r,e),e.length!==d)throw new Error("bad public key size");var n=new Uint8Array(r.length),t=crypto_sign_open(n,r,r.length,e);if(t<0)return null;for(var o=new Uint8Array(t),a=0;a<o.length;a++)o[a]=n[a];return o},r.sign.detached=function(e,n){for(var t=r.sign(e,n),o=new Uint8Array(v),a=0;a<o.length;a++)o[a]=t[a];return o},r.sign.detached.verify=function(r,e,n){if(checkArrayTypes(r,e,n),e.length!==v)throw new Error("bad signature size");if(n.length!==d)throw new Error("bad public key size");var t,o=new Uint8Array(v+r.length),a=new Uint8Array(v+r.length);for(t=0;t<v;t++)o[t]=e[t];for(t=0;t<r.length;t++)o[t+v]=r[t];return crypto_sign_open(a,o,o.length,n)>=0},r.sign.keyPair=function(){var r=new Uint8Array(d),e=new Uint8Array(m);return crypto_sign_keypair(r,e),{publicKey:r,secretKey:e}},r.sign.keyPair.fromSecretKey=function(r){if(checkArrayTypes(r),r.length!==m)throw new Error("bad secret key size");for(var e=new Uint8Array(d),n=0;n<e.length;n++)e[n]=r[32+n];return{publicKey:e,secretKey:new Uint8Array(r)}},r.sign.keyPair.fromSeed=function(r){if(checkArrayTypes(r),32!==r.length)throw new Error("bad seed size");for(var e=new Uint8Array(d),n=new Uint8Array(m),t=0;t<32;t++)n[t]=r[t];return crypto_sign_keypair(e,n,!0),{publicKey:e,secretKey:n}},r.sign.publicKeyLength=d,r.sign.secretKeyLength=m,r.sign.seedLength=32,r.sign.signatureLength=v,r.hash=function(r){checkArrayTypes(r);var e=new Uint8Array(64);return crypto_hash(e,r,r.length),e},r.hash.hashLength=64,r.verify=function(r,e){return checkArrayTypes(r,e),0!==r.length&&0!==e.length&&(r.length===e.length&&0===vn(r,0,e,0,r.length))},r.setPRNG=function(r){randombytes=r},function(){if("undefined"==typeof wx||"function"!=typeof wx.getRandomValues)throw new Error("No suitable random number generator available.");r.setPRNG((function(r,e){wx.getRandomValues({length:e,success:n=>{const t=new Uint8Array(n.randomValues);for(var o=0;o<e;o++)r[o]=t[o];!function(r){for(var e=0;e<r.length;e++)r[e]=0}(t)}})}))}()}("undefined"!=typeof module&&module.exports?module.exports:self.nacl=self.nacl||{});
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 生成traceparent Id
|
|
3
|
-
* 用途:用于生成version - trace-id - parent-id/span-id - trace-flags规则的id,方便链路追踪
|
|
4
|
-
*/
|
|
5
|
-
/* eslint-disable */
|
|
6
|
-
var CryptoJS=CryptoJS||function(t){var n={},i=n.lib={},s=function(){},r=i.Base={extend:function(t){s.prototype=this;var n=new s;return t&&n.mixIn(t),n.hasOwnProperty("init")||(n.init=function(){n.$super.init.apply(this,arguments)}),n.init.prototype=n,n.$super=this,n},create:function(){var t=this.extend();return t.init.apply(t,arguments),t},init:function(){},mixIn:function(t){for(var n in t)t.hasOwnProperty(n)&&(this[n]=t[n]);t.hasOwnProperty("toString")&&(this.toString=t.toString)},clone:function(){return this.init.prototype.extend(this)}},e=i.WordArray=r.extend({init:function(t,n){t=this.words=t||[],this.sigBytes=null!=n?n:4*t.length},toString:function(t){return(t||o).stringify(this)},concat:function(t){var n=this.words,i=t.words,r=this.sigBytes;if(t=t.sigBytes,this.clamp(),r%4)for(var e=0;e<t;e++)n[r+e>>>2]|=(i[e>>>2]>>>24-e%4*8&255)<<24-(r+e)%4*8;else if(65535<i.length)for(e=0;e<t;e+=4)n[r+e>>>2]=i[e>>>2];else n.push.apply(n,i);return this.sigBytes+=t,this},clamp:function(){var n=this.words,i=this.sigBytes;n[i>>>2]&=4294967295<<32-i%4*8,n.length=t.ceil(i/4)},clone:function(){var t=r.clone.call(this);return t.words=this.words.slice(0),t},random:function(n){for(var i=[],r=0;r<n;r+=4)i.push(4294967296*t.random()|0);return new e.init(i,n)}}),o=(n.enc={}).Hex={stringify:function(t){var n=t.words;t=t.sigBytes;for(var i=[],r=0;r<t;r++){var e=n[r>>>2]>>>24-r%4*8&255;i.push((e>>>4).toString(16)),i.push((15&e).toString(16))}return i.join("")},parse:function(t){for(var n=t.length,i=[],r=0;r<n;r+=2)i[r>>>3]|=parseInt(t.substr(r,2),16)<<24-r%8*4;return new e.init(i,n/2)}};return n}(Math);
|
|
7
|
-
function generateRandomHex(size){return CryptoJS.lib.WordArray.random(size).toString(CryptoJS.enc.Hex)}function genTraceparent(){const version='00';const traceId=generateRandomHex(16);const parentId=generateRandomHex(8);const traceFlags='01';return`${version}-${traceId}-${parentId}-${traceFlags}`}export{genTraceparent,};
|