@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,70 @@
1
+ 'use strict';
2
+
3
+ var index = require('../validate/index.cjs');
4
+
5
+ /**
6
+ * 浏览器是否支持 Cookie
7
+ * @returns {Boolean} 返回true和false
8
+ */
9
+ function isSupportCookie() {
10
+ return window.navigator.cookieEnabled;
11
+ }
12
+ /* cookie存储 */
13
+ /**
14
+ * 通过name获取cookie
15
+ * @param {String} name 参数name
16
+ * @returns {String} 返回获取的值
17
+ */
18
+ function getCookie(name) {
19
+ if (typeof document == "undefined")
20
+ return "";
21
+ // cookie中每个分号后面是有个空格的,需要替换掉
22
+ let arr = document.cookie ? document.cookie.replace(/\s/g, "").split(";") : [];
23
+ for (let i in arr) {
24
+ let tempArr = arr[i].split("=");
25
+ if (tempArr[0] === name) {
26
+ return decodeURIComponent(tempArr[1]);
27
+ }
28
+ }
29
+ return "";
30
+ }
31
+ /**
32
+ * 通过name设置cookie
33
+ * 注:timestamp参数不填,则默认为session级别,浏览器关闭即cookie过期
34
+ * @param {String} name 参数name
35
+ * @param {String} value 设置的value
36
+ * @param {Number} timestamp 过期的时间戳值,默认为一天,设置一天过期则为:24*60*60*1000
37
+ */
38
+ function setCookie(name, value, timestamp = 24 * 60 * 60 * 1000) {
39
+ if (typeof document == "undefined")
40
+ return;
41
+ document.cookie = name + "=" + value + ";expires=" + new Date(Date.now() + timestamp);
42
+ }
43
+ /**
44
+ * 通过name删除cookie
45
+ * @param {String} name 参数name
46
+ */
47
+ function removeCookie(name) {
48
+ if (index.isEmpty(name))
49
+ return;
50
+ setCookie(name, "", -1);
51
+ }
52
+ /**
53
+ * 清空当前站点域名的cookie
54
+ * @param {String} domain 域名地址,默认是当前站点域名
55
+ */
56
+ function clearCookie(domain = document.domain) {
57
+ let names = document.cookie.match(/[^ =;]+(?==)/g);
58
+ if (names) {
59
+ for (let i = names.length; i--;) {
60
+ document.cookie =
61
+ names[i] + "=0;path=/;" + domain ? "domain=" + domain + ";" : "" + "expires=" + new Date(0).toUTCString();
62
+ }
63
+ }
64
+ }
65
+
66
+ exports.clearCookie = clearCookie;
67
+ exports.getCookie = getCookie;
68
+ exports.isSupportCookie = isSupportCookie;
69
+ exports.removeCookie = removeCookie;
70
+ exports.setCookie = setCookie;
@@ -0,0 +1,480 @@
1
+ 'use strict';
2
+
3
+ var index = require('../base64/index.cjs');
4
+
5
+ const { encode, decode, utf8Decode, utf8Encode } = index;
6
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
7
+ /* AES implementation in JavaScript (c) Chris Veness 2005-2019 */
8
+ /* MIT Licence */
9
+ /* www.movable-type.co.uk/scripts/aes.html */
10
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
11
+ /**
12
+ * AES (Rijndael cipher) encryption routines reference implementation,
13
+ *
14
+ * This is an annotated direct implementation of FIPS 197, without any optimisations. It is
15
+ * intended to aid understanding of the algorithm rather than for production use.
16
+ *
17
+ * While it could be used where performance is not critical, I would recommend using the ‘Web
18
+ * Cryptography API’ (developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt) for the browser,
19
+ * or the ‘crypto’ library (nodejs.org/api/crypto.html#crypto_class_cipher) in Node.js.
20
+ *
21
+ * See csrc.nist.gov/publications/fips/fips197/fips-197.pdf
22
+ */
23
+ class Aes {
24
+ /**
25
+ * AES Cipher function: encrypt 'input' state with Rijndael algorithm [§5.1];
26
+ * applies Nr rounds (10/12/14) using key schedule w for 'add round key' stage.
27
+ *
28
+ * @param {number[]} input - 16-byte (128-bit) input state array.
29
+ * @param {number[][]} w - Key schedule as 2D byte-array (Nr+1 × Nb bytes).
30
+ * @returns {number[]} Encrypted output state array.
31
+ */
32
+ static cipher(input, w) {
33
+ const Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES)
34
+ const Nr = w.length / Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys
35
+ let state = [[], [], [], []]; // initialise 4×Nb byte-array 'state' with input [§3.4]
36
+ for (let i = 0; i < 4 * Nb; i++)
37
+ state[i % 4][Math.floor(i / 4)] = input[i];
38
+ state = Aes.addRoundKey(state, w, 0, Nb);
39
+ for (let round = 1; round < Nr; round++) {
40
+ state = Aes.subBytes(state, Nb);
41
+ state = Aes.shiftRows(state, Nb);
42
+ state = Aes.mixColumns(state, Nb);
43
+ state = Aes.addRoundKey(state, w, round, Nb);
44
+ }
45
+ state = Aes.subBytes(state, Nb);
46
+ state = Aes.shiftRows(state, Nb);
47
+ state = Aes.addRoundKey(state, w, Nr, Nb);
48
+ const output = new Array(4 * Nb); // convert state to 1-d array before returning [§3.4]
49
+ for (let i = 0; i < 4 * Nb; i++)
50
+ output[i] = state[i % 4][Math.floor(i / 4)];
51
+ return output;
52
+ }
53
+ /**
54
+ * Perform key expansion to generate a key schedule from a cipher key [§5.2].
55
+ *
56
+ * @param {number[]} key - Cipher key as 16/24/32-byte array.
57
+ * @returns {number[][]} Expanded key schedule as 2D byte-array (Nr+1 × Nb bytes).
58
+ */
59
+ static keyExpansion(key) {
60
+ const Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES)
61
+ const Nk = key.length / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys
62
+ const Nr = Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys
63
+ const w = new Array(Nb * (Nr + 1));
64
+ let temp = new Array(4);
65
+ // initialise first Nk words of expanded key with cipher key
66
+ for (let i = 0; i < Nk; i++) {
67
+ const r = [key[4 * i], key[4 * i + 1], key[4 * i + 2], key[4 * i + 3]];
68
+ w[i] = r;
69
+ }
70
+ // expand the key into the remainder of the schedule
71
+ for (let i = Nk; i < Nb * (Nr + 1); i++) {
72
+ w[i] = new Array(4);
73
+ for (let t = 0; t < 4; t++)
74
+ temp[t] = w[i - 1][t];
75
+ // each Nk'th word has extra transformation
76
+ if (i % Nk == 0) {
77
+ temp = Aes.subWord(Aes.rotWord(temp));
78
+ for (let t = 0; t < 4; t++)
79
+ temp[t] ^= Aes.rCon[i / Nk][t];
80
+ }
81
+ // 256-bit key has subWord applied every 4th word
82
+ else if (Nk > 6 && i % Nk == 4) {
83
+ temp = Aes.subWord(temp);
84
+ }
85
+ // xor w[i] with w[i-1] and w[i-Nk]
86
+ for (let t = 0; t < 4; t++)
87
+ w[i][t] = w[i - Nk][t] ^ temp[t];
88
+ }
89
+ return w;
90
+ }
91
+ /**
92
+ * Apply SBox to state S [§5.1.1].
93
+ *
94
+ * @private
95
+ */
96
+ static subBytes(s, Nb) {
97
+ for (let r = 0; r < 4; r++) {
98
+ for (let c = 0; c < Nb; c++)
99
+ s[r][c] = Aes.sBox[s[r][c]];
100
+ }
101
+ return s;
102
+ }
103
+ /**
104
+ * Shift row r of state S left by r bytes [§5.1.2].
105
+ *
106
+ * @private
107
+ */
108
+ static shiftRows(s, Nb) {
109
+ const t = new Array(4);
110
+ for (let r = 1; r < 4; r++) {
111
+ for (let c = 0; c < 4; c++)
112
+ t[c] = s[r][(c + r) % Nb]; // shift into temp copy
113
+ for (let c = 0; c < 4; c++)
114
+ s[r][c] = t[c]; // and copy back
115
+ } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES):
116
+ return s; // see asmaes.sourceforge.net/rijndael/rijndaelImplementation.pdf
117
+ }
118
+ /**
119
+ * Combine bytes of each col of state S [§5.1.3].
120
+ *
121
+ * @private
122
+ */
123
+ static mixColumns(s, Nb) {
124
+ for (let c = 0; c < Nb; c++) {
125
+ const a = new Array(Nb); // 'a' is a copy of the current column from 's'
126
+ const b = new Array(Nb); // 'b' is a•{02} in GF(2^8)
127
+ for (let r = 0; r < 4; r++) {
128
+ a[r] = s[r][c];
129
+ b[r] = s[r][c] & 0x80 ? (s[r][c] << 1) ^ 0x011b : s[r][c] << 1;
130
+ }
131
+ // a[n] ^ b[n] is a•{03} in GF(2^8)
132
+ s[0][c] = b[0] ^ a[1] ^ b[1] ^ a[2] ^ a[3]; // {02}•a0 + {03}•a1 + a2 + a3
133
+ s[1][c] = a[0] ^ b[1] ^ a[2] ^ b[2] ^ a[3]; // a0 • {02}•a1 + {03}•a2 + a3
134
+ s[2][c] = a[0] ^ a[1] ^ b[2] ^ a[3] ^ b[3]; // a0 + a1 + {02}•a2 + {03}•a3
135
+ s[3][c] = a[0] ^ b[0] ^ a[1] ^ a[2] ^ b[3]; // {03}•a0 + a1 + a2 + {02}•a3
136
+ }
137
+ return s;
138
+ }
139
+ /**
140
+ * Xor Round Key into state S [§5.1.4].
141
+ *
142
+ * @private
143
+ */
144
+ static addRoundKey(state, w, rnd, Nb) {
145
+ for (let r = 0; r < 4; r++) {
146
+ for (let c = 0; c < Nb; c++)
147
+ state[r][c] ^= w[rnd * 4 + c][r];
148
+ }
149
+ return state;
150
+ }
151
+ /**
152
+ * Apply SBox to 4-byte word w.
153
+ *
154
+ * @private
155
+ */
156
+ static subWord(w) {
157
+ for (let i = 0; i < 4; i++)
158
+ w[i] = Aes.sBox[w[i]];
159
+ return w;
160
+ }
161
+ /**
162
+ * Rotate 4-byte word w left by one byte.
163
+ *
164
+ * @private
165
+ */
166
+ static rotWord(w) {
167
+ const tmp = w[0];
168
+ for (let i = 0; i < 3; i++)
169
+ w[i] = w[i + 1];
170
+ w[3] = tmp;
171
+ return w;
172
+ }
173
+ }
174
+ // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [§5.1.1]
175
+ Aes.sBox = [
176
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9,
177
+ 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f,
178
+ 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07,
179
+ 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3,
180
+ 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58,
181
+ 0xcf, 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3,
182
+ 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec, 0x5f,
183
+ 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
184
+ 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac,
185
+ 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a,
186
+ 0xae, 0x08, 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 0x70,
187
+ 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
188
+ 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42,
189
+ 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
190
+ ];
191
+ // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [§5.2]
192
+ Aes.rCon = [
193
+ [0x00, 0x00, 0x00, 0x00],
194
+ [0x01, 0x00, 0x00, 0x00],
195
+ [0x02, 0x00, 0x00, 0x00],
196
+ [0x04, 0x00, 0x00, 0x00],
197
+ [0x08, 0x00, 0x00, 0x00],
198
+ [0x10, 0x00, 0x00, 0x00],
199
+ [0x20, 0x00, 0x00, 0x00],
200
+ [0x40, 0x00, 0x00, 0x00],
201
+ [0x80, 0x00, 0x00, 0x00],
202
+ [0x1b, 0x00, 0x00, 0x00],
203
+ [0x36, 0x00, 0x00, 0x00],
204
+ ];
205
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
206
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
207
+ /* AES counter-mode (CTR) implementation in JavaScript (c) Chris Veness 2005-2019 */
208
+ /* MIT Licence */
209
+ /* www.movable-type.co.uk/scripts/aes.html */
210
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
211
+ /* global WorkerGlobalScope */
212
+ /**
213
+ * AesCtr: Counter-mode (CTR) wrapper for AES.
214
+ *
215
+ * This encrypts a Unicode string to produces a base64 ciphertext using 128/192/256-bit AES,
216
+ * and the converse to decrypt an encrypted ciphertext.
217
+ *
218
+ * See csrc.nist.gov/publications/detail/sp/800-38a/final
219
+ */
220
+ class AesCtr extends Aes {
221
+ /**
222
+ * Encrypt a text using AES encryption in Counter mode of operation.
223
+ *
224
+ * Unicode multi-byte character safe.
225
+ *
226
+ * @param {string} plaintext - Source text to be encrypted.
227
+ * @param {string} password - The password to use to generate a key for encryption.
228
+ * @param {number} nBits - Number of bits to be used in the key; 128 / 192 / 256.
229
+ * @returns {string} Encrypted text, base-64 encoded.
230
+ *
231
+ * @example
232
+ * const encr = AesCtr.encrypt('big secret', 'pāşšŵōřđ', 256); // 'lwGl66VVwVObKIr6of8HVqJr'
233
+ */
234
+ static encrypt(plaintext, password, nBits) {
235
+ if (![128, 192, 256].includes(nBits))
236
+ throw new Error("Key size is not 128 / 192 / 256");
237
+ plaintext = AesCtr.utf8Encode(String(plaintext));
238
+ password = AesCtr.utf8Encode(String(password));
239
+ // use AES itself to encrypt password to get cipher key (using plain password as source for key
240
+ // expansion) to give us well encrypted key (in real use hashed password could be used for key)
241
+ const nBytes = nBits / 8; // no bytes in key (16/24/32)
242
+ const pwBytes = new Array(nBytes);
243
+ for (let i = 0; i < nBytes; i++) {
244
+ // use 1st 16/24/32 chars of password for key
245
+ pwBytes[i] = i < password.length ? password.charCodeAt(i) : 0;
246
+ }
247
+ let key = Aes.cipher(pwBytes, Aes.keyExpansion(pwBytes)); // gives us 16-byte key
248
+ key = key.concat(key.slice(0, nBytes - 16)); // expand key to 16/24/32 bytes long
249
+ // initialise 1st 8 bytes of counter block with nonce (NIST SP 800-38A §B.2): [0-1] = millisec,
250
+ // [2-3] = random, [4-7] = seconds, together giving full sub-millisec uniqueness up to Feb 2106
251
+ const timestamp = new Date().getTime(); // milliseconds since 1-Jan-1970
252
+ const nonceMs = timestamp % 1000;
253
+ const nonceSec = Math.floor(timestamp / 1000);
254
+ const nonceRnd = Math.floor(Math.random() * 0xffff);
255
+ // for debugging: const [ nonceMs, nonceSec, nonceRnd ] = [ 0, 0, 0 ];
256
+ const counterBlock = [
257
+ // 16-byte array; blocksize is fixed at 16 for AES
258
+ nonceMs & 0xff,
259
+ (nonceMs >>> 8) & 0xff,
260
+ nonceRnd & 0xff,
261
+ (nonceRnd >>> 8) & 0xff,
262
+ nonceSec & 0xff,
263
+ (nonceSec >>> 8) & 0xff,
264
+ (nonceSec >>> 16) & 0xff,
265
+ (nonceSec >>> 24) & 0xff,
266
+ 0,
267
+ 0,
268
+ 0,
269
+ 0,
270
+ 0,
271
+ 0,
272
+ 0,
273
+ 0,
274
+ ];
275
+ // and convert nonce to a string to go on the front of the ciphertext
276
+ const nonceStr = counterBlock
277
+ .slice(0, 8)
278
+ .map((i) => String.fromCharCode(i))
279
+ .join("");
280
+ // convert (utf-8) plaintext to byte array
281
+ const plaintextBytes = plaintext.split("").map((ch) => ch.charCodeAt(0));
282
+ // ------------ perform encryption ------------
283
+ const ciphertextBytes = AesCtr.nistEncryption(plaintextBytes, key, counterBlock);
284
+ // convert byte array to (utf-8) ciphertext string
285
+ const ciphertextUtf8 = ciphertextBytes.map((i) => String.fromCharCode(i)).join("");
286
+ // base-64 encode ciphertext
287
+ const ciphertextB64 = AesCtr.base64Encode(nonceStr + ciphertextUtf8);
288
+ return ciphertextB64;
289
+ }
290
+ /**
291
+ * NIST SP 800-38A sets out recommendations for block cipher modes of operation in terms of byte
292
+ * operations. This implements the §6.5 Counter Mode (CTR).
293
+ *
294
+ * Oⱼ = CIPHₖ(Tⱼ) for j = 1, 2 … n
295
+ * Cⱼ = Pⱼ ⊕ Oⱼ for j = 1, 2 … n-1
296
+ * C*ₙ = P* ⊕ MSBᵤ(Oₙ) final (partial?) block
297
+ * where CIPHₖ is the forward cipher function, O output blocks, P plaintext blocks, C
298
+ * ciphertext blocks
299
+ *
300
+ * @param {number[]} plaintext - Plaintext to be encrypted, as byte array.
301
+ * @param {number[]} key - Key to be used to encrypt plaintext.
302
+ * @param {number[]} counterBlock - Initial 16-byte CTR counter block (with nonce & 0 counter).
303
+ * @returns {number[]} Ciphertext as byte array.
304
+ *
305
+ * @private
306
+ */
307
+ static nistEncryption(plaintext, key, counterBlock) {
308
+ const blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES
309
+ // generate key schedule - an expansion of the key into distinct Key Rounds for each round
310
+ const keySchedule = Aes.keyExpansion(key);
311
+ const blockCount = Math.ceil(plaintext.length / blockSize);
312
+ const ciphertext = new Array(plaintext.length);
313
+ for (let b = 0; b < blockCount; b++) {
314
+ // ---- encrypt counter block; Oⱼ = CIPHₖ(Tⱼ) ----
315
+ const cipherCntr = Aes.cipher(counterBlock, keySchedule);
316
+ // block size is reduced on final block
317
+ const blockLength = b < blockCount - 1 ? blockSize : ((plaintext.length - 1) % blockSize) + 1;
318
+ // ---- xor plaintext with ciphered counter byte-by-byte; Cⱼ = Pⱼ ⊕ Oⱼ ----
319
+ for (let i = 0; i < blockLength; i++) {
320
+ ciphertext[b * blockSize + i] = cipherCntr[i] ^ plaintext[b * blockSize + i];
321
+ }
322
+ // increment counter block (counter in 2nd 8 bytes of counter block, big-endian)
323
+ counterBlock[blockSize - 1]++;
324
+ // and propagate carry digits
325
+ for (let i = blockSize - 1; i >= 8; i--) {
326
+ counterBlock[i - 1] += counterBlock[i] >> 8;
327
+ counterBlock[i] &= 0xff;
328
+ }
329
+ // if within web worker, announce progress every 1000 blocks (roughly every 50ms)
330
+ if (typeof WorkerGlobalScope != "undefined" && self instanceof WorkerGlobalScope) {
331
+ if (b % 1000 == 0)
332
+ self.postMessage({ progress: b / blockCount });
333
+ }
334
+ }
335
+ return ciphertext;
336
+ }
337
+ /**
338
+ * Decrypt a text encrypted by AES in counter mode of operation.
339
+ *
340
+ * @param {string} ciphertext - Cipher text to be decrypted.
341
+ * @param {string} password - Password to use to generate a key for decryption.
342
+ * @param {number} nBits - Number of bits to be used in the key; 128 / 192 / 256.
343
+ * @returns {string} Decrypted text
344
+ *
345
+ * @example
346
+ * const decr = AesCtr.decrypt('lwGl66VVwVObKIr6of8HVqJr', 'pāşšŵōřđ', 256); // 'big secret'
347
+ */
348
+ static decrypt(ciphertext, password, nBits) {
349
+ if (![128, 192, 256].includes(nBits))
350
+ throw new Error("Key size is not 128 / 192 / 256");
351
+ ciphertext = AesCtr.base64Decode(String(ciphertext));
352
+ password = AesCtr.utf8Encode(String(password));
353
+ // use AES to encrypt password (mirroring encrypt routine)
354
+ const nBytes = nBits / 8; // no bytes in key
355
+ const pwBytes = new Array(nBytes);
356
+ for (let i = 0; i < nBytes; i++) {
357
+ // use 1st nBytes chars of password for key
358
+ pwBytes[i] = i < password.length ? password.charCodeAt(i) : 0;
359
+ }
360
+ let key = Aes.cipher(pwBytes, Aes.keyExpansion(pwBytes));
361
+ key = key.concat(key.slice(0, nBytes - 16)); // expand key to 16/24/32 bytes long
362
+ // recover nonce from 1st 8 bytes of ciphertext into 1st 8 bytes of counter block
363
+ const counterBlock = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
364
+ for (let i = 0; i < 8; i++)
365
+ counterBlock[i] = ciphertext.charCodeAt(i);
366
+ // convert ciphertext to byte array (skipping past initial 8 bytes)
367
+ const ciphertextBytes = new Array(ciphertext.length - 8);
368
+ for (let i = 8; i < ciphertext.length; i++)
369
+ ciphertextBytes[i - 8] = ciphertext.charCodeAt(i);
370
+ // ------------ perform decryption ------------
371
+ const plaintextBytes = AesCtr.nistDecryption(ciphertextBytes, key, counterBlock);
372
+ // convert byte array to (utf-8) plaintext string
373
+ const plaintextUtf8 = plaintextBytes.map((i) => String.fromCharCode(i)).join("");
374
+ // decode from UTF8 back to Unicode multi-byte chars
375
+ const plaintext = AesCtr.utf8Decode(plaintextUtf8);
376
+ return plaintext;
377
+ }
378
+ /**
379
+ * NIST SP 800-38A sets out recommendations for block cipher modes of operation in terms of byte
380
+ * operations. This implements the §6.5 Counter Mode (CTR).
381
+ *
382
+ * Oⱼ = CIPHₖ(Tⱼ) for j = 1, 2 … n
383
+ * Pⱼ = Cⱼ ⊕ Oⱼ for j = 1, 2 … n-1
384
+ * P*ₙ = C* ⊕ MSBᵤ(Oₙ) final (partial?) block
385
+ * where CIPHₖ is the forward cipher function, O output blocks, C ciphertext blocks, P
386
+ * plaintext blocks
387
+ *
388
+ * @param {number[]} ciphertext - Ciphertext to be decrypted, as byte array.
389
+ * @param {number[]} key - Key to be used to decrypt ciphertext.
390
+ * @param {number[]} counterBlock - Initial 16-byte CTR counter block (with nonce & 0 counter).
391
+ * @returns {number[]} Plaintext as byte array.
392
+ *
393
+ * @private
394
+ */
395
+ static nistDecryption(ciphertext, key, counterBlock) {
396
+ const blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES
397
+ // generate key schedule - an expansion of the key into distinct Key Rounds for each round
398
+ const keySchedule = Aes.keyExpansion(key);
399
+ const blockCount = Math.ceil(ciphertext.length / blockSize);
400
+ const plaintext = new Array(ciphertext.length);
401
+ for (let b = 0; b < blockCount; b++) {
402
+ // ---- decrypt counter block; Oⱼ = CIPHₖ(Tⱼ) ----
403
+ const cipherCntr = Aes.cipher(counterBlock, keySchedule);
404
+ // block size is reduced on final block
405
+ const blockLength = b < blockCount - 1 ? blockSize : ((ciphertext.length - 1) % blockSize) + 1;
406
+ // ---- xor ciphertext with ciphered counter byte-by-byte; Pⱼ = Cⱼ ⊕ Oⱼ ----
407
+ for (let i = 0; i < blockLength; i++) {
408
+ plaintext[b * blockSize + i] = cipherCntr[i] ^ ciphertext[b * blockSize + i];
409
+ }
410
+ // increment counter block (counter in 2nd 8 bytes of counter block, big-endian)
411
+ counterBlock[blockSize - 1]++;
412
+ // and propagate carry digits
413
+ for (let i = blockSize - 1; i >= 8; i--) {
414
+ counterBlock[i - 1] += counterBlock[i] >> 8;
415
+ counterBlock[i] &= 0xff;
416
+ }
417
+ // if within web worker, announce progress every 1000 blocks (roughly every 50ms)
418
+ if (typeof WorkerGlobalScope != "undefined" && self instanceof WorkerGlobalScope) {
419
+ if (b % 1000 == 0)
420
+ self.postMessage({ progress: b / blockCount });
421
+ }
422
+ }
423
+ return plaintext;
424
+ }
425
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
426
+ /**
427
+ * Encodes multi-byte string to utf8.
428
+ *
429
+ * Note utf8Encode is an identity function with 7-bit ascii strings, but not with 8-bit strings;
430
+ * utf8Encode('x') = 'x', but utf8Encode('ça') = 'ça', and utf8Encode('ça') = 'ça'.
431
+ */
432
+ static utf8Encode(str) {
433
+ // 注释第三方js的代码
434
+ // try {
435
+ // return new TextEncoder().encode(str, "utf-8").reduce((prev, curr) => prev + String.fromCharCode(curr), "");
436
+ // } catch (e) {
437
+ // // no TextEncoder available?
438
+ // return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
439
+ // }
440
+ return utf8Encode(str);
441
+ }
442
+ /**
443
+ * Decodes utf8 string to multi-byte.
444
+ */
445
+ static utf8Decode(str) {
446
+ // 注释第三方js的代码
447
+ // try {
448
+ // return new TextEncoder().decode(str, "utf-8").reduce((prev, curr) => prev + String.fromCharCode(curr), "");
449
+ // } catch (e) {
450
+ // // no TextEncoder available?
451
+ // return decodeURIComponent(escape(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
452
+ // }
453
+ return utf8Decode(str);
454
+ }
455
+ /*
456
+ * Encodes string as base-64.
457
+ *
458
+ * - developer.mozilla.org/en-US/docs/Web/API/window.btoa, nodejs.org/api/buffer.html
459
+ * - note: btoa & Buffer/binary work on single-byte Unicode (C0/C1), so ok for utf8 strings, not for general Unicode...
460
+ * - note: if btoa()/atob() are not available (eg IE9-), try github.com/davidchambers/Base64.js
461
+ */
462
+ static base64Encode(str) {
463
+ // 注释第三方js的代码
464
+ // if (typeof btoa != "undefined") return btoa(str); // browser
465
+ // if (typeof Buffer != "undefined") return new Buffer(str, "binary").toString("base64"); // Node.js
466
+ // throw new Error("No Base64 Encode");
467
+ return encode(str);
468
+ }
469
+ /*
470
+ * Decodes base-64 encoded string.
471
+ */
472
+ static base64Decode(str) {
473
+ // if (typeof atob != "undefined") return atob(str); // browser
474
+ // if (typeof Buffer != "undefined") return new Buffer(str, "base64").toString("binary"); // Node.js
475
+ // throw new Error("No Base64 Decode");
476
+ return decode(str);
477
+ }
478
+ }
479
+
480
+ module.exports = AesCtr;
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ var aes = require('./aes.cjs');
4
+
5
+ /**
6
+ * aes 加密
7
+ * @param {String} str 字符串
8
+ * @param {String} key 秘钥
9
+ * @param {Number} bits 密钥长度位数,支持128、192和256,默认256
10
+ * @returns {string} 返回加密后的字符串
11
+ */
12
+ function encrypt(str, key, bits = 256) {
13
+ return aes.encrypt(str, key, bits);
14
+ }
15
+ /**
16
+ * aes 解密
17
+ * @param {String} str 字符串
18
+ * @param {String} key 秘钥
19
+ * @param {Number} bits 密钥长度位数,支持128、192和256,默认256
20
+ * @returns {string} 返回解密后的字符串
21
+ */
22
+ function decrypt(str, key, bits) {
23
+ return aes.decrypt(str, key, bits);
24
+ }
25
+
26
+ exports.decrypt = decrypt;
27
+ exports.encrypt = encrypt;