@helloao/cli 0.0.1

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 (110) hide show
  1. package/actions.d.ts +57 -0
  2. package/actions.js +262 -0
  3. package/cli.d.ts +2 -0
  4. package/cli.js +139 -0
  5. package/db.d.ts +110 -0
  6. package/db.js +754 -0
  7. package/downloads.d.ts +2 -0
  8. package/downloads.js +12 -0
  9. package/files.d.ts +56 -0
  10. package/files.js +232 -0
  11. package/index.d.ts +8 -0
  12. package/index.js +38 -0
  13. package/migrations/20240420231455_initial/migration.sql +66 -0
  14. package/migrations/20240623183848_add_book_order/migration.sql +26 -0
  15. package/migrations/20240629194121_add_chapter_links/migration.sql +45 -0
  16. package/migrations/20240629194513_add_chapter_content/migration.sql +30 -0
  17. package/migrations/20240705221833_remove_unused_columns/migration.sql +27 -0
  18. package/migrations/20240711173108_add_chapter_audio/migration.sql +13 -0
  19. package/migrations/20240724212651_add_hashing/migration.sql +25 -0
  20. package/node_modules/@zip.js/zip.js/LICENSE +28 -0
  21. package/node_modules/@zip.js/zip.js/README.md +173 -0
  22. package/node_modules/@zip.js/zip.js/deno.json +8 -0
  23. package/node_modules/@zip.js/zip.js/dist/README.md +28 -0
  24. package/node_modules/@zip.js/zip.js/dist/z-worker-fflate.js +1 -0
  25. package/node_modules/@zip.js/zip.js/dist/z-worker-pako.js +1 -0
  26. package/node_modules/@zip.js/zip.js/dist/z-worker.js +1 -0
  27. package/node_modules/@zip.js/zip.js/dist/zip-fs-full.js +11935 -0
  28. package/node_modules/@zip.js/zip.js/dist/zip-fs-full.min.js +1 -0
  29. package/node_modules/@zip.js/zip.js/dist/zip-fs.js +6079 -0
  30. package/node_modules/@zip.js/zip.js/dist/zip-fs.min.js +1 -0
  31. package/node_modules/@zip.js/zip.js/dist/zip-full.js +9463 -0
  32. package/node_modules/@zip.js/zip.js/dist/zip-full.min.js +1 -0
  33. package/node_modules/@zip.js/zip.js/dist/zip-no-worker-deflate.min.js +1 -0
  34. package/node_modules/@zip.js/zip.js/dist/zip-no-worker-inflate.min.js +1 -0
  35. package/node_modules/@zip.js/zip.js/dist/zip-no-worker.min.js +1 -0
  36. package/node_modules/@zip.js/zip.js/dist/zip.js +5240 -0
  37. package/node_modules/@zip.js/zip.js/dist/zip.min.js +1 -0
  38. package/node_modules/@zip.js/zip.js/index-fflate.js +82 -0
  39. package/node_modules/@zip.js/zip.js/index.cjs +11927 -0
  40. package/node_modules/@zip.js/zip.js/index.d.ts +2048 -0
  41. package/node_modules/@zip.js/zip.js/index.js +87 -0
  42. package/node_modules/@zip.js/zip.js/index.min.js +1 -0
  43. package/node_modules/@zip.js/zip.js/lib/core/codec-pool.js +127 -0
  44. package/node_modules/@zip.js/zip.js/lib/core/codec-worker.js +348 -0
  45. package/node_modules/@zip.js/zip.js/lib/core/configuration.js +127 -0
  46. package/node_modules/@zip.js/zip.js/lib/core/constants.js +114 -0
  47. package/node_modules/@zip.js/zip.js/lib/core/io.js +749 -0
  48. package/node_modules/@zip.js/zip.js/lib/core/streams/aes-crypto-stream.js +326 -0
  49. package/node_modules/@zip.js/zip.js/lib/core/streams/codec-stream.js +154 -0
  50. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/crc32.js +63 -0
  51. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/deflate.js +2063 -0
  52. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/inflate.js +2167 -0
  53. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/sjcl.js +827 -0
  54. package/node_modules/@zip.js/zip.js/lib/core/streams/common-crypto.js +55 -0
  55. package/node_modules/@zip.js/zip.js/lib/core/streams/crc32-stream.js +56 -0
  56. package/node_modules/@zip.js/zip.js/lib/core/streams/stream-adapter.js +55 -0
  57. package/node_modules/@zip.js/zip.js/lib/core/streams/zip-crypto-stream.js +162 -0
  58. package/node_modules/@zip.js/zip.js/lib/core/streams/zip-entry-stream.js +165 -0
  59. package/node_modules/@zip.js/zip.js/lib/core/util/cp437-decode.js +48 -0
  60. package/node_modules/@zip.js/zip.js/lib/core/util/decode-text.js +43 -0
  61. package/node_modules/@zip.js/zip.js/lib/core/util/default-mime-type.js +38 -0
  62. package/node_modules/@zip.js/zip.js/lib/core/util/encode-text.js +48 -0
  63. package/node_modules/@zip.js/zip.js/lib/core/util/mime-type.js +1639 -0
  64. package/node_modules/@zip.js/zip.js/lib/core/util/stream-codec-shim.js +91 -0
  65. package/node_modules/@zip.js/zip.js/lib/core/z-worker-core.js +176 -0
  66. package/node_modules/@zip.js/zip.js/lib/core/zip-entry.js +86 -0
  67. package/node_modules/@zip.js/zip.js/lib/core/zip-fs-core.js +865 -0
  68. package/node_modules/@zip.js/zip.js/lib/core/zip-reader.js +757 -0
  69. package/node_modules/@zip.js/zip.js/lib/core/zip-writer.js +1186 -0
  70. package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-fflate.js +40 -0
  71. package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-pako.js +39 -0
  72. package/node_modules/@zip.js/zip.js/lib/z-worker-fflate.js +40 -0
  73. package/node_modules/@zip.js/zip.js/lib/z-worker-inline-template.js +42 -0
  74. package/node_modules/@zip.js/zip.js/lib/z-worker-inline.js +1 -0
  75. package/node_modules/@zip.js/zip.js/lib/z-worker.js +38 -0
  76. package/node_modules/@zip.js/zip.js/lib/zip-data-uri.js +53 -0
  77. package/node_modules/@zip.js/zip.js/lib/zip-fflate-shim.js +37 -0
  78. package/node_modules/@zip.js/zip.js/lib/zip-fs.js +53 -0
  79. package/node_modules/@zip.js/zip.js/lib/zip-full-fflate.js +53 -0
  80. package/node_modules/@zip.js/zip.js/lib/zip-full.js +54 -0
  81. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-deflate.js +42 -0
  82. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-deflate.js +42 -0
  83. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-inflate.js +42 -0
  84. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate.js +43 -0
  85. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-inflate.js +42 -0
  86. package/node_modules/@zip.js/zip.js/lib/zip-no-worker.js +44 -0
  87. package/node_modules/@zip.js/zip.js/lib/zip.js +52 -0
  88. package/node_modules/@zip.js/zip.js/package.json +86 -0
  89. package/package.json +43 -0
  90. package/prisma-gen/default.d.ts +1 -0
  91. package/prisma-gen/default.js +1 -0
  92. package/prisma-gen/edge.d.ts +1 -0
  93. package/prisma-gen/edge.js +242 -0
  94. package/prisma-gen/index-browser.js +236 -0
  95. package/prisma-gen/index.d.ts +13248 -0
  96. package/prisma-gen/index.js +265 -0
  97. package/prisma-gen/runtime/edge-esm.js +28 -0
  98. package/prisma-gen/runtime/edge.js +28 -0
  99. package/prisma-gen/runtime/index-browser.d.ts +365 -0
  100. package/prisma-gen/runtime/index-browser.js +13 -0
  101. package/prisma-gen/runtime/library.d.ts +3168 -0
  102. package/prisma-gen/runtime/library.js +140 -0
  103. package/prisma-gen/runtime/wasm.js +29 -0
  104. package/prisma-gen/wasm.d.ts +1 -0
  105. package/prisma-gen/wasm.js +236 -0
  106. package/s3.d.ts +14 -0
  107. package/s3.js +76 -0
  108. package/schema.prisma +154 -0
  109. package/uploads.d.ts +54 -0
  110. package/uploads.js +141 -0
@@ -0,0 +1,827 @@
1
+ // Derived from https://github.com/xqdoo00o/jszip/blob/master/lib/sjcl.js and https://github.com/bitwiseshiftleft/sjcl
2
+
3
+ // deno-lint-ignore-file no-this-alias
4
+
5
+ /*
6
+ * SJCL is open. You can use, modify and redistribute it under a BSD
7
+ * license or under the GNU GPL, version 2.0.
8
+ */
9
+
10
+ /** @fileOverview Javascript cryptography implementation.
11
+ *
12
+ * Crush to remove comments, shorten variable names and
13
+ * generally reduce transmission size.
14
+ *
15
+ * @author Emily Stark
16
+ * @author Mike Hamburg
17
+ * @author Dan Boneh
18
+ */
19
+
20
+ /*jslint indent: 2, bitwise: false, nomen: false, plusplus: false, white: false, regexp: false */
21
+
22
+ /** @fileOverview Arrays of bits, encoded as arrays of Numbers.
23
+ *
24
+ * @author Emily Stark
25
+ * @author Mike Hamburg
26
+ * @author Dan Boneh
27
+ */
28
+
29
+ /**
30
+ * Arrays of bits, encoded as arrays of Numbers.
31
+ * @namespace
32
+ * @description
33
+ * <p>
34
+ * These objects are the currency accepted by SJCL's crypto functions.
35
+ * </p>
36
+ *
37
+ * <p>
38
+ * Most of our crypto primitives operate on arrays of 4-byte words internally,
39
+ * but many of them can take arguments that are not a multiple of 4 bytes.
40
+ * This library encodes arrays of bits (whose size need not be a multiple of 8
41
+ * bits) as arrays of 32-bit words. The bits are packed, big-endian, into an
42
+ * array of words, 32 bits at a time. Since the words are double-precision
43
+ * floating point numbers, they fit some extra data. We use this (in a private,
44
+ * possibly-changing manner) to encode the number of bits actually present
45
+ * in the last word of the array.
46
+ * </p>
47
+ *
48
+ * <p>
49
+ * Because bitwise ops clear this out-of-band data, these arrays can be passed
50
+ * to ciphers like AES which want arrays of words.
51
+ * </p>
52
+ */
53
+ const bitArray = {
54
+ /**
55
+ * Concatenate two bit arrays.
56
+ * @param {bitArray} a1 The first array.
57
+ * @param {bitArray} a2 The second array.
58
+ * @return {bitArray} The concatenation of a1 and a2.
59
+ */
60
+ concat(a1, a2) {
61
+ if (a1.length === 0 || a2.length === 0) {
62
+ return a1.concat(a2);
63
+ }
64
+
65
+ const last = a1[a1.length - 1], shift = bitArray.getPartial(last);
66
+ if (shift === 32) {
67
+ return a1.concat(a2);
68
+ } else {
69
+ return bitArray._shiftRight(a2, shift, last | 0, a1.slice(0, a1.length - 1));
70
+ }
71
+ },
72
+
73
+ /**
74
+ * Find the length of an array of bits.
75
+ * @param {bitArray} a The array.
76
+ * @return {Number} The length of a, in bits.
77
+ */
78
+ bitLength(a) {
79
+ const l = a.length;
80
+ if (l === 0) {
81
+ return 0;
82
+ }
83
+ const x = a[l - 1];
84
+ return (l - 1) * 32 + bitArray.getPartial(x);
85
+ },
86
+
87
+ /**
88
+ * Truncate an array.
89
+ * @param {bitArray} a The array.
90
+ * @param {Number} len The length to truncate to, in bits.
91
+ * @return {bitArray} A new array, truncated to len bits.
92
+ */
93
+ clamp(a, len) {
94
+ if (a.length * 32 < len) {
95
+ return a;
96
+ }
97
+ a = a.slice(0, Math.ceil(len / 32));
98
+ const l = a.length;
99
+ len = len & 31;
100
+ if (l > 0 && len) {
101
+ a[l - 1] = bitArray.partial(len, a[l - 1] & 0x80000000 >> (len - 1), 1);
102
+ }
103
+ return a;
104
+ },
105
+
106
+ /**
107
+ * Make a partial word for a bit array.
108
+ * @param {Number} len The number of bits in the word.
109
+ * @param {Number} x The bits.
110
+ * @param {Number} [_end=0] Pass 1 if x has already been shifted to the high side.
111
+ * @return {Number} The partial word.
112
+ */
113
+ partial(len, x, _end) {
114
+ if (len === 32) {
115
+ return x;
116
+ }
117
+ return (_end ? x | 0 : x << (32 - len)) + len * 0x10000000000;
118
+ },
119
+
120
+ /**
121
+ * Get the number of bits used by a partial word.
122
+ * @param {Number} x The partial word.
123
+ * @return {Number} The number of bits used by the partial word.
124
+ */
125
+ getPartial(x) {
126
+ return Math.round(x / 0x10000000000) || 32;
127
+ },
128
+
129
+ /** Shift an array right.
130
+ * @param {bitArray} a The array to shift.
131
+ * @param {Number} shift The number of bits to shift.
132
+ * @param {Number} [carry=0] A byte to carry in
133
+ * @param {bitArray} [out=[]] An array to prepend to the output.
134
+ * @private
135
+ */
136
+ _shiftRight(a, shift, carry, out) {
137
+ if (out === undefined) {
138
+ out = [];
139
+ }
140
+
141
+ for (; shift >= 32; shift -= 32) {
142
+ out.push(carry);
143
+ carry = 0;
144
+ }
145
+ if (shift === 0) {
146
+ return out.concat(a);
147
+ }
148
+
149
+ for (let i = 0; i < a.length; i++) {
150
+ out.push(carry | a[i] >>> shift);
151
+ carry = a[i] << (32 - shift);
152
+ }
153
+ const last2 = a.length ? a[a.length - 1] : 0;
154
+ const shift2 = bitArray.getPartial(last2);
155
+ out.push(bitArray.partial(shift + shift2 & 31, (shift + shift2 > 32) ? carry : out.pop(), 1));
156
+ return out;
157
+ }
158
+ };
159
+
160
+ /** @fileOverview Bit array codec implementations.
161
+ *
162
+ * @author Emily Stark
163
+ * @author Mike Hamburg
164
+ * @author Dan Boneh
165
+ */
166
+
167
+ /**
168
+ * Arrays of bytes
169
+ * @namespace
170
+ */
171
+ const codec = {
172
+ bytes: {
173
+ /** Convert from a bitArray to an array of bytes. */
174
+ fromBits(arr) {
175
+ const bl = bitArray.bitLength(arr);
176
+ const byteLength = bl / 8;
177
+ const out = new Uint8Array(byteLength);
178
+ let tmp;
179
+ for (let i = 0; i < byteLength; i++) {
180
+ if ((i & 3) === 0) {
181
+ tmp = arr[i / 4];
182
+ }
183
+ out[i] = tmp >>> 24;
184
+ tmp <<= 8;
185
+ }
186
+ return out;
187
+ },
188
+ /** Convert from an array of bytes to a bitArray. */
189
+ toBits(bytes) {
190
+ const out = [];
191
+ let i;
192
+ let tmp = 0;
193
+ for (i = 0; i < bytes.length; i++) {
194
+ tmp = tmp << 8 | bytes[i];
195
+ if ((i & 3) === 3) {
196
+ out.push(tmp);
197
+ tmp = 0;
198
+ }
199
+ }
200
+ if (i & 3) {
201
+ out.push(bitArray.partial(8 * (i & 3), tmp));
202
+ }
203
+ return out;
204
+ }
205
+ }
206
+ };
207
+
208
+ const hash = {};
209
+
210
+ /**
211
+ * Context for a SHA-1 operation in progress.
212
+ * @constructor
213
+ */
214
+ hash.sha1 = class {
215
+ constructor(hash) {
216
+ const sha1 = this;
217
+ /**
218
+ * The hash's block size, in bits.
219
+ * @constant
220
+ */
221
+ sha1.blockSize = 512;
222
+ /**
223
+ * The SHA-1 initialization vector.
224
+ * @private
225
+ */
226
+ sha1._init = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0];
227
+ /**
228
+ * The SHA-1 hash key.
229
+ * @private
230
+ */
231
+ sha1._key = [0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6];
232
+ if (hash) {
233
+ sha1._h = hash._h.slice(0);
234
+ sha1._buffer = hash._buffer.slice(0);
235
+ sha1._length = hash._length;
236
+ } else {
237
+ sha1.reset();
238
+ }
239
+ }
240
+
241
+ /**
242
+ * Reset the hash state.
243
+ * @return this
244
+ */
245
+ reset() {
246
+ const sha1 = this;
247
+ sha1._h = sha1._init.slice(0);
248
+ sha1._buffer = [];
249
+ sha1._length = 0;
250
+ return sha1;
251
+ }
252
+
253
+ /**
254
+ * Input several words to the hash.
255
+ * @param {bitArray|String} data the data to hash.
256
+ * @return this
257
+ */
258
+ update(data) {
259
+ const sha1 = this;
260
+ if (typeof data === "string") {
261
+ data = codec.utf8String.toBits(data);
262
+ }
263
+ const b = sha1._buffer = bitArray.concat(sha1._buffer, data);
264
+ const ol = sha1._length;
265
+ const nl = sha1._length = ol + bitArray.bitLength(data);
266
+ if (nl > 9007199254740991) {
267
+ throw new Error("Cannot hash more than 2^53 - 1 bits");
268
+ }
269
+ const c = new Uint32Array(b);
270
+ let j = 0;
271
+ for (let i = sha1.blockSize + ol - ((sha1.blockSize + ol) & (sha1.blockSize - 1)); i <= nl;
272
+ i += sha1.blockSize) {
273
+ sha1._block(c.subarray(16 * j, 16 * (j + 1)));
274
+ j += 1;
275
+ }
276
+ b.splice(0, 16 * j);
277
+ return sha1;
278
+ }
279
+
280
+ /**
281
+ * Complete hashing and output the hash value.
282
+ * @return {bitArray} The hash value, an array of 5 big-endian words. TODO
283
+ */
284
+ finalize() {
285
+ const sha1 = this;
286
+ let b = sha1._buffer;
287
+ const h = sha1._h;
288
+
289
+ // Round out and push the buffer
290
+ b = bitArray.concat(b, [bitArray.partial(1, 1)]);
291
+ // Round out the buffer to a multiple of 16 words, less the 2 length words.
292
+ for (let i = b.length + 2; i & 15; i++) {
293
+ b.push(0);
294
+ }
295
+
296
+ // append the length
297
+ b.push(Math.floor(sha1._length / 0x100000000));
298
+ b.push(sha1._length | 0);
299
+
300
+ while (b.length) {
301
+ sha1._block(b.splice(0, 16));
302
+ }
303
+
304
+ sha1.reset();
305
+ return h;
306
+ }
307
+
308
+ /**
309
+ * The SHA-1 logical functions f(0), f(1), ..., f(79).
310
+ * @private
311
+ */
312
+ _f(t, b, c, d) {
313
+ if (t <= 19) {
314
+ return (b & c) | (~b & d);
315
+ } else if (t <= 39) {
316
+ return b ^ c ^ d;
317
+ } else if (t <= 59) {
318
+ return (b & c) | (b & d) | (c & d);
319
+ } else if (t <= 79) {
320
+ return b ^ c ^ d;
321
+ }
322
+ }
323
+
324
+ /**
325
+ * Circular left-shift operator.
326
+ * @private
327
+ */
328
+ _S(n, x) {
329
+ return (x << n) | (x >>> 32 - n);
330
+ }
331
+
332
+ /**
333
+ * Perform one cycle of SHA-1.
334
+ * @param {Uint32Array|bitArray} words one block of words.
335
+ * @private
336
+ */
337
+ _block(words) {
338
+ const sha1 = this;
339
+ const h = sha1._h;
340
+ // When words is passed to _block, it has 16 elements. SHA1 _block
341
+ // function extends words with new elements (at the end there are 80 elements).
342
+ // The problem is that if we use Uint32Array instead of Array,
343
+ // the length of Uint32Array cannot be changed. Thus, we replace words with a
344
+ // normal Array here.
345
+ const w = Array(80); // do not use Uint32Array here as the instantiation is slower
346
+ for (let j = 0; j < 16; j++) {
347
+ w[j] = words[j];
348
+ }
349
+
350
+ let a = h[0];
351
+ let b = h[1];
352
+ let c = h[2];
353
+ let d = h[3];
354
+ let e = h[4];
355
+
356
+ for (let t = 0; t <= 79; t++) {
357
+ if (t >= 16) {
358
+ w[t] = sha1._S(1, w[t - 3] ^ w[t - 8] ^ w[t - 14] ^ w[t - 16]);
359
+ }
360
+ const tmp = (sha1._S(5, a) + sha1._f(t, b, c, d) + e + w[t] +
361
+ sha1._key[Math.floor(t / 20)]) | 0;
362
+ e = d;
363
+ d = c;
364
+ c = sha1._S(30, b);
365
+ b = a;
366
+ a = tmp;
367
+ }
368
+
369
+ h[0] = (h[0] + a) | 0;
370
+ h[1] = (h[1] + b) | 0;
371
+ h[2] = (h[2] + c) | 0;
372
+ h[3] = (h[3] + d) | 0;
373
+ h[4] = (h[4] + e) | 0;
374
+ }
375
+ };
376
+
377
+ /** @fileOverview Low-level AES implementation.
378
+ *
379
+ * This file contains a low-level implementation of AES, optimized for
380
+ * size and for efficiency on several browsers. It is based on
381
+ * OpenSSL's aes_core.c, a public-domain implementation by Vincent
382
+ * Rijmen, Antoon Bosselaers and Paulo Barreto.
383
+ *
384
+ * An older version of this implementation is available in the public
385
+ * domain, but this one is (c) Emily Stark, Mike Hamburg, Dan Boneh,
386
+ * Stanford University 2008-2010 and BSD-licensed for liability
387
+ * reasons.
388
+ *
389
+ * @author Emily Stark
390
+ * @author Mike Hamburg
391
+ * @author Dan Boneh
392
+ */
393
+
394
+ const cipher = {};
395
+
396
+ /**
397
+ * Schedule out an AES key for both encryption and decryption. This
398
+ * is a low-level class. Use a cipher mode to do bulk encryption.
399
+ *
400
+ * @constructor
401
+ * @param {Array} key The key as an array of 4, 6 or 8 words.
402
+ */
403
+ cipher.aes = class {
404
+ constructor(key) {
405
+ /**
406
+ * The expanded S-box and inverse S-box tables. These will be computed
407
+ * on the client so that we don't have to send them down the wire.
408
+ *
409
+ * There are two tables, _tables[0] is for encryption and
410
+ * _tables[1] is for decryption.
411
+ *
412
+ * The first 4 sub-tables are the expanded S-box with MixColumns. The
413
+ * last (_tables[01][4]) is the S-box itself.
414
+ *
415
+ * @private
416
+ */
417
+ const aes = this;
418
+ aes._tables = [[[], [], [], [], []], [[], [], [], [], []]];
419
+
420
+ if (!aes._tables[0][0][0]) {
421
+ aes._precompute();
422
+ }
423
+
424
+ const sbox = aes._tables[0][4];
425
+ const decTable = aes._tables[1];
426
+ const keyLen = key.length;
427
+
428
+ let i, encKey, decKey, rcon = 1;
429
+
430
+ if (keyLen !== 4 && keyLen !== 6 && keyLen !== 8) {
431
+ throw new Error("invalid aes key size");
432
+ }
433
+
434
+ aes._key = [encKey = key.slice(0), decKey = []];
435
+
436
+ // schedule encryption keys
437
+ for (i = keyLen; i < 4 * keyLen + 28; i++) {
438
+ let tmp = encKey[i - 1];
439
+
440
+ // apply sbox
441
+ if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
442
+ tmp = sbox[tmp >>> 24] << 24 ^ sbox[tmp >> 16 & 255] << 16 ^ sbox[tmp >> 8 & 255] << 8 ^ sbox[tmp & 255];
443
+
444
+ // shift rows and add rcon
445
+ if (i % keyLen === 0) {
446
+ tmp = tmp << 8 ^ tmp >>> 24 ^ rcon << 24;
447
+ rcon = rcon << 1 ^ (rcon >> 7) * 283;
448
+ }
449
+ }
450
+
451
+ encKey[i] = encKey[i - keyLen] ^ tmp;
452
+ }
453
+
454
+ // schedule decryption keys
455
+ for (let j = 0; i; j++, i--) {
456
+ const tmp = encKey[j & 3 ? i : i - 4];
457
+ if (i <= 4 || j < 4) {
458
+ decKey[j] = tmp;
459
+ } else {
460
+ decKey[j] = decTable[0][sbox[tmp >>> 24]] ^
461
+ decTable[1][sbox[tmp >> 16 & 255]] ^
462
+ decTable[2][sbox[tmp >> 8 & 255]] ^
463
+ decTable[3][sbox[tmp & 255]];
464
+ }
465
+ }
466
+ }
467
+ // public
468
+ /* Something like this might appear here eventually
469
+ name: "AES",
470
+ blockSize: 4,
471
+ keySizes: [4,6,8],
472
+ */
473
+
474
+ /**
475
+ * Encrypt an array of 4 big-endian words.
476
+ * @param {Array} data The plaintext.
477
+ * @return {Array} The ciphertext.
478
+ */
479
+ encrypt(data) {
480
+ return this._crypt(data, 0);
481
+ }
482
+
483
+ /**
484
+ * Decrypt an array of 4 big-endian words.
485
+ * @param {Array} data The ciphertext.
486
+ * @return {Array} The plaintext.
487
+ */
488
+ decrypt(data) {
489
+ return this._crypt(data, 1);
490
+ }
491
+
492
+ /**
493
+ * Expand the S-box tables.
494
+ *
495
+ * @private
496
+ */
497
+ _precompute() {
498
+ const encTable = this._tables[0];
499
+ const decTable = this._tables[1];
500
+ const sbox = encTable[4];
501
+ const sboxInv = decTable[4];
502
+ const d = [];
503
+ const th = [];
504
+ let xInv, x2, x4, x8;
505
+
506
+ // Compute double and third tables
507
+ for (let i = 0; i < 256; i++) {
508
+ th[(d[i] = i << 1 ^ (i >> 7) * 283) ^ i] = i;
509
+ }
510
+
511
+ for (let x = xInv = 0; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
512
+ // Compute sbox
513
+ let s = xInv ^ xInv << 1 ^ xInv << 2 ^ xInv << 3 ^ xInv << 4;
514
+ s = s >> 8 ^ s & 255 ^ 99;
515
+ sbox[x] = s;
516
+ sboxInv[s] = x;
517
+
518
+ // Compute MixColumns
519
+ x8 = d[x4 = d[x2 = d[x]]];
520
+ let tDec = x8 * 0x1010101 ^ x4 * 0x10001 ^ x2 * 0x101 ^ x * 0x1010100;
521
+ let tEnc = d[s] * 0x101 ^ s * 0x1010100;
522
+
523
+ for (let i = 0; i < 4; i++) {
524
+ encTable[i][x] = tEnc = tEnc << 24 ^ tEnc >>> 8;
525
+ decTable[i][s] = tDec = tDec << 24 ^ tDec >>> 8;
526
+ }
527
+ }
528
+
529
+ // Compactify. Considerable speedup on Firefox.
530
+ for (let i = 0; i < 5; i++) {
531
+ encTable[i] = encTable[i].slice(0);
532
+ decTable[i] = decTable[i].slice(0);
533
+ }
534
+ }
535
+
536
+ /**
537
+ * Encryption and decryption core.
538
+ * @param {Array} input Four words to be encrypted or decrypted.
539
+ * @param dir The direction, 0 for encrypt and 1 for decrypt.
540
+ * @return {Array} The four encrypted or decrypted words.
541
+ * @private
542
+ */
543
+ _crypt(input, dir) {
544
+ if (input.length !== 4) {
545
+ throw new Error("invalid aes block size");
546
+ }
547
+
548
+ const key = this._key[dir];
549
+
550
+ const nInnerRounds = key.length / 4 - 2;
551
+ const out = [0, 0, 0, 0];
552
+ const table = this._tables[dir];
553
+
554
+ // load up the tables
555
+ const t0 = table[0];
556
+ const t1 = table[1];
557
+ const t2 = table[2];
558
+ const t3 = table[3];
559
+ const sbox = table[4];
560
+
561
+ // state variables a,b,c,d are loaded with pre-whitened data
562
+ let a = input[0] ^ key[0];
563
+ let b = input[dir ? 3 : 1] ^ key[1];
564
+ let c = input[2] ^ key[2];
565
+ let d = input[dir ? 1 : 3] ^ key[3];
566
+ let kIndex = 4;
567
+ let a2, b2, c2;
568
+
569
+ // Inner rounds. Cribbed from OpenSSL.
570
+ for (let i = 0; i < nInnerRounds; i++) {
571
+ a2 = t0[a >>> 24] ^ t1[b >> 16 & 255] ^ t2[c >> 8 & 255] ^ t3[d & 255] ^ key[kIndex];
572
+ b2 = t0[b >>> 24] ^ t1[c >> 16 & 255] ^ t2[d >> 8 & 255] ^ t3[a & 255] ^ key[kIndex + 1];
573
+ c2 = t0[c >>> 24] ^ t1[d >> 16 & 255] ^ t2[a >> 8 & 255] ^ t3[b & 255] ^ key[kIndex + 2];
574
+ d = t0[d >>> 24] ^ t1[a >> 16 & 255] ^ t2[b >> 8 & 255] ^ t3[c & 255] ^ key[kIndex + 3];
575
+ kIndex += 4;
576
+ a = a2; b = b2; c = c2;
577
+ }
578
+
579
+ // Last round.
580
+ for (let i = 0; i < 4; i++) {
581
+ out[dir ? 3 & -i : i] =
582
+ sbox[a >>> 24] << 24 ^
583
+ sbox[b >> 16 & 255] << 16 ^
584
+ sbox[c >> 8 & 255] << 8 ^
585
+ sbox[d & 255] ^
586
+ key[kIndex++];
587
+ a2 = a; a = b; b = c; c = d; d = a2;
588
+ }
589
+
590
+ return out;
591
+ }
592
+ };
593
+
594
+ /**
595
+ * Random values
596
+ * @namespace
597
+ */
598
+ const random = {
599
+ /**
600
+ * Generate random words with pure js, cryptographically not as strong & safe as native implementation.
601
+ * @param {TypedArray} typedArray The array to fill.
602
+ * @return {TypedArray} The random values.
603
+ */
604
+ getRandomValues(typedArray) {
605
+ const words = new Uint32Array(typedArray.buffer);
606
+ const r = (m_w) => {
607
+ let m_z = 0x3ade68b1;
608
+ const mask = 0xffffffff;
609
+ return function () {
610
+ m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
611
+ m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
612
+ const result = ((((m_z << 0x10) + m_w) & mask) / 0x100000000) + .5;
613
+ return result * (Math.random() > .5 ? 1 : -1);
614
+ };
615
+ };
616
+ for (let i = 0, rcache; i < typedArray.length; i += 4) {
617
+ const _r = r((rcache || Math.random()) * 0x100000000);
618
+ rcache = _r() * 0x3ade67b7;
619
+ words[i / 4] = (_r() * 0x100000000) | 0;
620
+ }
621
+ return typedArray;
622
+ }
623
+ };
624
+
625
+ /** @fileOverview CTR mode implementation.
626
+ *
627
+ * Special thanks to Roy Nicholson for pointing out a bug in our
628
+ * implementation.
629
+ *
630
+ * @author Emily Stark
631
+ * @author Mike Hamburg
632
+ * @author Dan Boneh
633
+ */
634
+
635
+ /** Brian Gladman's CTR Mode.
636
+ * @constructor
637
+ * @param {Object} _prf The aes instance to generate key.
638
+ * @param {bitArray} _iv The iv for ctr mode, it must be 128 bits.
639
+ */
640
+
641
+ const mode = {};
642
+
643
+ /**
644
+ * Brian Gladman's CTR Mode.
645
+ * @namespace
646
+ */
647
+ mode.ctrGladman = class {
648
+ constructor(prf, iv) {
649
+ this._prf = prf;
650
+ this._initIv = iv;
651
+ this._iv = iv;
652
+ }
653
+
654
+ reset() {
655
+ this._iv = this._initIv;
656
+ }
657
+
658
+ /** Input some data to calculate.
659
+ * @param {bitArray} data the data to process, it must be intergral multiple of 128 bits unless it's the last.
660
+ */
661
+ update(data) {
662
+ return this.calculate(this._prf, data, this._iv);
663
+ }
664
+
665
+ incWord(word) {
666
+ if (((word >> 24) & 0xff) === 0xff) { //overflow
667
+ let b1 = (word >> 16) & 0xff;
668
+ let b2 = (word >> 8) & 0xff;
669
+ let b3 = word & 0xff;
670
+
671
+ if (b1 === 0xff) { // overflow b1
672
+ b1 = 0;
673
+ if (b2 === 0xff) {
674
+ b2 = 0;
675
+ if (b3 === 0xff) {
676
+ b3 = 0;
677
+ } else {
678
+ ++b3;
679
+ }
680
+ } else {
681
+ ++b2;
682
+ }
683
+ } else {
684
+ ++b1;
685
+ }
686
+
687
+ word = 0;
688
+ word += (b1 << 16);
689
+ word += (b2 << 8);
690
+ word += b3;
691
+ } else {
692
+ word += (0x01 << 24);
693
+ }
694
+ return word;
695
+ }
696
+
697
+ incCounter(counter) {
698
+ if ((counter[0] = this.incWord(counter[0])) === 0) {
699
+ // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
700
+ counter[1] = this.incWord(counter[1]);
701
+ }
702
+ }
703
+
704
+ calculate(prf, data, iv) {
705
+ let l;
706
+ if (!(l = data.length)) {
707
+ return [];
708
+ }
709
+ const bl = bitArray.bitLength(data);
710
+ for (let i = 0; i < l; i += 4) {
711
+ this.incCounter(iv);
712
+ const e = prf.encrypt(iv);
713
+ data[i] ^= e[0];
714
+ data[i + 1] ^= e[1];
715
+ data[i + 2] ^= e[2];
716
+ data[i + 3] ^= e[3];
717
+ }
718
+ return bitArray.clamp(data, bl);
719
+ }
720
+ };
721
+
722
+ const misc = {
723
+ importKey(password) {
724
+ return new misc.hmacSha1(codec.bytes.toBits(password));
725
+ },
726
+ pbkdf2(prf, salt, count, length) {
727
+ count = count || 10000;
728
+ if (length < 0 || count < 0) {
729
+ throw new Error("invalid params to pbkdf2");
730
+ }
731
+ const byteLength = ((length >> 5) + 1) << 2;
732
+ let u, ui, i, j, k;
733
+ const arrayBuffer = new ArrayBuffer(byteLength);
734
+ const out = new DataView(arrayBuffer);
735
+ let outLength = 0;
736
+ const b = bitArray;
737
+ salt = codec.bytes.toBits(salt);
738
+ for (k = 1; outLength < (byteLength || 1); k++) {
739
+ u = ui = prf.encrypt(b.concat(salt, [k]));
740
+ for (i = 1; i < count; i++) {
741
+ ui = prf.encrypt(ui);
742
+ for (j = 0; j < ui.length; j++) {
743
+ u[j] ^= ui[j];
744
+ }
745
+ }
746
+ for (i = 0; outLength < (byteLength || 1) && i < u.length; i++) {
747
+ out.setInt32(outLength, u[i]);
748
+ outLength += 4;
749
+ }
750
+ }
751
+ return arrayBuffer.slice(0, length / 8);
752
+ }
753
+ };
754
+
755
+ /** @fileOverview HMAC implementation.
756
+ *
757
+ * @author Emily Stark
758
+ * @author Mike Hamburg
759
+ * @author Dan Boneh
760
+ */
761
+
762
+ /** HMAC with the specified hash function.
763
+ * @constructor
764
+ * @param {bitArray} key the key for HMAC.
765
+ * @param {Object} [Hash=hash.sha1] The hash function to use.
766
+ */
767
+ misc.hmacSha1 = class {
768
+
769
+ constructor(key) {
770
+ const hmac = this;
771
+ const Hash = hmac._hash = hash.sha1;
772
+ const exKey = [[], []];
773
+ hmac._baseHash = [new Hash(), new Hash()];
774
+ const bs = hmac._baseHash[0].blockSize / 32;
775
+
776
+ if (key.length > bs) {
777
+ key = new Hash().update(key).finalize();
778
+ }
779
+
780
+ for (let i = 0; i < bs; i++) {
781
+ exKey[0][i] = key[i] ^ 0x36363636;
782
+ exKey[1][i] = key[i] ^ 0x5C5C5C5C;
783
+ }
784
+
785
+ hmac._baseHash[0].update(exKey[0]);
786
+ hmac._baseHash[1].update(exKey[1]);
787
+ hmac._resultHash = new Hash(hmac._baseHash[0]);
788
+ }
789
+ reset() {
790
+ const hmac = this;
791
+ hmac._resultHash = new hmac._hash(hmac._baseHash[0]);
792
+ hmac._updated = false;
793
+ }
794
+
795
+ update(data) {
796
+ const hmac = this;
797
+ hmac._updated = true;
798
+ hmac._resultHash.update(data);
799
+ }
800
+
801
+ digest() {
802
+ const hmac = this;
803
+ const w = hmac._resultHash.finalize();
804
+ const result = new (hmac._hash)(hmac._baseHash[1]).update(w).finalize();
805
+
806
+ hmac.reset();
807
+
808
+ return result;
809
+ }
810
+
811
+ encrypt(data) {
812
+ if (!this._updated) {
813
+ this.update(data);
814
+ return this.digest(data);
815
+ } else {
816
+ throw new Error("encrypt on already updated hmac called!");
817
+ }
818
+ }
819
+ };
820
+
821
+ export {
822
+ cipher,
823
+ codec,
824
+ misc,
825
+ mode,
826
+ random
827
+ };