@tmsfe/tms-core 0.0.152 → 0.0.154

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/src/encrypt.js DELETED
@@ -1,327 +0,0 @@
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;
@@ -1,2 +0,0 @@
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/traceUtils.js DELETED
@@ -1,23 +0,0 @@
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
- };