@ivujs/i-utils 1.1.1 → 1.1.2

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