@nebulaaa/md5 0.0.0-stage → 1.0.0
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/dist/md5.cjs +301 -0
- package/dist/md5.d.ts +30 -0
- package/dist/md5.min.cjs +1 -0
- package/dist/md5.min.mjs +1 -0
- package/dist/md5.mjs +296 -0
- package/dist/package.json +34 -0
- package/package.json +66 -6
- package/README.md +0 -3
package/dist/md5.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* MD5 编码工具
|
|
5
|
+
*
|
|
6
|
+
* 提供 MD5 编码能力,支持 ASCII/UTF8
|
|
7
|
+
*/
|
|
8
|
+
/** MD5 初始状态常量 */
|
|
9
|
+
const MD5_STATE_IDENTITY = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
|
|
10
|
+
/** MD5 空缓冲区常量 */
|
|
11
|
+
const MD5_BUFFER_IDENTITY = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
12
|
+
/** 十六进制字符集 */
|
|
13
|
+
const HEX_CHARS = '0123456789abcdef';
|
|
14
|
+
/** 空状态常量 */
|
|
15
|
+
const EMPTY_STATE = new Int32Array(4);
|
|
16
|
+
/**
|
|
17
|
+
* 将字节数组转换为十六进制字符串
|
|
18
|
+
* @param x - 32位整数数组
|
|
19
|
+
* @returns 十六进制字符串
|
|
20
|
+
*/
|
|
21
|
+
function bytesToHex(x) {
|
|
22
|
+
const hexOut = Array(32).fill('');
|
|
23
|
+
let offset;
|
|
24
|
+
let j;
|
|
25
|
+
for (let i = 0; i < 4; i += 1) {
|
|
26
|
+
offset = i * 8;
|
|
27
|
+
let n = x[i];
|
|
28
|
+
for (j = 0; j < 8; j += 2) {
|
|
29
|
+
hexOut[offset + 1 + j] = HEX_CHARS.charAt(n & 0x0f);
|
|
30
|
+
n >>>= 4;
|
|
31
|
+
hexOut[offset + 0 + j] = HEX_CHARS.charAt(n & 0x0f);
|
|
32
|
+
n >>>= 4;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return hexOut.join('');
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* MD5 核心循环算法
|
|
39
|
+
* @param x - 状态数组
|
|
40
|
+
* @param k - 密钥数组
|
|
41
|
+
*/
|
|
42
|
+
function md5cycle(x, k) {
|
|
43
|
+
let a = x[0];
|
|
44
|
+
let b = x[1];
|
|
45
|
+
let c = x[2];
|
|
46
|
+
let d = x[3];
|
|
47
|
+
// 第一轮 (F)
|
|
48
|
+
a += (((b & c) | (~b & d)) + k[0] - 680876936) | 0;
|
|
49
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
50
|
+
d += (((a & b) | (~a & c)) + k[1] - 389564586) | 0;
|
|
51
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
52
|
+
c += (((d & a) | (~d & b)) + k[2] + 606105819) | 0;
|
|
53
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
54
|
+
b += (((c & d) | (~c & a)) + k[3] - 1044525330) | 0;
|
|
55
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
56
|
+
a += (((b & c) | (~b & d)) + k[4] - 176418897) | 0;
|
|
57
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
58
|
+
d += (((a & b) | (~a & c)) + k[5] + 1200080426) | 0;
|
|
59
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
60
|
+
c += (((d & a) | (~d & b)) + k[6] - 1473231341) | 0;
|
|
61
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
62
|
+
b += (((c & d) | (~c & a)) + k[7] - 45705983) | 0;
|
|
63
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
64
|
+
a += (((b & c) | (~b & d)) + k[8] + 1770035416) | 0;
|
|
65
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
66
|
+
d += (((a & b) | (~a & c)) + k[9] - 1958414417) | 0;
|
|
67
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
68
|
+
c += (((d & a) | (~d & b)) + k[10] - 42063) | 0;
|
|
69
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
70
|
+
b += (((c & d) | (~c & a)) + k[11] - 1990404162) | 0;
|
|
71
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
72
|
+
a += (((b & c) | (~b & d)) + k[12] + 1804603682) | 0;
|
|
73
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
74
|
+
d += (((a & b) | (~a & c)) + k[13] - 40341101) | 0;
|
|
75
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
76
|
+
c += (((d & a) | (~d & b)) + k[14] - 1502002290) | 0;
|
|
77
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
78
|
+
b += (((c & d) | (~c & a)) + k[15] + 1236535329) | 0;
|
|
79
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
80
|
+
// 第二轮 (G)
|
|
81
|
+
a += (((b & d) | (c & ~d)) + k[1] - 165796510) | 0;
|
|
82
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
83
|
+
d += (((a & c) | (b & ~c)) + k[6] - 1069501632) | 0;
|
|
84
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
85
|
+
c += (((d & b) | (a & ~b)) + k[11] + 643717713) | 0;
|
|
86
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
87
|
+
b += (((c & a) | (d & ~a)) + k[0] - 373897302) | 0;
|
|
88
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
89
|
+
a += (((b & d) | (c & ~d)) + k[5] - 701558691) | 0;
|
|
90
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
91
|
+
d += (((a & c) | (b & ~c)) + k[10] + 38016083) | 0;
|
|
92
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
93
|
+
c += (((d & b) | (a & ~b)) + k[15] - 660478335) | 0;
|
|
94
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
95
|
+
b += (((c & a) | (d & ~a)) + k[4] - 405537848) | 0;
|
|
96
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
97
|
+
a += (((b & d) | (c & ~d)) + k[9] + 568446438) | 0;
|
|
98
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
99
|
+
d += (((a & c) | (b & ~c)) + k[14] - 1019803690) | 0;
|
|
100
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
101
|
+
c += (((d & b) | (a & ~b)) + k[3] - 187363961) | 0;
|
|
102
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
103
|
+
b += (((c & a) | (d & ~a)) + k[8] + 1163531501) | 0;
|
|
104
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
105
|
+
a += (((b & d) | (c & ~d)) + k[13] - 1444681467) | 0;
|
|
106
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
107
|
+
d += (((a & c) | (b & ~c)) + k[2] - 51403784) | 0;
|
|
108
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
109
|
+
c += (((d & b) | (a & ~b)) + k[7] + 1735328473) | 0;
|
|
110
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
111
|
+
b += (((c & a) | (d & ~a)) + k[12] - 1926607734) | 0;
|
|
112
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
113
|
+
// 第三轮 (H)
|
|
114
|
+
a += ((b ^ c ^ d) + k[5] - 378558) | 0;
|
|
115
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
116
|
+
d += ((a ^ b ^ c) + k[8] - 2022574463) | 0;
|
|
117
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
118
|
+
c += ((d ^ a ^ b) + k[11] + 1839030562) | 0;
|
|
119
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
120
|
+
b += ((c ^ d ^ a) + k[14] - 35309556) | 0;
|
|
121
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
122
|
+
a += ((b ^ c ^ d) + k[1] - 1530992060) | 0;
|
|
123
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
124
|
+
d += ((a ^ b ^ c) + k[4] + 1272893353) | 0;
|
|
125
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
126
|
+
c += ((d ^ a ^ b) + k[7] - 155497632) | 0;
|
|
127
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
128
|
+
b += ((c ^ d ^ a) + k[10] - 1094730640) | 0;
|
|
129
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
130
|
+
a += ((b ^ c ^ d) + k[13] + 681279174) | 0;
|
|
131
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
132
|
+
d += ((a ^ b ^ c) + k[0] - 358537222) | 0;
|
|
133
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
134
|
+
c += ((d ^ a ^ b) + k[3] - 722521979) | 0;
|
|
135
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
136
|
+
b += ((c ^ d ^ a) + k[6] + 76029189) | 0;
|
|
137
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
138
|
+
a += ((b ^ c ^ d) + k[9] - 640364487) | 0;
|
|
139
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
140
|
+
d += ((a ^ b ^ c) + k[12] - 421815835) | 0;
|
|
141
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
142
|
+
c += ((d ^ a ^ b) + k[15] + 530742520) | 0;
|
|
143
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
144
|
+
b += ((c ^ d ^ a) + k[2] - 995338651) | 0;
|
|
145
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
146
|
+
// 第四轮 (I)
|
|
147
|
+
a += ((c ^ (b | ~d)) + k[0] - 198630844) | 0;
|
|
148
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
149
|
+
d += ((b ^ (a | ~c)) + k[7] + 1126891415) | 0;
|
|
150
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
151
|
+
c += ((a ^ (d | ~b)) + k[14] - 1416354905) | 0;
|
|
152
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
153
|
+
b += ((d ^ (c | ~a)) + k[5] - 57434055) | 0;
|
|
154
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
155
|
+
a += ((c ^ (b | ~d)) + k[12] + 1700485571) | 0;
|
|
156
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
157
|
+
d += ((b ^ (a | ~c)) + k[3] - 1894986606) | 0;
|
|
158
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
159
|
+
c += ((a ^ (d | ~b)) + k[10] - 1051523) | 0;
|
|
160
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
161
|
+
b += ((d ^ (c | ~a)) + k[1] - 2054922799) | 0;
|
|
162
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
163
|
+
a += ((c ^ (b | ~d)) + k[8] + 1873313359) | 0;
|
|
164
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
165
|
+
d += ((b ^ (a | ~c)) + k[15] - 30611744) | 0;
|
|
166
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
167
|
+
c += ((a ^ (d | ~b)) + k[6] - 1560198380) | 0;
|
|
168
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
169
|
+
b += ((d ^ (c | ~a)) + k[13] + 1309151649) | 0;
|
|
170
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
171
|
+
a += ((c ^ (b | ~d)) + k[4] - 145523070) | 0;
|
|
172
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
173
|
+
d += ((b ^ (a | ~c)) + k[11] - 1120210379) | 0;
|
|
174
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
175
|
+
c += ((a ^ (d | ~b)) + k[2] + 718787259) | 0;
|
|
176
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
177
|
+
b += ((d ^ (c | ~a)) + k[9] - 343485551) | 0;
|
|
178
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
179
|
+
x[0] = (a + x[0]) | 0;
|
|
180
|
+
x[1] = (b + x[1]) | 0;
|
|
181
|
+
x[2] = (c + x[2]) | 0;
|
|
182
|
+
x[3] = (d + x[3]) | 0;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 创建 MD5 哈希器实例
|
|
186
|
+
* @returns MD5 哈希器对象
|
|
187
|
+
*/
|
|
188
|
+
function CreateMd5Hasher() {
|
|
189
|
+
let dataLength = 0;
|
|
190
|
+
let bufferLength = 0;
|
|
191
|
+
const state = new Int32Array(4);
|
|
192
|
+
const buffer = new ArrayBuffer(68);
|
|
193
|
+
const buffer8 = new Uint8Array(buffer, 0, 68);
|
|
194
|
+
const buffer32 = new Uint32Array(buffer, 0, 17);
|
|
195
|
+
/**
|
|
196
|
+
* 初始化哈希器状态
|
|
197
|
+
*/
|
|
198
|
+
function start() {
|
|
199
|
+
dataLength = 0;
|
|
200
|
+
bufferLength = 0;
|
|
201
|
+
state.set(MD5_STATE_IDENTITY);
|
|
202
|
+
return { start, appendStr, end };
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* 追加 UTF-8 字符串
|
|
206
|
+
* @param str - 要追加的字符串
|
|
207
|
+
*/
|
|
208
|
+
function appendStr(str) {
|
|
209
|
+
let bufLen = bufferLength;
|
|
210
|
+
let code;
|
|
211
|
+
for (let i = 0; i < str.length; i += 1) {
|
|
212
|
+
code = str.charCodeAt(i);
|
|
213
|
+
if (code < 128) {
|
|
214
|
+
buffer8[bufLen++] = code;
|
|
215
|
+
}
|
|
216
|
+
else if (code < 0x800) {
|
|
217
|
+
buffer8[bufLen++] = (code >>> 6) + 0xc0;
|
|
218
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
219
|
+
}
|
|
220
|
+
else if (code < 0xd800 || code > 0xdbff) {
|
|
221
|
+
buffer8[bufLen++] = (code >>> 12) + 0xe0;
|
|
222
|
+
buffer8[bufLen++] = ((code >>> 6) & 0x3f) | 0x80;
|
|
223
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
code = (code - 0xd800) * 0x400 + (str.charCodeAt(++i) - 0xdc00) + 0x10000;
|
|
227
|
+
if (code > 0x10ffff) {
|
|
228
|
+
throw new Error('Unicode standard supports code points up to U+10FFFF');
|
|
229
|
+
}
|
|
230
|
+
buffer8[bufLen++] = (code >>> 18) + 0xf0;
|
|
231
|
+
buffer8[bufLen++] = ((code >>> 12) & 0x3f) | 0x80;
|
|
232
|
+
buffer8[bufLen++] = ((code >>> 6) & 0x3f) | 0x80;
|
|
233
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
234
|
+
}
|
|
235
|
+
if (bufLen >= 64) {
|
|
236
|
+
dataLength += 64;
|
|
237
|
+
md5cycle(state, buffer32);
|
|
238
|
+
bufLen -= 64;
|
|
239
|
+
buffer32[0] = buffer32[16];
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
bufferLength = bufLen;
|
|
243
|
+
return { start, appendStr, end };
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* 完成哈希计算
|
|
247
|
+
* @param raw - 是否返回原始字节
|
|
248
|
+
* @returns MD5 哈希值
|
|
249
|
+
*/
|
|
250
|
+
function end(raw = false) {
|
|
251
|
+
const bufLen = bufferLength;
|
|
252
|
+
const i = (bufLen >> 2) + 1;
|
|
253
|
+
dataLength += bufLen;
|
|
254
|
+
const dataBitsLen = dataLength * 8;
|
|
255
|
+
buffer8[bufLen] = 0x80;
|
|
256
|
+
buffer8[bufLen + 1] = buffer8[bufLen + 2] = buffer8[bufLen + 3] = 0;
|
|
257
|
+
buffer32.set(MD5_BUFFER_IDENTITY.subarray(i), i);
|
|
258
|
+
if (bufLen > 55) {
|
|
259
|
+
md5cycle(state, buffer32);
|
|
260
|
+
buffer32.set(MD5_BUFFER_IDENTITY);
|
|
261
|
+
}
|
|
262
|
+
if (dataBitsLen <= 0xffffffff) {
|
|
263
|
+
buffer32[14] = dataBitsLen;
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
|
|
267
|
+
if (matches === null)
|
|
268
|
+
return raw ? EMPTY_STATE : '';
|
|
269
|
+
const lo = parseInt(matches[2], 16);
|
|
270
|
+
const hi = parseInt(matches[1], 16) || 0;
|
|
271
|
+
buffer32[14] = lo;
|
|
272
|
+
buffer32[15] = hi;
|
|
273
|
+
}
|
|
274
|
+
md5cycle(state, buffer32);
|
|
275
|
+
return raw ? state : bytesToHex(state);
|
|
276
|
+
}
|
|
277
|
+
return start();
|
|
278
|
+
}
|
|
279
|
+
// 创建单例哈希器用于一次性哈希
|
|
280
|
+
const ONE_PASS_HASHER = CreateMd5Hasher();
|
|
281
|
+
/**
|
|
282
|
+
* 计算 UTF-8 字符串的 MD5 哈希值
|
|
283
|
+
* @param str - 要计算的字符串
|
|
284
|
+
* @returns MD5 哈希字符串
|
|
285
|
+
*/
|
|
286
|
+
function Md5HashUTF8(str) {
|
|
287
|
+
return ONE_PASS_HASHER.start().appendStr(str).end();
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* 计算 ASCII 字符串的 MD5 哈希值
|
|
291
|
+
* @param str - 要计算的字符串
|
|
292
|
+
* @returns MD5 哈希字符串
|
|
293
|
+
*/
|
|
294
|
+
function Md5HashAscii(str) {
|
|
295
|
+
return ONE_PASS_HASHER.start().appendStr(str).end();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
exports.CreateMd5Hasher = CreateMd5Hasher;
|
|
299
|
+
exports.Md5HashAscii = Md5HashAscii;
|
|
300
|
+
exports.Md5HashUTF8 = Md5HashUTF8;
|
|
301
|
+
exports.Md5Hasher = ONE_PASS_HASHER;
|
package/dist/md5.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MD5 编码工具
|
|
3
|
+
*
|
|
4
|
+
* 提供 MD5 编码能力,支持 ASCII/UTF8
|
|
5
|
+
*/
|
|
6
|
+
interface Hasher {
|
|
7
|
+
appendStr: (str: string) => Hasher;
|
|
8
|
+
end: (raw?: boolean) => string | Int32Array;
|
|
9
|
+
start: () => Hasher;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 创建 MD5 哈希器实例
|
|
13
|
+
* @returns MD5 哈希器对象
|
|
14
|
+
*/
|
|
15
|
+
declare function CreateMd5Hasher(): Hasher;
|
|
16
|
+
declare const ONE_PASS_HASHER: Hasher;
|
|
17
|
+
/**
|
|
18
|
+
* 计算 UTF-8 字符串的 MD5 哈希值
|
|
19
|
+
* @param str - 要计算的字符串
|
|
20
|
+
* @returns MD5 哈希字符串
|
|
21
|
+
*/
|
|
22
|
+
declare function Md5HashUTF8(str: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* 计算 ASCII 字符串的 MD5 哈希值
|
|
25
|
+
* @param str - 要计算的字符串
|
|
26
|
+
* @returns MD5 哈希字符串
|
|
27
|
+
*/
|
|
28
|
+
declare function Md5HashAscii(str: string): string;
|
|
29
|
+
|
|
30
|
+
export { CreateMd5Hasher, Md5HashAscii, Md5HashUTF8, ONE_PASS_HASHER as Md5Hasher };
|
package/dist/md5.min.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const t=new Int32Array([1732584193,-271733879,-1732584194,271733878]),r=new Int32Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),n="0123456789abcdef",e=new Int32Array(4);function s(t,r){let n=t[0],e=t[1],s=t[2],o=t[3];n+=(e&s|~e&o)+r[0]-680876936|0,n=(n<<7|n>>>25)+e|0,o+=(n&e|~n&s)+r[1]-389564586|0,o=(o<<12|o>>>20)+n|0,s+=(o&n|~o&e)+r[2]+606105819|0,s=(s<<17|s>>>15)+o|0,e+=(s&o|~s&n)+r[3]-1044525330|0,e=(e<<22|e>>>10)+s|0,n+=(e&s|~e&o)+r[4]-176418897|0,n=(n<<7|n>>>25)+e|0,o+=(n&e|~n&s)+r[5]+1200080426|0,o=(o<<12|o>>>20)+n|0,s+=(o&n|~o&e)+r[6]-1473231341|0,s=(s<<17|s>>>15)+o|0,e+=(s&o|~s&n)+r[7]-45705983|0,e=(e<<22|e>>>10)+s|0,n+=(e&s|~e&o)+r[8]+1770035416|0,n=(n<<7|n>>>25)+e|0,o+=(n&e|~n&s)+r[9]-1958414417|0,o=(o<<12|o>>>20)+n|0,s+=(o&n|~o&e)+r[10]-42063|0,s=(s<<17|s>>>15)+o|0,e+=(s&o|~s&n)+r[11]-1990404162|0,e=(e<<22|e>>>10)+s|0,n+=(e&s|~e&o)+r[12]+1804603682|0,n=(n<<7|n>>>25)+e|0,o+=(n&e|~n&s)+r[13]-40341101|0,o=(o<<12|o>>>20)+n|0,s+=(o&n|~o&e)+r[14]-1502002290|0,s=(s<<17|s>>>15)+o|0,e+=(s&o|~s&n)+r[15]+1236535329|0,e=(e<<22|e>>>10)+s|0,n+=(e&o|s&~o)+r[1]-165796510|0,n=(n<<5|n>>>27)+e|0,o+=(n&s|e&~s)+r[6]-1069501632|0,o=(o<<9|o>>>23)+n|0,s+=(o&e|n&~e)+r[11]+643717713|0,s=(s<<14|s>>>18)+o|0,e+=(s&n|o&~n)+r[0]-373897302|0,e=(e<<20|e>>>12)+s|0,n+=(e&o|s&~o)+r[5]-701558691|0,n=(n<<5|n>>>27)+e|0,o+=(n&s|e&~s)+r[10]+38016083|0,o=(o<<9|o>>>23)+n|0,s+=(o&e|n&~e)+r[15]-660478335|0,s=(s<<14|s>>>18)+o|0,e+=(s&n|o&~n)+r[4]-405537848|0,e=(e<<20|e>>>12)+s|0,n+=(e&o|s&~o)+r[9]+568446438|0,n=(n<<5|n>>>27)+e|0,o+=(n&s|e&~s)+r[14]-1019803690|0,o=(o<<9|o>>>23)+n|0,s+=(o&e|n&~e)+r[3]-187363961|0,s=(s<<14|s>>>18)+o|0,e+=(s&n|o&~n)+r[8]+1163531501|0,e=(e<<20|e>>>12)+s|0,n+=(e&o|s&~o)+r[13]-1444681467|0,n=(n<<5|n>>>27)+e|0,o+=(n&s|e&~s)+r[2]-51403784|0,o=(o<<9|o>>>23)+n|0,s+=(o&e|n&~e)+r[7]+1735328473|0,s=(s<<14|s>>>18)+o|0,e+=(s&n|o&~n)+r[12]-1926607734|0,e=(e<<20|e>>>12)+s|0,n+=(e^s^o)+r[5]-378558|0,n=(n<<4|n>>>28)+e|0,o+=(n^e^s)+r[8]-2022574463|0,o=(o<<11|o>>>21)+n|0,s+=(o^n^e)+r[11]+1839030562|0,s=(s<<16|s>>>16)+o|0,e+=(s^o^n)+r[14]-35309556|0,e=(e<<23|e>>>9)+s|0,n+=(e^s^o)+r[1]-1530992060|0,n=(n<<4|n>>>28)+e|0,o+=(n^e^s)+r[4]+1272893353|0,o=(o<<11|o>>>21)+n|0,s+=(o^n^e)+r[7]-155497632|0,s=(s<<16|s>>>16)+o|0,e+=(s^o^n)+r[10]-1094730640|0,e=(e<<23|e>>>9)+s|0,n+=(e^s^o)+r[13]+681279174|0,n=(n<<4|n>>>28)+e|0,o+=(n^e^s)+r[0]-358537222|0,o=(o<<11|o>>>21)+n|0,s+=(o^n^e)+r[3]-722521979|0,s=(s<<16|s>>>16)+o|0,e+=(s^o^n)+r[6]+76029189|0,e=(e<<23|e>>>9)+s|0,n+=(e^s^o)+r[9]-640364487|0,n=(n<<4|n>>>28)+e|0,o+=(n^e^s)+r[12]-421815835|0,o=(o<<11|o>>>21)+n|0,s+=(o^n^e)+r[15]+530742520|0,s=(s<<16|s>>>16)+o|0,e+=(s^o^n)+r[2]-995338651|0,e=(e<<23|e>>>9)+s|0,n+=(s^(e|~o))+r[0]-198630844|0,n=(n<<6|n>>>26)+e|0,o+=(e^(n|~s))+r[7]+1126891415|0,o=(o<<10|o>>>22)+n|0,s+=(n^(o|~e))+r[14]-1416354905|0,s=(s<<15|s>>>17)+o|0,e+=(o^(s|~n))+r[5]-57434055|0,e=(e<<21|e>>>11)+s|0,n+=(s^(e|~o))+r[12]+1700485571|0,n=(n<<6|n>>>26)+e|0,o+=(e^(n|~s))+r[3]-1894986606|0,o=(o<<10|o>>>22)+n|0,s+=(n^(o|~e))+r[10]-1051523|0,s=(s<<15|s>>>17)+o|0,e+=(o^(s|~n))+r[1]-2054922799|0,e=(e<<21|e>>>11)+s|0,n+=(s^(e|~o))+r[8]+1873313359|0,n=(n<<6|n>>>26)+e|0,o+=(e^(n|~s))+r[15]-30611744|0,o=(o<<10|o>>>22)+n|0,s+=(n^(o|~e))+r[6]-1560198380|0,s=(s<<15|s>>>17)+o|0,e+=(o^(s|~n))+r[13]+1309151649|0,e=(e<<21|e>>>11)+s|0,n+=(s^(e|~o))+r[4]-145523070|0,n=(n<<6|n>>>26)+e|0,o+=(e^(n|~s))+r[11]-1120210379|0,o=(o<<10|o>>>22)+n|0,s+=(n^(o|~e))+r[2]+718787259|0,s=(s<<15|s>>>17)+o|0,e+=(o^(s|~n))+r[9]-343485551|0,e=(e<<21|e>>>11)+s|0,t[0]=n+t[0]|0,t[1]=e+t[1]|0,t[2]=s+t[2]|0,t[3]=o+t[3]|0}function o(){let o=0,a=0;const c=new Int32Array(4),i=new ArrayBuffer(68),u=new Uint8Array(i,0,68),f=new Uint32Array(i,0,17);function d(){return o=0,a=0,c.set(t),{start:d,appendStr:p,end:l}}function p(t){let r,n=a;for(let e=0;e<t.length;e+=1){if(r=t.charCodeAt(e),r<128)u[n++]=r;else if(r<2048)u[n++]=(r>>>6)+192,u[n++]=63&r|128;else if(r<55296||r>56319)u[n++]=(r>>>12)+224,u[n++]=r>>>6&63|128,u[n++]=63&r|128;else{if(r=1024*(r-55296)+(t.charCodeAt(++e)-56320)+65536,r>1114111)throw new Error("Unicode standard supports code points up to U+10FFFF");u[n++]=(r>>>18)+240,u[n++]=r>>>12&63|128,u[n++]=r>>>6&63|128,u[n++]=63&r|128}n>=64&&(o+=64,s(c,f),n-=64,f[0]=f[16])}return a=n,{start:d,appendStr:p,end:l}}function l(t=!1){const i=a,d=1+(i>>2);o+=i;const p=8*o;if(u[i]=128,u[i+1]=u[i+2]=u[i+3]=0,f.set(r.subarray(d),d),i>55&&(s(c,f),f.set(r)),p<=4294967295)f[14]=p;else{const r=p.toString(16).match(/(.*?)(.{0,8})$/);if(null===r)return t?e:"";const n=parseInt(r[2],16),s=parseInt(r[1],16)||0;f[14]=n,f[15]=s}return s(c,f),t?c:function(t){const r=Array(32).fill("");let e,s;for(let o=0;o<4;o+=1){e=8*o;let a=t[o];for(s=0;s<8;s+=2)r[e+1+s]=n.charAt(15&a),a>>>=4,r[e+0+s]=n.charAt(15&a),a>>>=4}return r.join("")}(c)}return d()}const a=o();exports.CreateMd5Hasher=o,exports.Md5HashAscii=function(t){return a.start().appendStr(t).end()},exports.Md5HashUTF8=function(t){return a.start().appendStr(t).end()},exports.Md5Hasher=a;
|
package/dist/md5.min.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const t=new Int32Array([1732584193,-271733879,-1732584194,271733878]),n=new Int32Array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),r="0123456789abcdef",e=new Int32Array(4);function o(t,n){let r=t[0],e=t[1],o=t[2],a=t[3];r+=(e&o|~e&a)+n[0]-680876936|0,r=(r<<7|r>>>25)+e|0,a+=(r&e|~r&o)+n[1]-389564586|0,a=(a<<12|a>>>20)+r|0,o+=(a&r|~a&e)+n[2]+606105819|0,o=(o<<17|o>>>15)+a|0,e+=(o&a|~o&r)+n[3]-1044525330|0,e=(e<<22|e>>>10)+o|0,r+=(e&o|~e&a)+n[4]-176418897|0,r=(r<<7|r>>>25)+e|0,a+=(r&e|~r&o)+n[5]+1200080426|0,a=(a<<12|a>>>20)+r|0,o+=(a&r|~a&e)+n[6]-1473231341|0,o=(o<<17|o>>>15)+a|0,e+=(o&a|~o&r)+n[7]-45705983|0,e=(e<<22|e>>>10)+o|0,r+=(e&o|~e&a)+n[8]+1770035416|0,r=(r<<7|r>>>25)+e|0,a+=(r&e|~r&o)+n[9]-1958414417|0,a=(a<<12|a>>>20)+r|0,o+=(a&r|~a&e)+n[10]-42063|0,o=(o<<17|o>>>15)+a|0,e+=(o&a|~o&r)+n[11]-1990404162|0,e=(e<<22|e>>>10)+o|0,r+=(e&o|~e&a)+n[12]+1804603682|0,r=(r<<7|r>>>25)+e|0,a+=(r&e|~r&o)+n[13]-40341101|0,a=(a<<12|a>>>20)+r|0,o+=(a&r|~a&e)+n[14]-1502002290|0,o=(o<<17|o>>>15)+a|0,e+=(o&a|~o&r)+n[15]+1236535329|0,e=(e<<22|e>>>10)+o|0,r+=(e&a|o&~a)+n[1]-165796510|0,r=(r<<5|r>>>27)+e|0,a+=(r&o|e&~o)+n[6]-1069501632|0,a=(a<<9|a>>>23)+r|0,o+=(a&e|r&~e)+n[11]+643717713|0,o=(o<<14|o>>>18)+a|0,e+=(o&r|a&~r)+n[0]-373897302|0,e=(e<<20|e>>>12)+o|0,r+=(e&a|o&~a)+n[5]-701558691|0,r=(r<<5|r>>>27)+e|0,a+=(r&o|e&~o)+n[10]+38016083|0,a=(a<<9|a>>>23)+r|0,o+=(a&e|r&~e)+n[15]-660478335|0,o=(o<<14|o>>>18)+a|0,e+=(o&r|a&~r)+n[4]-405537848|0,e=(e<<20|e>>>12)+o|0,r+=(e&a|o&~a)+n[9]+568446438|0,r=(r<<5|r>>>27)+e|0,a+=(r&o|e&~o)+n[14]-1019803690|0,a=(a<<9|a>>>23)+r|0,o+=(a&e|r&~e)+n[3]-187363961|0,o=(o<<14|o>>>18)+a|0,e+=(o&r|a&~r)+n[8]+1163531501|0,e=(e<<20|e>>>12)+o|0,r+=(e&a|o&~a)+n[13]-1444681467|0,r=(r<<5|r>>>27)+e|0,a+=(r&o|e&~o)+n[2]-51403784|0,a=(a<<9|a>>>23)+r|0,o+=(a&e|r&~e)+n[7]+1735328473|0,o=(o<<14|o>>>18)+a|0,e+=(o&r|a&~r)+n[12]-1926607734|0,e=(e<<20|e>>>12)+o|0,r+=(e^o^a)+n[5]-378558|0,r=(r<<4|r>>>28)+e|0,a+=(r^e^o)+n[8]-2022574463|0,a=(a<<11|a>>>21)+r|0,o+=(a^r^e)+n[11]+1839030562|0,o=(o<<16|o>>>16)+a|0,e+=(o^a^r)+n[14]-35309556|0,e=(e<<23|e>>>9)+o|0,r+=(e^o^a)+n[1]-1530992060|0,r=(r<<4|r>>>28)+e|0,a+=(r^e^o)+n[4]+1272893353|0,a=(a<<11|a>>>21)+r|0,o+=(a^r^e)+n[7]-155497632|0,o=(o<<16|o>>>16)+a|0,e+=(o^a^r)+n[10]-1094730640|0,e=(e<<23|e>>>9)+o|0,r+=(e^o^a)+n[13]+681279174|0,r=(r<<4|r>>>28)+e|0,a+=(r^e^o)+n[0]-358537222|0,a=(a<<11|a>>>21)+r|0,o+=(a^r^e)+n[3]-722521979|0,o=(o<<16|o>>>16)+a|0,e+=(o^a^r)+n[6]+76029189|0,e=(e<<23|e>>>9)+o|0,r+=(e^o^a)+n[9]-640364487|0,r=(r<<4|r>>>28)+e|0,a+=(r^e^o)+n[12]-421815835|0,a=(a<<11|a>>>21)+r|0,o+=(a^r^e)+n[15]+530742520|0,o=(o<<16|o>>>16)+a|0,e+=(o^a^r)+n[2]-995338651|0,e=(e<<23|e>>>9)+o|0,r+=(o^(e|~a))+n[0]-198630844|0,r=(r<<6|r>>>26)+e|0,a+=(e^(r|~o))+n[7]+1126891415|0,a=(a<<10|a>>>22)+r|0,o+=(r^(a|~e))+n[14]-1416354905|0,o=(o<<15|o>>>17)+a|0,e+=(a^(o|~r))+n[5]-57434055|0,e=(e<<21|e>>>11)+o|0,r+=(o^(e|~a))+n[12]+1700485571|0,r=(r<<6|r>>>26)+e|0,a+=(e^(r|~o))+n[3]-1894986606|0,a=(a<<10|a>>>22)+r|0,o+=(r^(a|~e))+n[10]-1051523|0,o=(o<<15|o>>>17)+a|0,e+=(a^(o|~r))+n[1]-2054922799|0,e=(e<<21|e>>>11)+o|0,r+=(o^(e|~a))+n[8]+1873313359|0,r=(r<<6|r>>>26)+e|0,a+=(e^(r|~o))+n[15]-30611744|0,a=(a<<10|a>>>22)+r|0,o+=(r^(a|~e))+n[6]-1560198380|0,o=(o<<15|o>>>17)+a|0,e+=(a^(o|~r))+n[13]+1309151649|0,e=(e<<21|e>>>11)+o|0,r+=(o^(e|~a))+n[4]-145523070|0,r=(r<<6|r>>>26)+e|0,a+=(e^(r|~o))+n[11]-1120210379|0,a=(a<<10|a>>>22)+r|0,o+=(r^(a|~e))+n[2]+718787259|0,o=(o<<15|o>>>17)+a|0,e+=(a^(o|~r))+n[9]-343485551|0,e=(e<<21|e>>>11)+o|0,t[0]=r+t[0]|0,t[1]=e+t[1]|0,t[2]=o+t[2]|0,t[3]=a+t[3]|0}function a(){let a=0,s=0;const c=new Int32Array(4),f=new ArrayBuffer(68),i=new Uint8Array(f,0,68),u=new Uint32Array(f,0,17);function d(){return a=0,s=0,c.set(t),{start:d,appendStr:l,end:p}}function l(t){let n,r=s;for(let e=0;e<t.length;e+=1){if(n=t.charCodeAt(e),n<128)i[r++]=n;else if(n<2048)i[r++]=(n>>>6)+192,i[r++]=63&n|128;else if(n<55296||n>56319)i[r++]=(n>>>12)+224,i[r++]=n>>>6&63|128,i[r++]=63&n|128;else{if(n=1024*(n-55296)+(t.charCodeAt(++e)-56320)+65536,n>1114111)throw new Error("Unicode standard supports code points up to U+10FFFF");i[r++]=(n>>>18)+240,i[r++]=n>>>12&63|128,i[r++]=n>>>6&63|128,i[r++]=63&n|128}r>=64&&(a+=64,o(c,u),r-=64,u[0]=u[16])}return s=r,{start:d,appendStr:l,end:p}}function p(t=!1){const f=s,d=1+(f>>2);a+=f;const l=8*a;if(i[f]=128,i[f+1]=i[f+2]=i[f+3]=0,u.set(n.subarray(d),d),f>55&&(o(c,u),u.set(n)),l<=4294967295)u[14]=l;else{const n=l.toString(16).match(/(.*?)(.{0,8})$/);if(null===n)return t?e:"";const r=parseInt(n[2],16),o=parseInt(n[1],16)||0;u[14]=r,u[15]=o}return o(c,u),t?c:function(t){const n=Array(32).fill("");let e,o;for(let a=0;a<4;a+=1){e=8*a;let s=t[a];for(o=0;o<8;o+=2)n[e+1+o]=r.charAt(15&s),s>>>=4,n[e+0+o]=r.charAt(15&s),s>>>=4}return n.join("")}(c)}return d()}const s=a();function c(t){return s.start().appendStr(t).end()}function f(t){return s.start().appendStr(t).end()}export{a as CreateMd5Hasher,f as Md5HashAscii,c as Md5HashUTF8,s as Md5Hasher};
|
package/dist/md5.mjs
ADDED
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MD5 编码工具
|
|
3
|
+
*
|
|
4
|
+
* 提供 MD5 编码能力,支持 ASCII/UTF8
|
|
5
|
+
*/
|
|
6
|
+
/** MD5 初始状态常量 */
|
|
7
|
+
const MD5_STATE_IDENTITY = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
|
|
8
|
+
/** MD5 空缓冲区常量 */
|
|
9
|
+
const MD5_BUFFER_IDENTITY = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
|
10
|
+
/** 十六进制字符集 */
|
|
11
|
+
const HEX_CHARS = '0123456789abcdef';
|
|
12
|
+
/** 空状态常量 */
|
|
13
|
+
const EMPTY_STATE = new Int32Array(4);
|
|
14
|
+
/**
|
|
15
|
+
* 将字节数组转换为十六进制字符串
|
|
16
|
+
* @param x - 32位整数数组
|
|
17
|
+
* @returns 十六进制字符串
|
|
18
|
+
*/
|
|
19
|
+
function bytesToHex(x) {
|
|
20
|
+
const hexOut = Array(32).fill('');
|
|
21
|
+
let offset;
|
|
22
|
+
let j;
|
|
23
|
+
for (let i = 0; i < 4; i += 1) {
|
|
24
|
+
offset = i * 8;
|
|
25
|
+
let n = x[i];
|
|
26
|
+
for (j = 0; j < 8; j += 2) {
|
|
27
|
+
hexOut[offset + 1 + j] = HEX_CHARS.charAt(n & 0x0f);
|
|
28
|
+
n >>>= 4;
|
|
29
|
+
hexOut[offset + 0 + j] = HEX_CHARS.charAt(n & 0x0f);
|
|
30
|
+
n >>>= 4;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return hexOut.join('');
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* MD5 核心循环算法
|
|
37
|
+
* @param x - 状态数组
|
|
38
|
+
* @param k - 密钥数组
|
|
39
|
+
*/
|
|
40
|
+
function md5cycle(x, k) {
|
|
41
|
+
let a = x[0];
|
|
42
|
+
let b = x[1];
|
|
43
|
+
let c = x[2];
|
|
44
|
+
let d = x[3];
|
|
45
|
+
// 第一轮 (F)
|
|
46
|
+
a += (((b & c) | (~b & d)) + k[0] - 680876936) | 0;
|
|
47
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
48
|
+
d += (((a & b) | (~a & c)) + k[1] - 389564586) | 0;
|
|
49
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
50
|
+
c += (((d & a) | (~d & b)) + k[2] + 606105819) | 0;
|
|
51
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
52
|
+
b += (((c & d) | (~c & a)) + k[3] - 1044525330) | 0;
|
|
53
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
54
|
+
a += (((b & c) | (~b & d)) + k[4] - 176418897) | 0;
|
|
55
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
56
|
+
d += (((a & b) | (~a & c)) + k[5] + 1200080426) | 0;
|
|
57
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
58
|
+
c += (((d & a) | (~d & b)) + k[6] - 1473231341) | 0;
|
|
59
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
60
|
+
b += (((c & d) | (~c & a)) + k[7] - 45705983) | 0;
|
|
61
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
62
|
+
a += (((b & c) | (~b & d)) + k[8] + 1770035416) | 0;
|
|
63
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
64
|
+
d += (((a & b) | (~a & c)) + k[9] - 1958414417) | 0;
|
|
65
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
66
|
+
c += (((d & a) | (~d & b)) + k[10] - 42063) | 0;
|
|
67
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
68
|
+
b += (((c & d) | (~c & a)) + k[11] - 1990404162) | 0;
|
|
69
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
70
|
+
a += (((b & c) | (~b & d)) + k[12] + 1804603682) | 0;
|
|
71
|
+
a = (((a << 7) | (a >>> 25)) + b) | 0;
|
|
72
|
+
d += (((a & b) | (~a & c)) + k[13] - 40341101) | 0;
|
|
73
|
+
d = (((d << 12) | (d >>> 20)) + a) | 0;
|
|
74
|
+
c += (((d & a) | (~d & b)) + k[14] - 1502002290) | 0;
|
|
75
|
+
c = (((c << 17) | (c >>> 15)) + d) | 0;
|
|
76
|
+
b += (((c & d) | (~c & a)) + k[15] + 1236535329) | 0;
|
|
77
|
+
b = (((b << 22) | (b >>> 10)) + c) | 0;
|
|
78
|
+
// 第二轮 (G)
|
|
79
|
+
a += (((b & d) | (c & ~d)) + k[1] - 165796510) | 0;
|
|
80
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
81
|
+
d += (((a & c) | (b & ~c)) + k[6] - 1069501632) | 0;
|
|
82
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
83
|
+
c += (((d & b) | (a & ~b)) + k[11] + 643717713) | 0;
|
|
84
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
85
|
+
b += (((c & a) | (d & ~a)) + k[0] - 373897302) | 0;
|
|
86
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
87
|
+
a += (((b & d) | (c & ~d)) + k[5] - 701558691) | 0;
|
|
88
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
89
|
+
d += (((a & c) | (b & ~c)) + k[10] + 38016083) | 0;
|
|
90
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
91
|
+
c += (((d & b) | (a & ~b)) + k[15] - 660478335) | 0;
|
|
92
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
93
|
+
b += (((c & a) | (d & ~a)) + k[4] - 405537848) | 0;
|
|
94
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
95
|
+
a += (((b & d) | (c & ~d)) + k[9] + 568446438) | 0;
|
|
96
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
97
|
+
d += (((a & c) | (b & ~c)) + k[14] - 1019803690) | 0;
|
|
98
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
99
|
+
c += (((d & b) | (a & ~b)) + k[3] - 187363961) | 0;
|
|
100
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
101
|
+
b += (((c & a) | (d & ~a)) + k[8] + 1163531501) | 0;
|
|
102
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
103
|
+
a += (((b & d) | (c & ~d)) + k[13] - 1444681467) | 0;
|
|
104
|
+
a = (((a << 5) | (a >>> 27)) + b) | 0;
|
|
105
|
+
d += (((a & c) | (b & ~c)) + k[2] - 51403784) | 0;
|
|
106
|
+
d = (((d << 9) | (d >>> 23)) + a) | 0;
|
|
107
|
+
c += (((d & b) | (a & ~b)) + k[7] + 1735328473) | 0;
|
|
108
|
+
c = (((c << 14) | (c >>> 18)) + d) | 0;
|
|
109
|
+
b += (((c & a) | (d & ~a)) + k[12] - 1926607734) | 0;
|
|
110
|
+
b = (((b << 20) | (b >>> 12)) + c) | 0;
|
|
111
|
+
// 第三轮 (H)
|
|
112
|
+
a += ((b ^ c ^ d) + k[5] - 378558) | 0;
|
|
113
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
114
|
+
d += ((a ^ b ^ c) + k[8] - 2022574463) | 0;
|
|
115
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
116
|
+
c += ((d ^ a ^ b) + k[11] + 1839030562) | 0;
|
|
117
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
118
|
+
b += ((c ^ d ^ a) + k[14] - 35309556) | 0;
|
|
119
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
120
|
+
a += ((b ^ c ^ d) + k[1] - 1530992060) | 0;
|
|
121
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
122
|
+
d += ((a ^ b ^ c) + k[4] + 1272893353) | 0;
|
|
123
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
124
|
+
c += ((d ^ a ^ b) + k[7] - 155497632) | 0;
|
|
125
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
126
|
+
b += ((c ^ d ^ a) + k[10] - 1094730640) | 0;
|
|
127
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
128
|
+
a += ((b ^ c ^ d) + k[13] + 681279174) | 0;
|
|
129
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
130
|
+
d += ((a ^ b ^ c) + k[0] - 358537222) | 0;
|
|
131
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
132
|
+
c += ((d ^ a ^ b) + k[3] - 722521979) | 0;
|
|
133
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
134
|
+
b += ((c ^ d ^ a) + k[6] + 76029189) | 0;
|
|
135
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
136
|
+
a += ((b ^ c ^ d) + k[9] - 640364487) | 0;
|
|
137
|
+
a = (((a << 4) | (a >>> 28)) + b) | 0;
|
|
138
|
+
d += ((a ^ b ^ c) + k[12] - 421815835) | 0;
|
|
139
|
+
d = (((d << 11) | (d >>> 21)) + a) | 0;
|
|
140
|
+
c += ((d ^ a ^ b) + k[15] + 530742520) | 0;
|
|
141
|
+
c = (((c << 16) | (c >>> 16)) + d) | 0;
|
|
142
|
+
b += ((c ^ d ^ a) + k[2] - 995338651) | 0;
|
|
143
|
+
b = (((b << 23) | (b >>> 9)) + c) | 0;
|
|
144
|
+
// 第四轮 (I)
|
|
145
|
+
a += ((c ^ (b | ~d)) + k[0] - 198630844) | 0;
|
|
146
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
147
|
+
d += ((b ^ (a | ~c)) + k[7] + 1126891415) | 0;
|
|
148
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
149
|
+
c += ((a ^ (d | ~b)) + k[14] - 1416354905) | 0;
|
|
150
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
151
|
+
b += ((d ^ (c | ~a)) + k[5] - 57434055) | 0;
|
|
152
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
153
|
+
a += ((c ^ (b | ~d)) + k[12] + 1700485571) | 0;
|
|
154
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
155
|
+
d += ((b ^ (a | ~c)) + k[3] - 1894986606) | 0;
|
|
156
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
157
|
+
c += ((a ^ (d | ~b)) + k[10] - 1051523) | 0;
|
|
158
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
159
|
+
b += ((d ^ (c | ~a)) + k[1] - 2054922799) | 0;
|
|
160
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
161
|
+
a += ((c ^ (b | ~d)) + k[8] + 1873313359) | 0;
|
|
162
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
163
|
+
d += ((b ^ (a | ~c)) + k[15] - 30611744) | 0;
|
|
164
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
165
|
+
c += ((a ^ (d | ~b)) + k[6] - 1560198380) | 0;
|
|
166
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
167
|
+
b += ((d ^ (c | ~a)) + k[13] + 1309151649) | 0;
|
|
168
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
169
|
+
a += ((c ^ (b | ~d)) + k[4] - 145523070) | 0;
|
|
170
|
+
a = (((a << 6) | (a >>> 26)) + b) | 0;
|
|
171
|
+
d += ((b ^ (a | ~c)) + k[11] - 1120210379) | 0;
|
|
172
|
+
d = (((d << 10) | (d >>> 22)) + a) | 0;
|
|
173
|
+
c += ((a ^ (d | ~b)) + k[2] + 718787259) | 0;
|
|
174
|
+
c = (((c << 15) | (c >>> 17)) + d) | 0;
|
|
175
|
+
b += ((d ^ (c | ~a)) + k[9] - 343485551) | 0;
|
|
176
|
+
b = (((b << 21) | (b >>> 11)) + c) | 0;
|
|
177
|
+
x[0] = (a + x[0]) | 0;
|
|
178
|
+
x[1] = (b + x[1]) | 0;
|
|
179
|
+
x[2] = (c + x[2]) | 0;
|
|
180
|
+
x[3] = (d + x[3]) | 0;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* 创建 MD5 哈希器实例
|
|
184
|
+
* @returns MD5 哈希器对象
|
|
185
|
+
*/
|
|
186
|
+
function CreateMd5Hasher() {
|
|
187
|
+
let dataLength = 0;
|
|
188
|
+
let bufferLength = 0;
|
|
189
|
+
const state = new Int32Array(4);
|
|
190
|
+
const buffer = new ArrayBuffer(68);
|
|
191
|
+
const buffer8 = new Uint8Array(buffer, 0, 68);
|
|
192
|
+
const buffer32 = new Uint32Array(buffer, 0, 17);
|
|
193
|
+
/**
|
|
194
|
+
* 初始化哈希器状态
|
|
195
|
+
*/
|
|
196
|
+
function start() {
|
|
197
|
+
dataLength = 0;
|
|
198
|
+
bufferLength = 0;
|
|
199
|
+
state.set(MD5_STATE_IDENTITY);
|
|
200
|
+
return { start, appendStr, end };
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* 追加 UTF-8 字符串
|
|
204
|
+
* @param str - 要追加的字符串
|
|
205
|
+
*/
|
|
206
|
+
function appendStr(str) {
|
|
207
|
+
let bufLen = bufferLength;
|
|
208
|
+
let code;
|
|
209
|
+
for (let i = 0; i < str.length; i += 1) {
|
|
210
|
+
code = str.charCodeAt(i);
|
|
211
|
+
if (code < 128) {
|
|
212
|
+
buffer8[bufLen++] = code;
|
|
213
|
+
}
|
|
214
|
+
else if (code < 0x800) {
|
|
215
|
+
buffer8[bufLen++] = (code >>> 6) + 0xc0;
|
|
216
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
217
|
+
}
|
|
218
|
+
else if (code < 0xd800 || code > 0xdbff) {
|
|
219
|
+
buffer8[bufLen++] = (code >>> 12) + 0xe0;
|
|
220
|
+
buffer8[bufLen++] = ((code >>> 6) & 0x3f) | 0x80;
|
|
221
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
code = (code - 0xd800) * 0x400 + (str.charCodeAt(++i) - 0xdc00) + 0x10000;
|
|
225
|
+
if (code > 0x10ffff) {
|
|
226
|
+
throw new Error('Unicode standard supports code points up to U+10FFFF');
|
|
227
|
+
}
|
|
228
|
+
buffer8[bufLen++] = (code >>> 18) + 0xf0;
|
|
229
|
+
buffer8[bufLen++] = ((code >>> 12) & 0x3f) | 0x80;
|
|
230
|
+
buffer8[bufLen++] = ((code >>> 6) & 0x3f) | 0x80;
|
|
231
|
+
buffer8[bufLen++] = (code & 0x3f) | 0x80;
|
|
232
|
+
}
|
|
233
|
+
if (bufLen >= 64) {
|
|
234
|
+
dataLength += 64;
|
|
235
|
+
md5cycle(state, buffer32);
|
|
236
|
+
bufLen -= 64;
|
|
237
|
+
buffer32[0] = buffer32[16];
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
bufferLength = bufLen;
|
|
241
|
+
return { start, appendStr, end };
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* 完成哈希计算
|
|
245
|
+
* @param raw - 是否返回原始字节
|
|
246
|
+
* @returns MD5 哈希值
|
|
247
|
+
*/
|
|
248
|
+
function end(raw = false) {
|
|
249
|
+
const bufLen = bufferLength;
|
|
250
|
+
const i = (bufLen >> 2) + 1;
|
|
251
|
+
dataLength += bufLen;
|
|
252
|
+
const dataBitsLen = dataLength * 8;
|
|
253
|
+
buffer8[bufLen] = 0x80;
|
|
254
|
+
buffer8[bufLen + 1] = buffer8[bufLen + 2] = buffer8[bufLen + 3] = 0;
|
|
255
|
+
buffer32.set(MD5_BUFFER_IDENTITY.subarray(i), i);
|
|
256
|
+
if (bufLen > 55) {
|
|
257
|
+
md5cycle(state, buffer32);
|
|
258
|
+
buffer32.set(MD5_BUFFER_IDENTITY);
|
|
259
|
+
}
|
|
260
|
+
if (dataBitsLen <= 0xffffffff) {
|
|
261
|
+
buffer32[14] = dataBitsLen;
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
|
|
265
|
+
if (matches === null)
|
|
266
|
+
return raw ? EMPTY_STATE : '';
|
|
267
|
+
const lo = parseInt(matches[2], 16);
|
|
268
|
+
const hi = parseInt(matches[1], 16) || 0;
|
|
269
|
+
buffer32[14] = lo;
|
|
270
|
+
buffer32[15] = hi;
|
|
271
|
+
}
|
|
272
|
+
md5cycle(state, buffer32);
|
|
273
|
+
return raw ? state : bytesToHex(state);
|
|
274
|
+
}
|
|
275
|
+
return start();
|
|
276
|
+
}
|
|
277
|
+
// 创建单例哈希器用于一次性哈希
|
|
278
|
+
const ONE_PASS_HASHER = CreateMd5Hasher();
|
|
279
|
+
/**
|
|
280
|
+
* 计算 UTF-8 字符串的 MD5 哈希值
|
|
281
|
+
* @param str - 要计算的字符串
|
|
282
|
+
* @returns MD5 哈希字符串
|
|
283
|
+
*/
|
|
284
|
+
function Md5HashUTF8(str) {
|
|
285
|
+
return ONE_PASS_HASHER.start().appendStr(str).end();
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* 计算 ASCII 字符串的 MD5 哈希值
|
|
289
|
+
* @param str - 要计算的字符串
|
|
290
|
+
* @returns MD5 哈希字符串
|
|
291
|
+
*/
|
|
292
|
+
function Md5HashAscii(str) {
|
|
293
|
+
return ONE_PASS_HASHER.start().appendStr(str).end();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export { CreateMd5Hasher, Md5HashAscii, Md5HashUTF8, ONE_PASS_HASHER as Md5Hasher };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nebulaaa/md5",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MD5",
|
|
5
|
+
"author": "doooreyn",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"md5",
|
|
9
|
+
"nebulaaa"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=16.0.0",
|
|
13
|
+
"npm": ">=10.0.0"
|
|
14
|
+
},
|
|
15
|
+
"main": "./md5.cjs",
|
|
16
|
+
"module": "./md5.mjs",
|
|
17
|
+
"types": "./md5.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./md5.d.ts",
|
|
21
|
+
"require": "./md5.cjs",
|
|
22
|
+
"import": "./md5.mjs",
|
|
23
|
+
"default": "./md5.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./min": {
|
|
26
|
+
"require": "./md5.min.cjs",
|
|
27
|
+
"import": "./md5.min.mjs"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"registry": "https://registry.npmjs.org/",
|
|
32
|
+
"access": "public"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,66 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nebulaaa/md5",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@nebulaaa/md5",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "MD5",
|
|
5
|
+
"main": "./dist/md5.cjs",
|
|
6
|
+
"module": "./dist/md5.mjs",
|
|
7
|
+
"types": "./dist/md5.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"require": "./dist/md5.cjs",
|
|
11
|
+
"import": "./dist/md5.mjs",
|
|
12
|
+
"types": "./dist/md5.d.ts",
|
|
13
|
+
"default": "./dist/md5.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./min": {
|
|
16
|
+
"require": "./dist/md5.min.cjs",
|
|
17
|
+
"import": "./dist/md5.min.mjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
25
|
+
"build": "npm run clean && rollup -c rollup.config.mjs && node ../scripts/write-dist-package.mjs md5",
|
|
26
|
+
"test": "jest"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"md5",
|
|
30
|
+
"nebulaaa"
|
|
31
|
+
],
|
|
32
|
+
"author": "doooreyn",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"devEngines": {
|
|
35
|
+
"packageManager": {
|
|
36
|
+
"name": "npm",
|
|
37
|
+
"version": ">=10.0.0",
|
|
38
|
+
"onFail": "warn"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=16.0.0",
|
|
43
|
+
"npm": ">=10.0.0"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"registry": "https://registry.npmjs.org/",
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"allowScripts": {
|
|
50
|
+
"unrs-resolver@1.12.2": true,
|
|
51
|
+
"@parcel/watcher@2.6.0": true
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@rollup/plugin-terser": "^1.0.2",
|
|
55
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
56
|
+
"@types/jest": "^30.0.0",
|
|
57
|
+
"@types/node": "^22.10.2",
|
|
58
|
+
"jest": "^30.5.2",
|
|
59
|
+
"rollup": "^4.28.1",
|
|
60
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
61
|
+
"ts-jest": "^29.1.2",
|
|
62
|
+
"ts-node": "^10.9.2",
|
|
63
|
+
"typescript": "5.7.3"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {}
|
|
66
|
+
}
|
package/README.md
DELETED