@ivujs/i-utils 2.0.0 → 2.1.0

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