@ivujs/i-utils 1.1.1 → 1.1.2

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.
Files changed (140) hide show
  1. package/README.md +17 -19
  2. package/dist/cjs/id/index.cjs +26 -43
  3. package/dist/cjs/index.cjs +8 -398
  4. package/dist/es/id/index.d.ts +16 -0
  5. package/dist/es/id/index.mjs +28 -42
  6. package/dist/es/index.d.ts +2 -0
  7. package/dist/es/index.mjs +8 -58
  8. package/dist/index.d.ts +21 -0
  9. package/dist/lib/index.full.cjs.js +38 -0
  10. package/dist/lib/index.full.cjs.min.js +2 -0
  11. package/dist/lib/index.full.cjs.min.js.map +1 -0
  12. package/dist/lib/index.full.esm.js +35 -15822
  13. package/dist/lib/index.full.esm.min.js +2 -58
  14. package/dist/lib/index.full.esm.min.js.map +1 -0
  15. package/dist/lib/index.full.umd.js +39 -16176
  16. package/dist/lib/index.full.umd.min.js +2 -0
  17. package/dist/lib/index.full.umd.min.js.map +1 -0
  18. package/package.json +77 -54
  19. package/dist/cjs/ID-card/index.cjs +0 -122
  20. package/dist/cjs/array/index.cjs +0 -465
  21. package/dist/cjs/clipboard/index.cjs +0 -135
  22. package/dist/cjs/color/index.cjs +0 -210
  23. package/dist/cjs/constants/date.cjs +0 -186
  24. package/dist/cjs/constants/lang.cjs +0 -13
  25. package/dist/cjs/constants/math.cjs +0 -13
  26. package/dist/cjs/constants/regexp.cjs +0 -57
  27. package/dist/cjs/constants/sort.cjs +0 -15
  28. package/dist/cjs/cookie/index.cjs +0 -71
  29. package/dist/cjs/crypto/aes/aes.cjs +0 -533
  30. package/dist/cjs/crypto/aes/index.cjs +0 -28
  31. package/dist/cjs/crypto/base32/base32.cjs +0 -385
  32. package/dist/cjs/crypto/base32/index.cjs +0 -44
  33. package/dist/cjs/crypto/base64/base64.cjs +0 -433
  34. package/dist/cjs/crypto/base64/index.cjs +0 -64
  35. package/dist/cjs/crypto/des/des.cjs +0 -1054
  36. package/dist/cjs/crypto/des/index.cjs +0 -30
  37. package/dist/cjs/crypto/md5/index.cjs +0 -25
  38. package/dist/cjs/crypto/md5/md5.cjs +0 -944
  39. package/dist/cjs/crypto/sha/sha1/index.cjs +0 -25
  40. package/dist/cjs/crypto/sha/sha1/sha1.cjs +0 -602
  41. package/dist/cjs/crypto/sha/sha256/index.cjs +0 -46
  42. package/dist/cjs/crypto/sha/sha256/sha256.cjs +0 -654
  43. package/dist/cjs/crypto/sha/sha3/index.cjs +0 -44
  44. package/dist/cjs/crypto/sha/sha3/sha3.cjs +0 -747
  45. package/dist/cjs/crypto/sha/sha512/index.cjs +0 -88
  46. package/dist/cjs/crypto/sha/sha512/sha512.cjs +0 -1186
  47. package/dist/cjs/crypto/sm/lib/asn1.cjs +0 -167
  48. package/dist/cjs/crypto/sm/lib/ec.cjs +0 -352
  49. package/dist/cjs/crypto/sm/lib/jsbn.cjs +0 -1633
  50. package/dist/cjs/crypto/sm/lib/sm3.cjs +0 -173
  51. package/dist/cjs/crypto/sm/lib/utils.cjs +0 -198
  52. package/dist/cjs/crypto/sm/sm2/index.cjs +0 -122
  53. package/dist/cjs/crypto/sm/sm2/sm2.cjs +0 -269
  54. package/dist/cjs/crypto/sm/sm3/index.cjs +0 -15
  55. package/dist/cjs/crypto/sm/sm3/sm3.cjs +0 -103
  56. package/dist/cjs/crypto/sm/sm4/index.cjs +0 -28
  57. package/dist/cjs/crypto/sm/sm4/sm4.cjs +0 -362
  58. package/dist/cjs/crypto/tea/index.cjs +0 -26
  59. package/dist/cjs/crypto/tea/tea.cjs +0 -215
  60. package/dist/cjs/date/index.cjs +0 -1349
  61. package/dist/cjs/desensitized/index.cjs +0 -74
  62. package/dist/cjs/device/index.cjs +0 -159
  63. package/dist/cjs/dom/index.cjs +0 -176
  64. package/dist/cjs/file/index.cjs +0 -339
  65. package/dist/cjs/function/index.cjs +0 -67
  66. package/dist/cjs/keycode/index.cjs +0 -142
  67. package/dist/cjs/math/index.cjs +0 -284
  68. package/dist/cjs/number/index.cjs +0 -14
  69. package/dist/cjs/object/index.cjs +0 -216
  70. package/dist/cjs/pagination/index.cjs +0 -140
  71. package/dist/cjs/random/index.cjs +0 -25
  72. package/dist/cjs/regexp/index.cjs +0 -109
  73. package/dist/cjs/storage/index.cjs +0 -11
  74. package/dist/cjs/storage/localStorage.cjs +0 -40
  75. package/dist/cjs/storage/sessionStorage.cjs +0 -40
  76. package/dist/cjs/string/index.cjs +0 -338
  77. package/dist/cjs/url/index.cjs +0 -279
  78. package/dist/cjs/validate/index.cjs +0 -487
  79. package/dist/cjs/weapp/index.cjs +0 -151
  80. package/dist/es/ID-card/index.mjs +0 -116
  81. package/dist/es/array/index.mjs +0 -437
  82. package/dist/es/clipboard/index.mjs +0 -129
  83. package/dist/es/color/index.mjs +0 -200
  84. package/dist/es/constants/date.mjs +0 -184
  85. package/dist/es/constants/lang.mjs +0 -11
  86. package/dist/es/constants/math.mjs +0 -11
  87. package/dist/es/constants/regexp.mjs +0 -55
  88. package/dist/es/constants/sort.mjs +0 -13
  89. package/dist/es/cookie/index.mjs +0 -65
  90. package/dist/es/crypto/aes/aes.mjs +0 -529
  91. package/dist/es/crypto/aes/index.mjs +0 -25
  92. package/dist/es/crypto/base32/base32.mjs +0 -381
  93. package/dist/es/crypto/base32/index.mjs +0 -39
  94. package/dist/es/crypto/base64/base64.mjs +0 -427
  95. package/dist/es/crypto/base64/index.mjs +0 -57
  96. package/dist/es/crypto/des/des.mjs +0 -1050
  97. package/dist/es/crypto/des/index.mjs +0 -27
  98. package/dist/es/crypto/md5/index.mjs +0 -22
  99. package/dist/es/crypto/md5/md5.mjs +0 -941
  100. package/dist/es/crypto/sha/sha1/index.mjs +0 -22
  101. package/dist/es/crypto/sha/sha1/sha1.mjs +0 -599
  102. package/dist/es/crypto/sha/sha256/index.mjs +0 -41
  103. package/dist/es/crypto/sha/sha256/sha256.mjs +0 -649
  104. package/dist/es/crypto/sha/sha3/index.mjs +0 -39
  105. package/dist/es/crypto/sha/sha3/sha3.mjs +0 -742
  106. package/dist/es/crypto/sha/sha512/index.mjs +0 -79
  107. package/dist/es/crypto/sha/sha512/sha512.mjs +0 -1177
  108. package/dist/es/crypto/sm/lib/asn1.mjs +0 -164
  109. package/dist/es/crypto/sm/lib/ec.mjs +0 -349
  110. package/dist/es/crypto/sm/lib/jsbn.mjs +0 -1630
  111. package/dist/es/crypto/sm/lib/sm3.mjs +0 -170
  112. package/dist/es/crypto/sm/lib/utils.mjs +0 -186
  113. package/dist/es/crypto/sm/sm2/index.mjs +0 -111
  114. package/dist/es/crypto/sm/sm2/sm2.mjs +0 -258
  115. package/dist/es/crypto/sm/sm3/index.mjs +0 -13
  116. package/dist/es/crypto/sm/sm3/sm3.mjs +0 -99
  117. package/dist/es/crypto/sm/sm4/index.mjs +0 -25
  118. package/dist/es/crypto/sm/sm4/sm4.mjs +0 -359
  119. package/dist/es/crypto/tea/index.mjs +0 -23
  120. package/dist/es/crypto/tea/tea.mjs +0 -211
  121. package/dist/es/date/index.mjs +0 -1262
  122. package/dist/es/desensitized/index.mjs +0 -69
  123. package/dist/es/device/index.mjs +0 -145
  124. package/dist/es/dom/index.mjs +0 -164
  125. package/dist/es/file/index.mjs +0 -323
  126. package/dist/es/function/index.mjs +0 -63
  127. package/dist/es/keycode/index.mjs +0 -139
  128. package/dist/es/math/index.mjs +0 -274
  129. package/dist/es/number/index.mjs +0 -12
  130. package/dist/es/object/index.mjs +0 -203
  131. package/dist/es/pagination/index.mjs +0 -134
  132. package/dist/es/random/index.mjs +0 -22
  133. package/dist/es/regexp/index.mjs +0 -98
  134. package/dist/es/storage/index.mjs +0 -9
  135. package/dist/es/storage/localStorage.mjs +0 -35
  136. package/dist/es/storage/sessionStorage.mjs +0 -35
  137. package/dist/es/string/index.mjs +0 -321
  138. package/dist/es/url/index.mjs +0 -261
  139. package/dist/es/validate/index.mjs +0 -451
  140. package/dist/es/weapp/index.mjs +0 -140
@@ -1,359 +0,0 @@
1
- const DECRYPT = 0;
2
- const ROUND = 32;
3
- const BLOCK = 16;
4
-
5
- const Sbox = [
6
- 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05, 0x2b, 0x67, 0x9a,
7
- 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef,
8
- 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62, 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80,
9
- 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6, 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19,
10
- 0xe6, 0x85, 0x4f, 0xa8, 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d,
11
- 0x35, 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87, 0xd4, 0x00,
12
- 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e, 0xea, 0xbf, 0x8a, 0xd2, 0x40,
13
- 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1, 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55,
14
- 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3, 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23,
15
- 0xab, 0x0d, 0x53, 0x4e, 0x6f, 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c,
16
- 0x5b, 0x51, 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8, 0x0a,
17
- 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0, 0x89, 0x69, 0x97, 0x4a,
18
- 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84, 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d,
19
- 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48,
20
- ];
21
-
22
- const CK = [
23
- 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269, 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9, 0xe0e7eef5,
24
- 0xfc030a11, 0x181f262d, 0x343b4249, 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9, 0xc0c7ced5, 0xdce3eaf1,
25
- 0xf8ff060d, 0x141b2229, 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299, 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed,
26
- 0xf4fb0209, 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279,
27
- ];
28
-
29
- /**
30
- * 16 进制串转字节数组
31
- */
32
- function hexToArray(str) {
33
- const arr = [];
34
- for (let i = 0, len = str.length; i < len; i += 2) {
35
- arr.push(parseInt(str.substr(i, 2), 16));
36
- }
37
- return arr;
38
- }
39
-
40
- /**
41
- * 字节数组转 16 进制串
42
- */
43
- function ArrayToHex(arr) {
44
- return arr
45
- .map((item) => {
46
- item = item.toString(16);
47
- return item.length === 1 ? "0" + item : item;
48
- })
49
- .join("");
50
- }
51
-
52
- /**
53
- * utf8 串转字节数组
54
- */
55
- function utf8ToArray(str) {
56
- const arr = [];
57
-
58
- for (let i = 0, len = str.length; i < len; i++) {
59
- const point = str.codePointAt(i);
60
-
61
- if (point <= 0x007f) {
62
- // 单字节,标量值:00000000 00000000 0zzzzzzz
63
- arr.push(point);
64
- } else if (point <= 0x07ff) {
65
- // 双字节,标量值:00000000 00000yyy yyzzzzzz
66
- arr.push(0xc0 | (point >>> 6)); // 110yyyyy(0xc0-0xdf)
67
- arr.push(0x80 | (point & 0x3f)); // 10zzzzzz(0x80-0xbf)
68
- } else if (point <= 0xd7ff || (point >= 0xe000 && point <= 0xffff)) {
69
- // 三字节:标量值:00000000 xxxxyyyy yyzzzzzz
70
- arr.push(0xe0 | (point >>> 12)); // 1110xxxx(0xe0-0xef)
71
- arr.push(0x80 | ((point >>> 6) & 0x3f)); // 10yyyyyy(0x80-0xbf)
72
- arr.push(0x80 | (point & 0x3f)); // 10zzzzzz(0x80-0xbf)
73
- } else if (point >= 0x010000 && point <= 0x10ffff) {
74
- // 四字节:标量值:000wwwxx xxxxyyyy yyzzzzzz
75
- i++;
76
- arr.push(0xf0 | ((point >>> 18) & 0x1c)); // 11110www(0xf0-0xf7)
77
- arr.push(0x80 | ((point >>> 12) & 0x3f)); // 10xxxxxx(0x80-0xbf)
78
- arr.push(0x80 | ((point >>> 6) & 0x3f)); // 10yyyyyy(0x80-0xbf)
79
- arr.push(0x80 | (point & 0x3f)); // 10zzzzzz(0x80-0xbf)
80
- } else {
81
- // 五、六字节,暂时不支持
82
- arr.push(point);
83
- throw new Error("input is not supported");
84
- }
85
- }
86
-
87
- return arr;
88
- }
89
-
90
- /**
91
- * 字节数组转 utf8 串
92
- */
93
- function arrayToUtf8(arr) {
94
- const str = [];
95
- for (let i = 0, len = arr.length; i < len; i++) {
96
- if (arr[i] >= 0xf0 && arr[i] <= 0xf7) {
97
- // 四字节
98
- str.push(
99
- String.fromCodePoint(
100
- ((arr[i] & 0x07) << 18) + ((arr[i + 1] & 0x3f) << 12) + ((arr[i + 2] & 0x3f) << 6) + (arr[i + 3] & 0x3f),
101
- ),
102
- );
103
- i += 3;
104
- } else if (arr[i] >= 0xe0 && arr[i] <= 0xef) {
105
- // 三字节
106
- str.push(String.fromCodePoint(((arr[i] & 0x0f) << 12) + ((arr[i + 1] & 0x3f) << 6) + (arr[i + 2] & 0x3f)));
107
- i += 2;
108
- } else if (arr[i] >= 0xc0 && arr[i] <= 0xdf) {
109
- // 双字节
110
- str.push(String.fromCodePoint(((arr[i] & 0x1f) << 6) + (arr[i + 1] & 0x3f)));
111
- i++;
112
- } else {
113
- // 单字节
114
- str.push(String.fromCodePoint(arr[i]));
115
- }
116
- }
117
-
118
- return str.join("");
119
- }
120
-
121
- /**
122
- * 32 比特循环左移
123
- */
124
- function rotl(x, n) {
125
- const s = n & 31;
126
- return (x << s) | (x >>> (32 - s));
127
- }
128
-
129
- /**
130
- * 非线性变换
131
- */
132
- function byteSub(a) {
133
- return (
134
- ((Sbox[(a >>> 24) & 0xff] & 0xff) << 24) |
135
- ((Sbox[(a >>> 16) & 0xff] & 0xff) << 16) |
136
- ((Sbox[(a >>> 8) & 0xff] & 0xff) << 8) |
137
- (Sbox[a & 0xff] & 0xff)
138
- );
139
- }
140
-
141
- /**
142
- * 线性变换,加密/解密用
143
- */
144
- function l1(b) {
145
- return b ^ rotl(b, 2) ^ rotl(b, 10) ^ rotl(b, 18) ^ rotl(b, 24);
146
- }
147
-
148
- /**
149
- * 线性变换,生成轮密钥用
150
- */
151
- function l2(b) {
152
- return b ^ rotl(b, 13) ^ rotl(b, 23);
153
- }
154
-
155
- /**
156
- * 以一组 128 比特进行加密/解密操作
157
- */
158
- function sms4Crypt(input, output, roundKey) {
159
- const x = new Array(4);
160
-
161
- // 字节数组转成字数组(此处 1 字 = 32 比特)
162
- const tmp = new Array(4);
163
- for (let i = 0; i < 4; i++) {
164
- tmp[0] = input[4 * i] & 0xff;
165
- tmp[1] = input[4 * i + 1] & 0xff;
166
- tmp[2] = input[4 * i + 2] & 0xff;
167
- tmp[3] = input[4 * i + 3] & 0xff;
168
- x[i] = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
169
- }
170
-
171
- // x[i + 4] = x[i] ^ l1(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ roundKey[i]))
172
- for (let r = 0, mid; r < 32; r += 4) {
173
- mid = x[1] ^ x[2] ^ x[3] ^ roundKey[r + 0];
174
- x[0] ^= l1(byteSub(mid)); // x[4]
175
-
176
- mid = x[2] ^ x[3] ^ x[0] ^ roundKey[r + 1];
177
- x[1] ^= l1(byteSub(mid)); // x[5]
178
-
179
- mid = x[3] ^ x[0] ^ x[1] ^ roundKey[r + 2];
180
- x[2] ^= l1(byteSub(mid)); // x[6]
181
-
182
- mid = x[0] ^ x[1] ^ x[2] ^ roundKey[r + 3];
183
- x[3] ^= l1(byteSub(mid)); // x[7]
184
- }
185
-
186
- // 反序变换
187
- for (let j = 0; j < 16; j += 4) {
188
- output[j] = (x[3 - j / 4] >>> 24) & 0xff;
189
- output[j + 1] = (x[3 - j / 4] >>> 16) & 0xff;
190
- output[j + 2] = (x[3 - j / 4] >>> 8) & 0xff;
191
- output[j + 3] = x[3 - j / 4] & 0xff;
192
- }
193
- }
194
-
195
- /**
196
- * 密钥扩展算法
197
- */
198
- function sms4KeyExt(key, roundKey, cryptFlag) {
199
- const x = new Array(4);
200
-
201
- // 字节数组转成字数组(此处 1 字 = 32 比特)
202
- const tmp = new Array(4);
203
- for (let i = 0; i < 4; i++) {
204
- tmp[0] = key[0 + 4 * i] & 0xff;
205
- tmp[1] = key[1 + 4 * i] & 0xff;
206
- tmp[2] = key[2 + 4 * i] & 0xff;
207
- tmp[3] = key[3 + 4 * i] & 0xff;
208
- x[i] = (tmp[0] << 24) | (tmp[1] << 16) | (tmp[2] << 8) | tmp[3];
209
- }
210
-
211
- // 与系统参数做异或
212
- x[0] ^= 0xa3b1bac6;
213
- x[1] ^= 0x56aa3350;
214
- x[2] ^= 0x677d9197;
215
- x[3] ^= 0xb27022dc;
216
-
217
- // roundKey[i] = x[i + 4] = x[i] ^ l2(byteSub(x[i + 1] ^ x[i + 2] ^ x[i + 3] ^ CK[i]))
218
- for (let r = 0, mid; r < 32; r += 4) {
219
- mid = x[1] ^ x[2] ^ x[3] ^ CK[r + 0];
220
- roundKey[r + 0] = x[0] ^= l2(byteSub(mid)); // x[4]
221
-
222
- mid = x[2] ^ x[3] ^ x[0] ^ CK[r + 1];
223
- roundKey[r + 1] = x[1] ^= l2(byteSub(mid)); // x[5]
224
-
225
- mid = x[3] ^ x[0] ^ x[1] ^ CK[r + 2];
226
- roundKey[r + 2] = x[2] ^= l2(byteSub(mid)); // x[6]
227
-
228
- mid = x[0] ^ x[1] ^ x[2] ^ CK[r + 3];
229
- roundKey[r + 3] = x[3] ^= l2(byteSub(mid)); // x[7]
230
- }
231
-
232
- // 解密时使用反序的轮密钥
233
- if (cryptFlag === DECRYPT) {
234
- for (let r = 0, mid; r < 16; r++) {
235
- mid = roundKey[r];
236
- roundKey[r] = roundKey[31 - r];
237
- roundKey[31 - r] = mid;
238
- }
239
- }
240
- }
241
-
242
- function sm4(inArray, key, cryptFlag, { padding = "pkcs#7", mode, iv = [], output = "string" } = {}) {
243
- if (mode === "cbc") {
244
- // CBC 模式,默认走 ECB 模式
245
- if (typeof iv === "string") iv = hexToArray(iv);
246
- if (iv.length !== 128 / 8) {
247
- // iv 不是 128 比特
248
- throw new Error("iv is invalid");
249
- }
250
- }
251
-
252
- // 检查 key
253
- if (typeof key === "string") key = hexToArray(key);
254
- if (key.length !== 128 / 8) {
255
- // key 不是 128 比特
256
- throw new Error("key is invalid");
257
- }
258
-
259
- // 检查输入
260
- if (typeof inArray === "string") {
261
- if (cryptFlag !== DECRYPT) {
262
- // 加密,输入为 utf8 串
263
- inArray = utf8ToArray(inArray);
264
- } else {
265
- // 解密,输入为 16 进制串
266
- inArray = hexToArray(inArray);
267
- }
268
- } else {
269
- inArray = [...inArray];
270
- }
271
-
272
- // 新增填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
273
- if ((padding === "pkcs#5" || padding === "pkcs#7") && cryptFlag !== DECRYPT) {
274
- const paddingCount = BLOCK - (inArray.length % BLOCK);
275
- for (let i = 0; i < paddingCount; i++) inArray.push(paddingCount);
276
- }
277
-
278
- // 生成轮密钥
279
- const roundKey = new Array(ROUND);
280
- sms4KeyExt(key, roundKey, cryptFlag);
281
-
282
- const outArray = [];
283
- let lastVector = iv;
284
- let restLen = inArray.length;
285
- let point = 0;
286
- while (restLen >= BLOCK) {
287
- const input = inArray.slice(point, point + 16);
288
- const output = new Array(16);
289
-
290
- if (mode === "cbc") {
291
- for (let i = 0; i < BLOCK; i++) {
292
- if (cryptFlag !== DECRYPT) {
293
- // 加密过程在组加密前进行异或
294
- input[i] ^= lastVector[i];
295
- }
296
- }
297
- }
298
-
299
- sms4Crypt(input, output, roundKey);
300
-
301
- for (let i = 0; i < BLOCK; i++) {
302
- if (mode === "cbc") {
303
- if (cryptFlag === DECRYPT) {
304
- // 解密过程在组解密后进行异或
305
- output[i] ^= lastVector[i];
306
- }
307
- }
308
-
309
- outArray[point + i] = output[i];
310
- }
311
-
312
- if (mode === "cbc") {
313
- if (cryptFlag !== DECRYPT) {
314
- // 使用上一次输出作为加密向量
315
- lastVector = output;
316
- } else {
317
- // 使用上一次输入作为解密向量
318
- lastVector = input;
319
- }
320
- }
321
-
322
- restLen -= BLOCK;
323
- point += BLOCK;
324
- }
325
-
326
- // 去除填充,sm4 是 16 个字节一个分组,所以统一走到 pkcs#7
327
- if ((padding === "pkcs#5" || padding === "pkcs#7") && cryptFlag === DECRYPT) {
328
- const len = outArray.length;
329
- const paddingCount = outArray[len - 1];
330
- for (let i = 1; i <= paddingCount; i++) {
331
- if (outArray[len - i] !== paddingCount) throw new Error("padding is invalid");
332
- }
333
- outArray.splice(len - paddingCount, paddingCount);
334
- }
335
-
336
- // 调整输出
337
- if (output !== "array") {
338
- if (cryptFlag !== DECRYPT) {
339
- // 加密,输出转 16 进制串
340
- return ArrayToHex(outArray);
341
- } else {
342
- // 解密,输出转 utf8 串
343
- return arrayToUtf8(outArray);
344
- }
345
- } else {
346
- return outArray;
347
- }
348
- }
349
-
350
- /* 以下是内部实现需要的es模块化导出方法 */
351
- function encrypt(inArray, key, options) {
352
- return sm4(inArray, key, 1, options);
353
- }
354
-
355
- function decrypt(inArray, key, options) {
356
- return sm4(inArray, key, 0, options);
357
- }
358
-
359
- export { decrypt, encrypt };
@@ -1,23 +0,0 @@
1
- import Tea from './tea.mjs';
2
-
3
- /**
4
- * tea 加密
5
- * @param {String} str 字符串
6
- * @param {String} key 秘钥
7
- * @returns {string} 返回加密后的字符串
8
- */
9
- function encrypt(str, key) {
10
- return Tea.encrypt(str, key);
11
- }
12
-
13
- /**
14
- * tea 解密
15
- * @param {String} str 字符串
16
- * @param {String} key 秘钥
17
- * @returns {string} 返回解密后的字符串
18
- */
19
- function decrypt(str, key) {
20
- return Tea.decrypt(str, key);
21
- }
22
-
23
- export { decrypt, encrypt };
@@ -1,211 +0,0 @@
1
- /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2
- /* Block TEA (xxtea) Tiny Encryption Algorithm (c) Chris Veness 2002-2019 */
3
- /* MIT Licence */
4
- /* www.movable-type.co.uk/scripts/tea-block.html */
5
-
6
- /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
7
-
8
- /**
9
- * Tiny Encryption Algorithm. David Wheeler & Roger Needham, Cambridge University Computer Lab.
10
- *
11
- * www.movable-type.co.uk/scripts/tea.pdf - TEA, a Tiny Encryption Algorithm (1994)
12
- * www.movable-type.co.uk/scripts/xtea.pdf - Tea extensions (1997)
13
- * www.movable-type.co.uk/scripts/xxtea.pdf - Correction to xtea (1998)
14
- */
15
- class Tea {
16
- /**
17
- * Encrypts text using Corrected Block TEA (xxtea) algorithm.
18
- *
19
- * @param {string} plaintext - String to be encrypted (multi-byte safe).
20
- * @param {string} password - Password to be used for encryption (1st 16 chars).
21
- * @returns {string} Encrypted text (encoded as base64).
22
- */
23
- static encrypt(plaintext, password) {
24
- plaintext = String(plaintext);
25
- password = String(password);
26
-
27
- if (plaintext.length == 0) return ""; // nothing to encrypt
28
-
29
- // v is n-word data vector; converted to array of longs from UTF-8 string
30
- const v = Tea.strToLongs(Tea.utf8Encode(plaintext));
31
- // k is 4-word key; simply convert first 16 chars of password as key
32
- const k = Tea.strToLongs(Tea.utf8Encode(password).slice(0, 16));
33
-
34
- const cipher = Tea.encode(v, k);
35
-
36
- // convert array of longs to string
37
- const ciphertext = Tea.longsToStr(cipher);
38
-
39
- // convert binary string to base64 ascii for safe transport
40
- const cipherbase64 = Tea.base64Encode(ciphertext);
41
-
42
- return cipherbase64;
43
- }
44
-
45
- /**
46
- * Decrypts text using Corrected Block TEA (xxtea) algorithm.
47
- *
48
- * @param {string} ciphertext - String to be decrypted.
49
- * @param {string} password - Password to be used for decryption (1st 16 chars).
50
- * @returns {string} Decrypted text.
51
- * @throws {Error} Invalid ciphertext
52
- */
53
- static decrypt(ciphertext, password) {
54
- ciphertext = String(ciphertext);
55
- password = String(password);
56
-
57
- if (ciphertext.length == 0) return ""; // nothing to decrypt
58
-
59
- // v is n-word data vector; converted to array of longs from base64 string
60
- const v = Tea.strToLongs(Tea.base64Decode(ciphertext));
61
- // k is 4-word key; simply convert first 16 chars of password as key
62
- const k = Tea.strToLongs(Tea.utf8Encode(password).slice(0, 16));
63
-
64
- const plain = Tea.decode(v, k);
65
-
66
- const plaintext = Tea.longsToStr(plain);
67
-
68
- // strip trailing null chars resulting from filling 4-char blocks:
69
- const plainUnicode = Tea.utf8Decode(plaintext.replace(/\0+$/, ""));
70
-
71
- return plainUnicode;
72
- }
73
-
74
- /**
75
- * XXTEA: encodes array of unsigned 32-bit integers using 128-bit key.
76
- *
77
- * @param {number[]} v - Data vector.
78
- * @param {number[]} k - Key.
79
- * @returns {number[]} Encoded vector.
80
- */
81
- static encode(v, k) {
82
- if (v.length < 2) v[1] = 0; // algorithm doesn't work for n<2 so fudge by adding a null
83
- const n = v.length;
84
- const delta = 0x9e3779b9;
85
- let q = Math.floor(6 + 52 / n);
86
-
87
- let z = v[n - 1],
88
- y = v[0];
89
- let mx,
90
- e,
91
- sum = 0;
92
-
93
- while (q-- > 0) {
94
- // 6 + 52/n operations gives between 6 & 32 mixes on each word
95
- sum += delta;
96
- e = (sum >>> 2) & 3;
97
- for (let p = 0; p < n; p++) {
98
- y = v[(p + 1) % n];
99
- mx = (((z >>> 5) ^ (y << 2)) + ((y >>> 3) ^ (z << 4))) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
100
- z = v[p] += mx;
101
- }
102
- }
103
-
104
- return v;
105
- }
106
-
107
- /**
108
- * XXTEA: decodes array of unsigned 32-bit integers using 128-bit key.
109
- *
110
- * @param {number[]} v - Data vector.
111
- * @param {number[]} k - Key.
112
- * @returns {number[]} Decoded vector.
113
- */
114
- static decode(v, k) {
115
- const n = v.length;
116
- const delta = 0x9e3779b9;
117
- const q = Math.floor(6 + 52 / n);
118
-
119
- let z = v[n - 1],
120
- y = v[0];
121
- let mx,
122
- e,
123
- sum = q * delta;
124
-
125
- while (sum != 0) {
126
- e = (sum >>> 2) & 3;
127
- for (let p = n - 1; p >= 0; p--) {
128
- z = v[p > 0 ? p - 1 : n - 1];
129
- mx = (((z >>> 5) ^ (y << 2)) + ((y >>> 3) ^ (z << 4))) ^ ((sum ^ y) + (k[(p & 3) ^ e] ^ z));
130
- y = v[p] -= mx;
131
- }
132
- sum -= delta;
133
- }
134
-
135
- return v;
136
- }
137
-
138
- /**
139
- * Converts string to array of longs (each containing 4 chars).
140
- * @private
141
- */
142
- static strToLongs(s) {
143
- // note chars must be within ISO-8859-1 (Unicode code-point <= U+00FF) to fit 4/long
144
- const l = new Array(Math.ceil(s.length / 4));
145
- for (let i = 0; i < l.length; i++) {
146
- // note little-endian encoding - endianness is irrelevant as long as it matches longsToStr()
147
- l[i] =
148
- s.charCodeAt(i * 4) +
149
- (s.charCodeAt(i * 4 + 1) << 8) +
150
- (s.charCodeAt(i * 4 + 2) << 16) +
151
- (s.charCodeAt(i * 4 + 3) << 24);
152
- } // note running off the end of the string generates nulls since bitwise operators treat NaN as 0
153
- return l;
154
- }
155
-
156
- /**
157
- * Converts array of longs to string.
158
- * @private
159
- */
160
- static longsToStr(l) {
161
- let str = "";
162
- for (let i = 0; i < l.length; i++) {
163
- str += String.fromCharCode(l[i] & 0xff, (l[i] >>> 8) & 0xff, (l[i] >>> 16) & 0xff, (l[i] >>> 24) & 0xff);
164
- }
165
- return str;
166
- }
167
-
168
- /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
169
-
170
- /**
171
- * Encodes multi-byte string to utf8 - monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
172
- */
173
- static utf8Encode(str) {
174
- return unescape(encodeURIComponent(str));
175
- }
176
-
177
- /**
178
- * Decodes utf8 string to multi-byte
179
- */
180
- static utf8Decode(utf8Str) {
181
- try {
182
- return decodeURIComponent(escape(utf8Str));
183
- } catch (e) {
184
- return utf8Str; // invalid UTF-8? return as-is
185
- }
186
- }
187
-
188
- /**
189
- * Encodes base64 - developer.mozilla.org/en-US/docs/Web/API/window.btoa, nodejs.org/api/buffer.html
190
- */
191
- static base64Encode(str) {
192
- if (typeof btoa != "undefined") return btoa(str); // browser
193
- if (typeof Buffer != "undefined") return new Buffer(str, "binary").toString("base64"); // Node.js
194
- throw new Error("No Base64 Encode");
195
- }
196
-
197
- /**
198
- * Decodes base64
199
- */
200
- static base64Decode(b64Str) {
201
- if (typeof atob == "undefined" && typeof Buffer == "undefined") throw new Error("No base64 decode");
202
- try {
203
- if (typeof atob != "undefined") return atob(b64Str); // browser
204
- if (typeof Buffer != "undefined") return new Buffer(b64Str, "base64").toString("binary"); // Node.js
205
- } catch (e) {
206
- throw new Error("Invalid ciphertext");
207
- }
208
- }
209
- }
210
-
211
- export { Tea as default };