@ivujs/i-utils 1.1.1 → 1.1.3

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