@ivujs/i-utils 1.1.14 → 1.1.16

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