@tmsfe/tms-core 0.0.20 → 0.0.21

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.
@@ -1,199 +0,0 @@
1
- /**
2
- * @copyright 2017-present, Tencent, Inc. All rights reserved.
3
- * @author Davis.Lu <davislu@tencent.com>
4
- *
5
- * @file crypto tools.
6
- *
7
- **/
8
-
9
- /**
10
- * @public
11
- * @description 基于md5算法对源字符串进行hash,生成hash字符串
12
- * @param {String} str 源字符串
13
- * @returns {String} 源字符串的md5 hash值
14
- */
15
- const md5 = function (str) {
16
- /**
17
- * 将unicode编码成utf-8
18
- * @private
19
- * @param {string} encoedStr unicode字符
20
- * @returns {string} utf8格式的字符串
21
- */
22
- const encodeUtf8 = encoedStr => {
23
- const string = encoedStr.replace(/\r\n/g, '\n');
24
- /**
25
- * @private
26
- * @param {string} c unicode字符
27
- * @returns {string} 字符串
28
- */
29
-
30
- const charCode = c => String.fromCharCode(c);
31
-
32
- const utftextArr = [];
33
-
34
- for (let n = 0; n < string.length; n += 1) {
35
- let c = string.charCodeAt(n);
36
-
37
- if (c < 128) {
38
- utftextArr.push(charCode(c));
39
- } else if (c < 2048) {
40
- utftextArr.push(charCode(c >> 6 | 192), charCode(c & 63 | 128));
41
- } else if (c < 55296 || c >= 57344) {
42
- utftextArr.push(charCode(c >> 12 | 224), charCode(c >> 6 & 63 | 128), charCode(c & 63 | 128));
43
- } else {
44
- c = 65536 + ((c & 1023) << 10 | string.charCodeAt(n += 1) & 1023);
45
- utftextArr.push(charCode(c >> 18 | 240), charCode(c >> 12 & 63 | 128), charCode(c >> 6 & 63 | 128), charCode(c & 63 | 128));
46
- }
47
- }
48
-
49
- return utftextArr.join('');
50
- };
51
- /**
52
- * @private
53
- * @param {string} string 字符串
54
- * @returns {array} 字符串分组
55
- */
56
-
57
-
58
- const convertToWordArray = string => {
59
- const msgLen = string.length;
60
- const lNumberOfWords = ((msgLen + 8 - (msgLen + 8) % 64) / 64 + 1) * 16;
61
- const lWordArray = Array(lNumberOfWords - 1);
62
- let lByteCount = 0;
63
-
64
- while (lByteCount <= msgLen) {
65
- const wordCount = (lByteCount - lByteCount % 4) / 4;
66
- const lBytePosition = lByteCount % 4 * 8;
67
- const byteWord = lByteCount === msgLen ? 0x80 : string.charCodeAt(lByteCount);
68
- lWordArray[wordCount] |= byteWord << lBytePosition;
69
- lByteCount += 1;
70
- }
71
-
72
- lWordArray[lNumberOfWords - 2] = msgLen << 3;
73
- lWordArray[lNumberOfWords - 1] = msgLen >>> 29;
74
- return lWordArray;
75
- };
76
- /**
77
- * @private
78
- * @param {string} lValue 字符串
79
- * @param {number} iShiftBits 移动位数
80
- * @returns {string} 字符串
81
- */
82
-
83
-
84
- const rotateLeft = (lValue, iShiftBits) => lValue << iShiftBits | lValue >>> 32 - iShiftBits;
85
- /**
86
- * @private
87
- * @param {string} lX 字符串
88
- * @param {string} lY 字符串
89
- * @returns {string} 字符串
90
- */
91
-
92
-
93
- const addUnsigned = (lX, lY) => {
94
- const lX8 = lX & 0x80000000;
95
- const lY8 = lY & 0x80000000;
96
- const lX4 = lX & 0x40000000;
97
- const lY4 = lY & 0x40000000;
98
- const lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
99
- if (lX4 & lY4) return lResult ^ 0x80000000 ^ lX8 ^ lY8;
100
- if (!(lX4 | lY4)) return lResult ^ lX8 ^ lY8;
101
- return lResult & 0x40000000 ? lResult ^ 0xC0000000 ^ lX8 ^ lY8 : lResult ^ 0x40000000 ^ lX8 ^ lY8;
102
- };
103
- /**
104
- * @private
105
- * @param {object} recycleData 对象
106
- * @returns {string} 字符串
107
- */
108
-
109
-
110
- const addRecycling = recycleData => {
111
- const {
112
- FN,
113
- a,
114
- b,
115
- c,
116
- d,
117
- x,
118
- s,
119
- ac
120
- } = recycleData;
121
- const aa = addUnsigned(a, addUnsigned(addUnsigned(FN(b, c, d), x), ac));
122
- return addUnsigned(rotateLeft(aa, s), b);
123
- };
124
- /**
125
- * @private
126
- * @param {string} lValue 字符串
127
- * @returns {string} 字符串
128
- */
129
-
130
-
131
- const wordToHex = lValue => {
132
- let WordToHexValue = '';
133
-
134
- for (let lCount = 0; lCount <= 3; lCount += 1) {
135
- const lByte = lValue >>> lCount * 8 & 255;
136
- const WordToHexValueTemp = `0${lByte.toString(16)}`;
137
- WordToHexValue += WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
138
- }
139
-
140
- return WordToHexValue;
141
- };
142
-
143
- let [a, b, c, d] = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476];
144
- const sArr = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]];
145
- const kiArr = '16b05af49e38d27c58be147ad0369cf207e5c3a18f6d4b29'.split('').map(n => parseInt(n, 16));
146
- const hxArr = [0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x2441453, 0xD8A1E681, 0xE7D3FBC8, 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x4881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391]; // eslint-disable-next-line require-jsdoc
147
-
148
- const cyc = (i, r = 0) => (i + r) % 4; // 4组处理位操作函数
149
- // eslint-disable-next-line require-jsdoc
150
-
151
-
152
- const md5F = (x, y, z) => x & y | ~x & z; // eslint-disable-next-line require-jsdoc
153
-
154
-
155
- const md5G = (x, y, z) => x & z | y & ~z; // eslint-disable-next-line require-jsdoc
156
-
157
-
158
- const md5H = (x, y, z) => x ^ y ^ z; // eslint-disable-next-line require-jsdoc
159
-
160
-
161
- const md5I = (x, y, z) => y ^ (x | ~z);
162
-
163
- const string = encodeUtf8(str);
164
- const x = convertToWordArray(string);
165
-
166
- for (let k = 0; k < x.length; k += 16) {
167
- const AA = a;
168
- const BB = b;
169
- const CC = c;
170
- const DD = d;
171
- const arr = [a, d, c, b];
172
- hxArr.forEach((hx, m) => {
173
- const i = m % 16;
174
- const g = m / 16 << 0;
175
- const ki = m < 16 ? m : kiArr[m - 16];
176
- const FN = [md5F, md5G, md5H, md5I][g];
177
- arr[cyc(i)] = addRecycling({
178
- FN,
179
- a: arr[cyc(i)],
180
- b: arr[cyc(i, 3)],
181
- c: arr[cyc(i, 2)],
182
- d: arr[cyc(i, 1)],
183
- x: x[k + ki],
184
- s: sArr[g][i % 4],
185
- ac: hx
186
- });
187
- });
188
- a = addUnsigned(arr[0], AA);
189
- b = addUnsigned(arr[3], BB);
190
- c = addUnsigned(arr[2], CC);
191
- d = addUnsigned(arr[1], DD);
192
- }
193
-
194
- return (wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d)).toLowerCase();
195
- };
196
-
197
- var md5$1 = md5;
198
-
199
- export { md5$1 as m };