@tmsfe/tms-core 0.0.162 → 0.0.164

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.
@@ -0,0 +1,172 @@
1
+ import encryptUtil from './encrypt-util';
2
+ import { genTraceparent } from './traceUtils';
3
+
4
+
5
+ const util = {
6
+ logInfo: (...args) => encryptUtil.logInfo(...args),
7
+ reportFunc: (...args) => {
8
+ util.logInfo('reportFunc init fail:', ...args);
9
+ },
10
+ reqEncrypt: (obj: { url: string, method: string, data: any, header: any }): {
11
+ data: any, header: any, msg: string, cryptoKeyInfo?: any } => {
12
+ const { url, method, data, header = {} } = obj;
13
+ // 判断请求是否满足加密要求,如果不满足则走默认请求
14
+ const { success, msg } = encryptUtil.isCryptoRuleMath(url, data);
15
+ if (!success) {
16
+ return { data, header, msg };
17
+ }
18
+ // 加密请求数据
19
+ const reqEncryptRes = encryptUtil.reqEncrypt(method, data, header, '');
20
+ if (!reqEncryptRes.success) {
21
+ return { data, header, msg };
22
+ }
23
+ return Object.assign({ msg }, reqEncryptRes.res);
24
+ },
25
+ };
26
+ let originalRequestApi;
27
+ let originalUploadFileApi;
28
+ // 劫持wx.request和wx.uploadFile函数
29
+ const requestInit = (utilFunc) => {
30
+ if (!wx.request.cryptoFlag) {
31
+ originalRequestApi = wx.request;
32
+ // 初始化参数加签函数和性能上报函数
33
+ const { report, composeParamsFunc } = utilFunc;
34
+ encryptUtil.init(composeParamsFunc);
35
+ util.reportFunc = (...args) => {
36
+ util.logInfo(...args);
37
+ report('request_encrypt_log', ...args);
38
+ };
39
+ proxyWxRequest();
40
+ wx.request.cryptoFlag = true;
41
+ }
42
+ if (!wx.uploadFile.cryptoFlag) {
43
+ originalUploadFileApi = wx.uploadFile;
44
+ proxyWxUploadFile();
45
+ wx.uploadFile.cryptoFlag = true;
46
+ }
47
+ };
48
+
49
+ function proxyWxRequest(): void {
50
+ Object.defineProperty(wx, 'request', {
51
+ writable: true,
52
+ enumerable: true,
53
+ configurable: true,
54
+ value(options: any) {
55
+ const { url, method, data, header = {}, success, fail, complete, dataType, responseType } = options;
56
+ const traceparent = genTraceparent();
57
+ const traceId = traceparent.split('-')[1];
58
+ const originalOptions = { ...options };
59
+
60
+ // 如果用户自定义了dataType或者responseType,则不做处理
61
+ if (dataType || responseType) {
62
+ util.reportFunc(url, traceparent, '用户自定义了dataType和responseType');
63
+ originalRequestApi.call(this, {
64
+ ...originalOptions,
65
+ success: (res) => {
66
+ encryptUtil.dealEncryptionSwitch(url, traceId, res.header);
67
+ success?.call(this, res);
68
+ },
69
+ header: { ...header, Traceparent: traceparent },
70
+ });
71
+ return;
72
+ }
73
+ // 加密请求数据
74
+ const { data: formatData, header: formatHeader, msg, cryptoKeyInfo } = util
75
+ .reqEncrypt({ url, method, data, header });
76
+ if (!cryptoKeyInfo) {
77
+ // 如果没有加密信息,则不走加密
78
+ util.logInfo(url, traceparent, msg);
79
+ originalRequestApi.call(this, {
80
+ ...originalOptions,
81
+ success: async (res) => {
82
+ const { success: dealSuccess, res: needDealHeader } = encryptUtil.dealRes(url, traceId,
83
+ res.header, formatData);
84
+ // 性能埋点接口不走验签逻辑
85
+ if (dealSuccess) {
86
+ needDealHeader && encryptUtil.dealEncryptionSwitch(url, traceId, res.header);
87
+ success?.call(this, res);
88
+ } else {
89
+ util.reportFunc(url, traceparent, `加密验签不通过: ${JSON.stringify(res)}`);
90
+ fail?.call(this, new Error('加密验签不通过'));
91
+ }
92
+ },
93
+ header: { ...header, Traceparent: traceparent },
94
+ });
95
+ return;
96
+ }
97
+
98
+ let completeResolver;
99
+ const completePromp = new Promise((resolve) => {
100
+ completeResolver = resolve;
101
+ });
102
+ originalRequestApi.call(this, {
103
+ ...originalOptions,
104
+ data: formatData,
105
+ header: { ...formatHeader, Traceparent: traceparent },
106
+ dataType: '其他',
107
+ responseType: 'arraybuffer',
108
+ success: async (result) => {
109
+ const { header: resHeader, data: resData } = result;
110
+ const { success: decSuccess, msg, res } = await encryptUtil
111
+ .resDecrypt(traceId, resHeader, resData, cryptoKeyInfo);
112
+ if (res.retry) { // 解密出现问题,需要明文重试
113
+ util.reportFunc(url, traceparent, `解密失败:${msg}`);
114
+ const newTraceparent = genTraceparent();
115
+ const traceId = newTraceparent.split('-')[1];
116
+ originalRequestApi.call(this, {
117
+ ...originalOptions,
118
+ success: (res) => {
119
+ encryptUtil.dealEncryptionSwitch(url, traceId, res.header);
120
+ success?.call(this, res);
121
+ },
122
+ header: { ...header, Traceparent: newTraceparent },
123
+ });
124
+ return;
125
+ }
126
+ if (decSuccess) {
127
+ // util.logInfo(url, traceparent, '解密成功');
128
+ encryptUtil.dealEncryptionSwitch(url, traceId, resHeader);
129
+ success?.call(this, res);
130
+ } else { // 不支持明文重试,且解密失败
131
+ util.reportFunc(url, traceparent, `解密失败:${msg}`);
132
+ fail?.call(this, new Error(msg));
133
+ }
134
+ const completeRes: any = await completePromp;
135
+ completeRes.header = resHeader;
136
+ completeRes.data = resData;
137
+ complete?.call(this, completeRes);
138
+ },
139
+ fail: async (err) => {
140
+ fail?.call(this, err);
141
+ const completeRes: any = await completePromp;
142
+ complete?.call(this, completeRes);
143
+ },
144
+ complete: (res) => {
145
+ completeResolver(res);
146
+ },
147
+ });
148
+ },
149
+ });
150
+ }
151
+
152
+ function proxyWxUploadFile(): void {
153
+ Object.defineProperty(wx, 'uploadFile', {
154
+ writable: true,
155
+ enumerable: true,
156
+ configurable: true,
157
+ value(options: any) {
158
+ originalUploadFileApi.call(this, Object.assign(options, {
159
+ header: { ...options.header, Traceparent: genTraceparent() },
160
+ }));
161
+ },
162
+ });
163
+ }
164
+
165
+ export const encryptObjInit = (utilFunc: {
166
+ composeParamsFunc: Function,
167
+ report: Function,
168
+ }) => {
169
+ // 劫持wx.request和wx.uploadFile函数
170
+ requestInit(utilFunc);
171
+ };
172
+
@@ -0,0 +1,2 @@
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;
@@ -0,0 +1,2 @@
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;
@@ -0,0 +1,2 @@
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||{});
@@ -0,0 +1,24 @@
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
+
8
+ // 生成随机的16字节trace-id和8字节parent-id/span-id
9
+ function generateRandomHex(size) {
10
+ return CryptoJS.lib.WordArray.random(size).toString(CryptoJS.enc.Hex);
11
+ }
12
+
13
+ // 生成traceparent ID
14
+ function genTraceparent() {
15
+ const version = '00';
16
+ const traceId = generateRandomHex(16); // 16字节的trace-id
17
+ const parentId = generateRandomHex(8); // 8字节的parent-id/span-id
18
+ const traceFlags = '01'; // 示例标志,表示采样
19
+ return `${version}-${traceId}-${parentId}-${traceFlags}`;
20
+ }
21
+
22
+ export {
23
+ genTraceparent,
24
+ };
package/src/index.js CHANGED
@@ -110,7 +110,6 @@ const getLocationBaseClass = () => LocationBase;
110
110
  const init = (options = {}) => {
111
111
  const {
112
112
  appVersion, wxAppId, secretKey = '', client, defaultHost, cloudEnvId, appEnv, appPagePaths, homePage,
113
- requestEncryptOpen,
114
113
  } = options;
115
114
  const envInfo = {
116
115
  wxAppId,
@@ -152,7 +151,7 @@ const init = (options = {}) => {
152
151
 
153
152
  Request.defaultHost = defaultHost;
154
153
  Request.defaultSecretKey = secretKey;
155
- Request.requestEncryptOpen = requestEncryptOpen;
154
+ Request.encryptInit(secretKey);
156
155
  // 初始化云环境
157
156
  wx.cloud.init({ env: cloudEnvId });
158
157
  };
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 {boolean} [raw] Raw output switch
359
+ * @param {string} [raw] Raw output switch
360
360
  * @returns {string} MD5 output
361
361
  */
362
362
  function md5(string, key, raw) {
package/src/request.js CHANGED
@@ -11,12 +11,10 @@
11
11
  import md5 from './md5';
12
12
  import { getEnvInfo, getAuthInfo } from './env';
13
13
  import { safeJsonParse } from './objUtils';
14
- import encryptObj from './encrypt';
14
+ import { encryptObjInit } from './encrypt/index';
15
15
  import reporter from './report/index';
16
- import { genTraceparent } from './traceUtils';
17
16
 
18
17
  const logger = wx.getLogManager({});
19
- let refreshPromise = null;
20
18
 
21
19
  /**
22
20
  * 用于序列化需要签名的参数
@@ -161,7 +159,16 @@ export default class Request {
161
159
  */
162
160
  static defaultSecretKey = '';
163
161
 
164
- static requestEncryptOpen = true;
162
+ static encryptInit(secretKey) {
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
+ }
165
172
 
166
173
  static getInstance() {
167
174
  if (reqInstance === null) {
@@ -348,94 +355,6 @@ export default class Request {
348
355
  return Promise.resolve({ url });
349
356
  }
350
357
 
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
-
439
358
  /**
440
359
  * 创建发送请求任务
441
360
  * @memberof Request
@@ -445,95 +364,61 @@ export default class Request {
445
364
  * @param {object} header 自定义的请求头
446
365
  * @returns {Promise} 接口返回结果
447
366
  */
448
- async createRequestTask(path, param = {}, method = 'POST', header = {}, doEncrypt = true, retryTimes = 0) {
367
+ async createRequestTask(path, param = {}, method = 'POST', header = {}) {
449
368
  const requestParam = await composeParam(param, this.withAuth, this.baseParam);
450
369
  const data = sign(requestParam, this.secretKey);
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
- }
370
+ return new Promise((resolve, reject) => {
371
+ const requestTime = Date.now();
372
+ const printLog = (isSuccess, res) => {
373
+ // 埋点已经单独打日志了,接口请求数据日志太长影响分析
374
+ if (path.indexOf('basic/event/upload') !== -1) {
375
+ return;
376
+ }
458
377
 
459
- // 打车、代驾日志不截断,方便用于回放
460
- const fullApi = path.indexOf('/takecar/') !== -1 || path.indexOf('dd/api/v2/order') !== -1;
378
+ // 打车、代驾日志不截断,方便用于回放
379
+ const fullApi = path.indexOf('/takecar/') !== -1 || path.indexOf('dd/api/v2/order') !== -1;
461
380
 
462
- let result = JSON.stringify(res);
463
- if (isSuccess && !fullApi && result.length > 500) {
464
- result = `${result.substring(0, 500)} 内容太长被截断`;
465
- }
381
+ let result = JSON.stringify(res);
382
+ if (isSuccess && !fullApi && result.length > 500) {
383
+ result = `${result.substring(0, 500)} 内容太长被截断`;
384
+ }
466
385
 
467
- const obj = {
468
- path,
469
- method,
470
- header: JSON.stringify(header),
471
- params: JSON.stringify(data),
472
- duration: Date.now() - requestTime,
473
- };
474
- if (isSuccess) {
475
- obj.res = result;
476
- } else {
477
- obj.err = result;
478
- }
386
+ const obj = {
387
+ path,
388
+ method,
389
+ header: JSON.stringify(header),
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
+ }
479
398
 
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('解密失败,请重试');
399
+ const str = JSON.stringify(obj, null, 2).replace(/\\"/ig, '\'');
400
+ if (isSuccess) {
401
+ logger.log(`接口请求成功:\n${str}`);
402
+ } else {
403
+ logger.warn(`接口请求失败:\n${str}`);
518
404
  }
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
- }
405
+ };
406
+
407
+ wx.request({
408
+ url: this.makeUrl(path),
409
+ header,
410
+ method,
411
+ data,
412
+ enableHttp2: true,
413
+ success: (res) => {
414
+ printLog(true, res.data);
415
+ resolve(res);
416
+ },
417
+ fail: (err) => {
418
+ printLog(false, err);
419
+ reject(err);
420
+ },
421
+ });
422
+ });
538
423
  }
539
424
  }
@@ -55,8 +55,6 @@ const loginFn = async () => {
55
55
  }
56
56
  if (userData.errCode === 0) {
57
57
  userInfo = userData.resData.userInfo;
58
- // 全局挂载公钥数据
59
- wx.$_publicKey = userData.resData.publicKey;
60
58
  // 缓存登录信息
61
59
  wx.setStorageSync('sinanCacheUserInfo', userInfo);
62
60
  }