@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 0.9.4

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 (174) hide show
  1. package/README.md +1 -1
  2. package/package.json +33 -18
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/get.js +1857 -0
  6. package/pdfjs/dist/index.js +767 -0
  7. package/pdfjs/dist/l10n_utils.js +140 -0
  8. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  9. package/{src/display → pdfjs/dist}/network.js +149 -87
  10. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  11. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  12. package/pdfjs/dist/pdf.js +12778 -0
  13. package/pdfjs/dist/pdf_link_service.js +638 -0
  14. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  15. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  16. package/pdfjs/dist/pdf_viewer.js +3598 -0
  17. package/pdfjs/dist/typeof.js +100 -0
  18. package/pdfjs/dist/ui_utils.js +1033 -0
  19. package/{src/shared → pdfjs/dist}/util.js +301 -287
  20. package/pdfjs/dist/worker.js +62813 -0
  21. package/src/vcf-pdf-viewer.js +77 -27
  22. package/theme/lumo/vcf-pdf-viewer-styles.js +1 -1
  23. package/theme/material/vcf-pdf-viewer.js +2 -2
  24. package/src/core/.eslintrc +0 -13
  25. package/src/core/annotation.js +0 -2948
  26. package/src/core/arithmetic_decoder.js +0 -182
  27. package/src/core/ascii_85_stream.js +0 -98
  28. package/src/core/ascii_hex_stream.js +0 -79
  29. package/src/core/base_stream.js +0 -110
  30. package/src/core/bidi.js +0 -438
  31. package/src/core/calibri_factors.js +0 -308
  32. package/src/core/catalog.js +0 -1459
  33. package/src/core/ccitt.js +0 -1062
  34. package/src/core/ccitt_stream.js +0 -60
  35. package/src/core/cff_font.js +0 -116
  36. package/src/core/cff_parser.js +0 -1949
  37. package/src/core/charsets.js +0 -119
  38. package/src/core/chunked_stream.js +0 -557
  39. package/src/core/cmap.js +0 -1039
  40. package/src/core/colorspace.js +0 -1533
  41. package/src/core/core_utils.js +0 -464
  42. package/src/core/crypto.js +0 -1900
  43. package/src/core/decode_stream.js +0 -170
  44. package/src/core/decrypt_stream.js +0 -59
  45. package/src/core/default_appearance.js +0 -99
  46. package/src/core/document.js +0 -1456
  47. package/src/core/encodings.js +0 -301
  48. package/src/core/evaluator.js +0 -4601
  49. package/src/core/file_spec.js +0 -108
  50. package/src/core/flate_stream.js +0 -402
  51. package/src/core/font_renderer.js +0 -882
  52. package/src/core/fonts.js +0 -3260
  53. package/src/core/fonts_utils.js +0 -221
  54. package/src/core/function.js +0 -1257
  55. package/src/core/glyf.js +0 -706
  56. package/src/core/glyphlist.js +0 -4558
  57. package/src/core/helvetica_factors.js +0 -353
  58. package/src/core/image.js +0 -802
  59. package/src/core/image_utils.js +0 -291
  60. package/src/core/jbig2.js +0 -2572
  61. package/src/core/jbig2_stream.js +0 -73
  62. package/src/core/jpeg_stream.js +0 -105
  63. package/src/core/jpg.js +0 -1416
  64. package/src/core/jpx.js +0 -2343
  65. package/src/core/jpx_stream.js +0 -87
  66. package/src/core/liberationsans_widths.js +0 -221
  67. package/src/core/lzw_stream.js +0 -150
  68. package/src/core/metadata_parser.js +0 -146
  69. package/src/core/metrics.js +0 -2970
  70. package/src/core/murmurhash3.js +0 -139
  71. package/src/core/myriadpro_factors.js +0 -290
  72. package/src/core/name_number_tree.js +0 -153
  73. package/src/core/object_loader.js +0 -149
  74. package/src/core/opentype_file_builder.js +0 -154
  75. package/src/core/operator_list.js +0 -734
  76. package/src/core/parser.js +0 -1416
  77. package/src/core/pattern.js +0 -985
  78. package/src/core/pdf_manager.js +0 -217
  79. package/src/core/predictor_stream.js +0 -238
  80. package/src/core/primitives.js +0 -402
  81. package/src/core/ps_parser.js +0 -272
  82. package/src/core/run_length_stream.js +0 -61
  83. package/src/core/segoeui_factors.js +0 -308
  84. package/src/core/standard_fonts.js +0 -817
  85. package/src/core/stream.js +0 -103
  86. package/src/core/struct_tree.js +0 -335
  87. package/src/core/to_unicode_map.js +0 -103
  88. package/src/core/type1_font.js +0 -421
  89. package/src/core/type1_parser.js +0 -776
  90. package/src/core/unicode.js +0 -1649
  91. package/src/core/worker.js +0 -848
  92. package/src/core/worker_stream.js +0 -135
  93. package/src/core/writer.js +0 -278
  94. package/src/core/xfa/bind.js +0 -652
  95. package/src/core/xfa/builder.js +0 -207
  96. package/src/core/xfa/config.js +0 -1926
  97. package/src/core/xfa/connection_set.js +0 -202
  98. package/src/core/xfa/data.js +0 -82
  99. package/src/core/xfa/datasets.js +0 -76
  100. package/src/core/xfa/factory.js +0 -111
  101. package/src/core/xfa/fonts.js +0 -181
  102. package/src/core/xfa/formcalc_lexer.js +0 -385
  103. package/src/core/xfa/formcalc_parser.js +0 -1340
  104. package/src/core/xfa/html_utils.js +0 -639
  105. package/src/core/xfa/layout.js +0 -383
  106. package/src/core/xfa/locale_set.js +0 -345
  107. package/src/core/xfa/namespaces.js +0 -81
  108. package/src/core/xfa/parser.js +0 -184
  109. package/src/core/xfa/setup.js +0 -38
  110. package/src/core/xfa/signature.js +0 -40
  111. package/src/core/xfa/som.js +0 -338
  112. package/src/core/xfa/stylesheet.js +0 -40
  113. package/src/core/xfa/template.js +0 -6260
  114. package/src/core/xfa/text.js +0 -290
  115. package/src/core/xfa/unknown.js +0 -29
  116. package/src/core/xfa/utils.js +0 -217
  117. package/src/core/xfa/xdp.js +0 -59
  118. package/src/core/xfa/xfa_object.js +0 -1130
  119. package/src/core/xfa/xhtml.js +0 -543
  120. package/src/core/xfa_fonts.js +0 -208
  121. package/src/core/xml_parser.js +0 -507
  122. package/src/core/xref.js +0 -899
  123. package/src/display/annotation_layer.js +0 -2107
  124. package/src/display/annotation_storage.js +0 -113
  125. package/src/display/api.js +0 -3292
  126. package/src/display/base_factory.js +0 -180
  127. package/src/display/canvas.js +0 -2828
  128. package/src/display/font_loader.js +0 -484
  129. package/src/display/metadata.js +0 -41
  130. package/src/display/network_utils.js +0 -100
  131. package/src/display/node_utils.js +0 -83
  132. package/src/display/optional_content_config.js +0 -189
  133. package/src/display/pattern_helper.js +0 -659
  134. package/src/display/svg.js +0 -1709
  135. package/src/display/text_layer.js +0 -847
  136. package/src/display/transport_stream.js +0 -303
  137. package/src/display/worker_options.js +0 -40
  138. package/src/display/xfa_layer.js +0 -204
  139. package/src/doc_helper.js +0 -25
  140. package/src/images/logo.svg +0 -41
  141. package/src/interfaces.js +0 -169
  142. package/src/license_header.js +0 -14
  143. package/src/license_header_libre.js +0 -21
  144. package/src/pdf.image_decoders.js +0 -46
  145. package/src/pdf.js +0 -146
  146. package/src/pdf.sandbox.external.js +0 -181
  147. package/src/pdf.sandbox.js +0 -151
  148. package/src/pdf.scripting.js +0 -25
  149. package/src/pdf.worker.entry.js +0 -19
  150. package/src/pdf.worker.js +0 -23
  151. package/src/scripting_api/aform.js +0 -608
  152. package/src/scripting_api/app.js +0 -621
  153. package/src/scripting_api/color.js +0 -129
  154. package/src/scripting_api/common.js +0 -58
  155. package/src/scripting_api/console.js +0 -38
  156. package/src/scripting_api/constants.js +0 -208
  157. package/src/scripting_api/doc.js +0 -1195
  158. package/src/scripting_api/error.js +0 -23
  159. package/src/scripting_api/event.js +0 -232
  160. package/src/scripting_api/field.js +0 -620
  161. package/src/scripting_api/fullscreen.js +0 -145
  162. package/src/scripting_api/initialization.js +0 -223
  163. package/src/scripting_api/pdf_object.js +0 -24
  164. package/src/scripting_api/print_params.js +0 -146
  165. package/src/scripting_api/proxy.js +0 -139
  166. package/src/scripting_api/thermometer.js +0 -69
  167. package/src/scripting_api/util.js +0 -581
  168. package/src/shared/.eslintrc +0 -13
  169. package/src/shared/cffStandardStrings.js +0 -311
  170. package/src/shared/compatibility.js +0 -114
  171. package/src/shared/fonts_utils.js +0 -429
  172. package/src/shared/is_node.js +0 -27
  173. package/src/shared/scripting_utils.js +0 -85
  174. package/src/worker_loader.js +0 -32
@@ -1,1900 +0,0 @@
1
- /* Copyright 2012 Mozilla Foundation
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- import {
17
- bytesToString,
18
- FormatError,
19
- isArrayEqual,
20
- PasswordException,
21
- PasswordResponses,
22
- stringToBytes,
23
- unreachable,
24
- utf8StringToString,
25
- warn,
26
- } from "../shared/util.js";
27
- import { isDict, isName, Name } from "./primitives.js";
28
- import { DecryptStream } from "./decrypt_stream.js";
29
-
30
- class ARCFourCipher {
31
- constructor(key) {
32
- this.a = 0;
33
- this.b = 0;
34
- const s = new Uint8Array(256);
35
- const keyLength = key.length;
36
-
37
- for (let i = 0; i < 256; ++i) {
38
- s[i] = i;
39
- }
40
- for (let i = 0, j = 0; i < 256; ++i) {
41
- const tmp = s[i];
42
- j = (j + tmp + key[i % keyLength]) & 0xff;
43
- s[i] = s[j];
44
- s[j] = tmp;
45
- }
46
- this.s = s;
47
- }
48
-
49
- encryptBlock(data) {
50
- let a = this.a,
51
- b = this.b;
52
- const s = this.s;
53
- const n = data.length;
54
- const output = new Uint8Array(n);
55
- for (let i = 0; i < n; ++i) {
56
- a = (a + 1) & 0xff;
57
- const tmp = s[a];
58
- b = (b + tmp) & 0xff;
59
- const tmp2 = s[b];
60
- s[a] = tmp2;
61
- s[b] = tmp;
62
- output[i] = data[i] ^ s[(tmp + tmp2) & 0xff];
63
- }
64
- this.a = a;
65
- this.b = b;
66
- return output;
67
- }
68
-
69
- decryptBlock(data) {
70
- return this.encryptBlock(data);
71
- }
72
-
73
- encrypt(data) {
74
- return this.encryptBlock(data);
75
- }
76
- }
77
-
78
- const calculateMD5 = (function calculateMD5Closure() {
79
- const r = new Uint8Array([
80
- 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5,
81
- 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11,
82
- 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10,
83
- 15, 21,
84
- ]);
85
-
86
- const k = new Int32Array([
87
- -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426,
88
- -1473231341, -45705983, 1770035416, -1958414417, -42063, -1990404162,
89
- 1804603682, -40341101, -1502002290, 1236535329, -165796510, -1069501632,
90
- 643717713, -373897302, -701558691, 38016083, -660478335, -405537848,
91
- 568446438, -1019803690, -187363961, 1163531501, -1444681467, -51403784,
92
- 1735328473, -1926607734, -378558, -2022574463, 1839030562, -35309556,
93
- -1530992060, 1272893353, -155497632, -1094730640, 681279174, -358537222,
94
- -722521979, 76029189, -640364487, -421815835, 530742520, -995338651,
95
- -198630844, 1126891415, -1416354905, -57434055, 1700485571, -1894986606,
96
- -1051523, -2054922799, 1873313359, -30611744, -1560198380, 1309151649,
97
- -145523070, -1120210379, 718787259, -343485551,
98
- ]);
99
-
100
- function hash(data, offset, length) {
101
- let h0 = 1732584193,
102
- h1 = -271733879,
103
- h2 = -1732584194,
104
- h3 = 271733878;
105
- // pre-processing
106
- const paddedLength = (length + 72) & ~63; // data + 9 extra bytes
107
- const padded = new Uint8Array(paddedLength);
108
- let i, j;
109
- for (i = 0; i < length; ++i) {
110
- padded[i] = data[offset++];
111
- }
112
- padded[i++] = 0x80;
113
- const n = paddedLength - 8;
114
- while (i < n) {
115
- padded[i++] = 0;
116
- }
117
- padded[i++] = (length << 3) & 0xff;
118
- padded[i++] = (length >> 5) & 0xff;
119
- padded[i++] = (length >> 13) & 0xff;
120
- padded[i++] = (length >> 21) & 0xff;
121
- padded[i++] = (length >>> 29) & 0xff;
122
- padded[i++] = 0;
123
- padded[i++] = 0;
124
- padded[i++] = 0;
125
- const w = new Int32Array(16);
126
- for (i = 0; i < paddedLength; ) {
127
- for (j = 0; j < 16; ++j, i += 4) {
128
- w[j] =
129
- padded[i] |
130
- (padded[i + 1] << 8) |
131
- (padded[i + 2] << 16) |
132
- (padded[i + 3] << 24);
133
- }
134
- let a = h0,
135
- b = h1,
136
- c = h2,
137
- d = h3,
138
- f,
139
- g;
140
- for (j = 0; j < 64; ++j) {
141
- if (j < 16) {
142
- f = (b & c) | (~b & d);
143
- g = j;
144
- } else if (j < 32) {
145
- f = (d & b) | (~d & c);
146
- g = (5 * j + 1) & 15;
147
- } else if (j < 48) {
148
- f = b ^ c ^ d;
149
- g = (3 * j + 5) & 15;
150
- } else {
151
- f = c ^ (b | ~d);
152
- g = (7 * j) & 15;
153
- }
154
- const tmp = d,
155
- rotateArg = (a + f + k[j] + w[g]) | 0,
156
- rotate = r[j];
157
- d = c;
158
- c = b;
159
- b = (b + ((rotateArg << rotate) | (rotateArg >>> (32 - rotate)))) | 0;
160
- a = tmp;
161
- }
162
- h0 = (h0 + a) | 0;
163
- h1 = (h1 + b) | 0;
164
- h2 = (h2 + c) | 0;
165
- h3 = (h3 + d) | 0;
166
- }
167
- // prettier-ignore
168
- return new Uint8Array([
169
- h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >>> 24) & 0xFF,
170
- h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >>> 24) & 0xFF,
171
- h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >>> 24) & 0xFF,
172
- h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >>> 24) & 0xFF
173
- ]);
174
- }
175
-
176
- return hash;
177
- })();
178
-
179
- class Word64 {
180
- constructor(highInteger, lowInteger) {
181
- this.high = highInteger | 0;
182
- this.low = lowInteger | 0;
183
- }
184
-
185
- and(word) {
186
- this.high &= word.high;
187
- this.low &= word.low;
188
- }
189
-
190
- xor(word) {
191
- this.high ^= word.high;
192
- this.low ^= word.low;
193
- }
194
-
195
- or(word) {
196
- this.high |= word.high;
197
- this.low |= word.low;
198
- }
199
-
200
- shiftRight(places) {
201
- if (places >= 32) {
202
- this.low = (this.high >>> (places - 32)) | 0;
203
- this.high = 0;
204
- } else {
205
- this.low = (this.low >>> places) | (this.high << (32 - places));
206
- this.high = (this.high >>> places) | 0;
207
- }
208
- }
209
-
210
- shiftLeft(places) {
211
- if (places >= 32) {
212
- this.high = this.low << (places - 32);
213
- this.low = 0;
214
- } else {
215
- this.high = (this.high << places) | (this.low >>> (32 - places));
216
- this.low <<= places;
217
- }
218
- }
219
-
220
- rotateRight(places) {
221
- let low, high;
222
- if (places & 32) {
223
- high = this.low;
224
- low = this.high;
225
- } else {
226
- low = this.low;
227
- high = this.high;
228
- }
229
- places &= 31;
230
- this.low = (low >>> places) | (high << (32 - places));
231
- this.high = (high >>> places) | (low << (32 - places));
232
- }
233
-
234
- not() {
235
- this.high = ~this.high;
236
- this.low = ~this.low;
237
- }
238
-
239
- add(word) {
240
- const lowAdd = (this.low >>> 0) + (word.low >>> 0);
241
- let highAdd = (this.high >>> 0) + (word.high >>> 0);
242
- if (lowAdd > 0xffffffff) {
243
- highAdd += 1;
244
- }
245
- this.low = lowAdd | 0;
246
- this.high = highAdd | 0;
247
- }
248
-
249
- copyTo(bytes, offset) {
250
- bytes[offset] = (this.high >>> 24) & 0xff;
251
- bytes[offset + 1] = (this.high >> 16) & 0xff;
252
- bytes[offset + 2] = (this.high >> 8) & 0xff;
253
- bytes[offset + 3] = this.high & 0xff;
254
- bytes[offset + 4] = (this.low >>> 24) & 0xff;
255
- bytes[offset + 5] = (this.low >> 16) & 0xff;
256
- bytes[offset + 6] = (this.low >> 8) & 0xff;
257
- bytes[offset + 7] = this.low & 0xff;
258
- }
259
-
260
- assign(word) {
261
- this.high = word.high;
262
- this.low = word.low;
263
- }
264
- }
265
-
266
- const calculateSHA256 = (function calculateSHA256Closure() {
267
- function rotr(x, n) {
268
- return (x >>> n) | (x << (32 - n));
269
- }
270
-
271
- function ch(x, y, z) {
272
- return (x & y) ^ (~x & z);
273
- }
274
-
275
- function maj(x, y, z) {
276
- return (x & y) ^ (x & z) ^ (y & z);
277
- }
278
-
279
- function sigma(x) {
280
- return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
281
- }
282
-
283
- function sigmaPrime(x) {
284
- return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
285
- }
286
-
287
- function littleSigma(x) {
288
- return rotr(x, 7) ^ rotr(x, 18) ^ (x >>> 3);
289
- }
290
-
291
- function littleSigmaPrime(x) {
292
- return rotr(x, 17) ^ rotr(x, 19) ^ (x >>> 10);
293
- }
294
-
295
- const k = [
296
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
297
- 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
298
- 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
299
- 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
300
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
301
- 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
302
- 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
303
- 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
304
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
305
- 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
306
- 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
307
- ];
308
-
309
- function hash(data, offset, length) {
310
- // initial hash values
311
- let h0 = 0x6a09e667,
312
- h1 = 0xbb67ae85,
313
- h2 = 0x3c6ef372,
314
- h3 = 0xa54ff53a,
315
- h4 = 0x510e527f,
316
- h5 = 0x9b05688c,
317
- h6 = 0x1f83d9ab,
318
- h7 = 0x5be0cd19;
319
- // pre-processing
320
- const paddedLength = Math.ceil((length + 9) / 64) * 64;
321
- const padded = new Uint8Array(paddedLength);
322
- let i, j;
323
- for (i = 0; i < length; ++i) {
324
- padded[i] = data[offset++];
325
- }
326
- padded[i++] = 0x80;
327
- const n = paddedLength - 8;
328
- while (i < n) {
329
- padded[i++] = 0;
330
- }
331
- padded[i++] = 0;
332
- padded[i++] = 0;
333
- padded[i++] = 0;
334
- padded[i++] = (length >>> 29) & 0xff;
335
- padded[i++] = (length >> 21) & 0xff;
336
- padded[i++] = (length >> 13) & 0xff;
337
- padded[i++] = (length >> 5) & 0xff;
338
- padded[i++] = (length << 3) & 0xff;
339
- const w = new Uint32Array(64);
340
- // for each 512 bit block
341
- for (i = 0; i < paddedLength; ) {
342
- for (j = 0; j < 16; ++j) {
343
- w[j] =
344
- (padded[i] << 24) |
345
- (padded[i + 1] << 16) |
346
- (padded[i + 2] << 8) |
347
- padded[i + 3];
348
- i += 4;
349
- }
350
-
351
- for (j = 16; j < 64; ++j) {
352
- w[j] =
353
- (littleSigmaPrime(w[j - 2]) +
354
- w[j - 7] +
355
- littleSigma(w[j - 15]) +
356
- w[j - 16]) |
357
- 0;
358
- }
359
- let a = h0,
360
- b = h1,
361
- c = h2,
362
- d = h3,
363
- e = h4,
364
- f = h5,
365
- g = h6,
366
- h = h7,
367
- t1,
368
- t2;
369
- for (j = 0; j < 64; ++j) {
370
- t1 = h + sigmaPrime(e) + ch(e, f, g) + k[j] + w[j];
371
- t2 = sigma(a) + maj(a, b, c);
372
- h = g;
373
- g = f;
374
- f = e;
375
- e = (d + t1) | 0;
376
- d = c;
377
- c = b;
378
- b = a;
379
- a = (t1 + t2) | 0;
380
- }
381
- h0 = (h0 + a) | 0;
382
- h1 = (h1 + b) | 0;
383
- h2 = (h2 + c) | 0;
384
- h3 = (h3 + d) | 0;
385
- h4 = (h4 + e) | 0;
386
- h5 = (h5 + f) | 0;
387
- h6 = (h6 + g) | 0;
388
- h7 = (h7 + h) | 0;
389
- }
390
- // prettier-ignore
391
- return new Uint8Array([
392
- (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, (h0) & 0xFF,
393
- (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, (h1) & 0xFF,
394
- (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, (h2) & 0xFF,
395
- (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, (h3) & 0xFF,
396
- (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, (h4) & 0xFF,
397
- (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, (h5) & 0xFF,
398
- (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, (h6) & 0xFF,
399
- (h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, (h7) & 0xFF
400
- ]);
401
- }
402
-
403
- return hash;
404
- })();
405
-
406
- const calculateSHA512 = (function calculateSHA512Closure() {
407
- function ch(result, x, y, z, tmp) {
408
- result.assign(x);
409
- result.and(y);
410
- tmp.assign(x);
411
- tmp.not();
412
- tmp.and(z);
413
- result.xor(tmp);
414
- }
415
-
416
- function maj(result, x, y, z, tmp) {
417
- result.assign(x);
418
- result.and(y);
419
- tmp.assign(x);
420
- tmp.and(z);
421
- result.xor(tmp);
422
- tmp.assign(y);
423
- tmp.and(z);
424
- result.xor(tmp);
425
- }
426
-
427
- function sigma(result, x, tmp) {
428
- result.assign(x);
429
- result.rotateRight(28);
430
- tmp.assign(x);
431
- tmp.rotateRight(34);
432
- result.xor(tmp);
433
- tmp.assign(x);
434
- tmp.rotateRight(39);
435
- result.xor(tmp);
436
- }
437
-
438
- function sigmaPrime(result, x, tmp) {
439
- result.assign(x);
440
- result.rotateRight(14);
441
- tmp.assign(x);
442
- tmp.rotateRight(18);
443
- result.xor(tmp);
444
- tmp.assign(x);
445
- tmp.rotateRight(41);
446
- result.xor(tmp);
447
- }
448
-
449
- function littleSigma(result, x, tmp) {
450
- result.assign(x);
451
- result.rotateRight(1);
452
- tmp.assign(x);
453
- tmp.rotateRight(8);
454
- result.xor(tmp);
455
- tmp.assign(x);
456
- tmp.shiftRight(7);
457
- result.xor(tmp);
458
- }
459
-
460
- function littleSigmaPrime(result, x, tmp) {
461
- result.assign(x);
462
- result.rotateRight(19);
463
- tmp.assign(x);
464
- tmp.rotateRight(61);
465
- result.xor(tmp);
466
- tmp.assign(x);
467
- tmp.shiftRight(6);
468
- result.xor(tmp);
469
- }
470
-
471
- // prettier-ignore
472
- const k = [
473
- new Word64(0x428a2f98, 0xd728ae22), new Word64(0x71374491, 0x23ef65cd),
474
- new Word64(0xb5c0fbcf, 0xec4d3b2f), new Word64(0xe9b5dba5, 0x8189dbbc),
475
- new Word64(0x3956c25b, 0xf348b538), new Word64(0x59f111f1, 0xb605d019),
476
- new Word64(0x923f82a4, 0xaf194f9b), new Word64(0xab1c5ed5, 0xda6d8118),
477
- new Word64(0xd807aa98, 0xa3030242), new Word64(0x12835b01, 0x45706fbe),
478
- new Word64(0x243185be, 0x4ee4b28c), new Word64(0x550c7dc3, 0xd5ffb4e2),
479
- new Word64(0x72be5d74, 0xf27b896f), new Word64(0x80deb1fe, 0x3b1696b1),
480
- new Word64(0x9bdc06a7, 0x25c71235), new Word64(0xc19bf174, 0xcf692694),
481
- new Word64(0xe49b69c1, 0x9ef14ad2), new Word64(0xefbe4786, 0x384f25e3),
482
- new Word64(0x0fc19dc6, 0x8b8cd5b5), new Word64(0x240ca1cc, 0x77ac9c65),
483
- new Word64(0x2de92c6f, 0x592b0275), new Word64(0x4a7484aa, 0x6ea6e483),
484
- new Word64(0x5cb0a9dc, 0xbd41fbd4), new Word64(0x76f988da, 0x831153b5),
485
- new Word64(0x983e5152, 0xee66dfab), new Word64(0xa831c66d, 0x2db43210),
486
- new Word64(0xb00327c8, 0x98fb213f), new Word64(0xbf597fc7, 0xbeef0ee4),
487
- new Word64(0xc6e00bf3, 0x3da88fc2), new Word64(0xd5a79147, 0x930aa725),
488
- new Word64(0x06ca6351, 0xe003826f), new Word64(0x14292967, 0x0a0e6e70),
489
- new Word64(0x27b70a85, 0x46d22ffc), new Word64(0x2e1b2138, 0x5c26c926),
490
- new Word64(0x4d2c6dfc, 0x5ac42aed), new Word64(0x53380d13, 0x9d95b3df),
491
- new Word64(0x650a7354, 0x8baf63de), new Word64(0x766a0abb, 0x3c77b2a8),
492
- new Word64(0x81c2c92e, 0x47edaee6), new Word64(0x92722c85, 0x1482353b),
493
- new Word64(0xa2bfe8a1, 0x4cf10364), new Word64(0xa81a664b, 0xbc423001),
494
- new Word64(0xc24b8b70, 0xd0f89791), new Word64(0xc76c51a3, 0x0654be30),
495
- new Word64(0xd192e819, 0xd6ef5218), new Word64(0xd6990624, 0x5565a910),
496
- new Word64(0xf40e3585, 0x5771202a), new Word64(0x106aa070, 0x32bbd1b8),
497
- new Word64(0x19a4c116, 0xb8d2d0c8), new Word64(0x1e376c08, 0x5141ab53),
498
- new Word64(0x2748774c, 0xdf8eeb99), new Word64(0x34b0bcb5, 0xe19b48a8),
499
- new Word64(0x391c0cb3, 0xc5c95a63), new Word64(0x4ed8aa4a, 0xe3418acb),
500
- new Word64(0x5b9cca4f, 0x7763e373), new Word64(0x682e6ff3, 0xd6b2b8a3),
501
- new Word64(0x748f82ee, 0x5defb2fc), new Word64(0x78a5636f, 0x43172f60),
502
- new Word64(0x84c87814, 0xa1f0ab72), new Word64(0x8cc70208, 0x1a6439ec),
503
- new Word64(0x90befffa, 0x23631e28), new Word64(0xa4506ceb, 0xde82bde9),
504
- new Word64(0xbef9a3f7, 0xb2c67915), new Word64(0xc67178f2, 0xe372532b),
505
- new Word64(0xca273ece, 0xea26619c), new Word64(0xd186b8c7, 0x21c0c207),
506
- new Word64(0xeada7dd6, 0xcde0eb1e), new Word64(0xf57d4f7f, 0xee6ed178),
507
- new Word64(0x06f067aa, 0x72176fba), new Word64(0x0a637dc5, 0xa2c898a6),
508
- new Word64(0x113f9804, 0xbef90dae), new Word64(0x1b710b35, 0x131c471b),
509
- new Word64(0x28db77f5, 0x23047d84), new Word64(0x32caab7b, 0x40c72493),
510
- new Word64(0x3c9ebe0a, 0x15c9bebc), new Word64(0x431d67c4, 0x9c100d4c),
511
- new Word64(0x4cc5d4be, 0xcb3e42b6), new Word64(0x597f299c, 0xfc657e2a),
512
- new Word64(0x5fcb6fab, 0x3ad6faec), new Word64(0x6c44198c, 0x4a475817)];
513
-
514
- function hash(data, offset, length, mode384 = false) {
515
- // initial hash values
516
- let h0, h1, h2, h3, h4, h5, h6, h7;
517
- if (!mode384) {
518
- h0 = new Word64(0x6a09e667, 0xf3bcc908);
519
- h1 = new Word64(0xbb67ae85, 0x84caa73b);
520
- h2 = new Word64(0x3c6ef372, 0xfe94f82b);
521
- h3 = new Word64(0xa54ff53a, 0x5f1d36f1);
522
- h4 = new Word64(0x510e527f, 0xade682d1);
523
- h5 = new Word64(0x9b05688c, 0x2b3e6c1f);
524
- h6 = new Word64(0x1f83d9ab, 0xfb41bd6b);
525
- h7 = new Word64(0x5be0cd19, 0x137e2179);
526
- } else {
527
- // SHA384 is exactly the same
528
- // except with different starting values and a trimmed result
529
- h0 = new Word64(0xcbbb9d5d, 0xc1059ed8);
530
- h1 = new Word64(0x629a292a, 0x367cd507);
531
- h2 = new Word64(0x9159015a, 0x3070dd17);
532
- h3 = new Word64(0x152fecd8, 0xf70e5939);
533
- h4 = new Word64(0x67332667, 0xffc00b31);
534
- h5 = new Word64(0x8eb44a87, 0x68581511);
535
- h6 = new Word64(0xdb0c2e0d, 0x64f98fa7);
536
- h7 = new Word64(0x47b5481d, 0xbefa4fa4);
537
- }
538
-
539
- // pre-processing
540
- const paddedLength = Math.ceil((length + 17) / 128) * 128;
541
- const padded = new Uint8Array(paddedLength);
542
- let i, j;
543
- for (i = 0; i < length; ++i) {
544
- padded[i] = data[offset++];
545
- }
546
- padded[i++] = 0x80;
547
- const n = paddedLength - 16;
548
- while (i < n) {
549
- padded[i++] = 0;
550
- }
551
- padded[i++] = 0;
552
- padded[i++] = 0;
553
- padded[i++] = 0;
554
- padded[i++] = 0;
555
- padded[i++] = 0;
556
- padded[i++] = 0;
557
- padded[i++] = 0;
558
- padded[i++] = 0;
559
- padded[i++] = 0;
560
- padded[i++] = 0;
561
- padded[i++] = 0;
562
- padded[i++] = (length >>> 29) & 0xff;
563
- padded[i++] = (length >> 21) & 0xff;
564
- padded[i++] = (length >> 13) & 0xff;
565
- padded[i++] = (length >> 5) & 0xff;
566
- padded[i++] = (length << 3) & 0xff;
567
-
568
- const w = new Array(80);
569
- for (i = 0; i < 80; i++) {
570
- w[i] = new Word64(0, 0);
571
- }
572
- let a = new Word64(0, 0),
573
- b = new Word64(0, 0),
574
- c = new Word64(0, 0);
575
- let d = new Word64(0, 0),
576
- e = new Word64(0, 0),
577
- f = new Word64(0, 0);
578
- let g = new Word64(0, 0),
579
- h = new Word64(0, 0);
580
- const t1 = new Word64(0, 0),
581
- t2 = new Word64(0, 0);
582
- const tmp1 = new Word64(0, 0),
583
- tmp2 = new Word64(0, 0);
584
- let tmp3;
585
-
586
- // for each 1024 bit block
587
- for (i = 0; i < paddedLength; ) {
588
- for (j = 0; j < 16; ++j) {
589
- w[j].high =
590
- (padded[i] << 24) |
591
- (padded[i + 1] << 16) |
592
- (padded[i + 2] << 8) |
593
- padded[i + 3];
594
- w[j].low =
595
- (padded[i + 4] << 24) |
596
- (padded[i + 5] << 16) |
597
- (padded[i + 6] << 8) |
598
- padded[i + 7];
599
- i += 8;
600
- }
601
- for (j = 16; j < 80; ++j) {
602
- tmp3 = w[j];
603
- littleSigmaPrime(tmp3, w[j - 2], tmp2);
604
- tmp3.add(w[j - 7]);
605
- littleSigma(tmp1, w[j - 15], tmp2);
606
- tmp3.add(tmp1);
607
- tmp3.add(w[j - 16]);
608
- }
609
-
610
- a.assign(h0);
611
- b.assign(h1);
612
- c.assign(h2);
613
- d.assign(h3);
614
- e.assign(h4);
615
- f.assign(h5);
616
- g.assign(h6);
617
- h.assign(h7);
618
- for (j = 0; j < 80; ++j) {
619
- t1.assign(h);
620
- sigmaPrime(tmp1, e, tmp2);
621
- t1.add(tmp1);
622
- ch(tmp1, e, f, g, tmp2);
623
- t1.add(tmp1);
624
- t1.add(k[j]);
625
- t1.add(w[j]);
626
-
627
- sigma(t2, a, tmp2);
628
- maj(tmp1, a, b, c, tmp2);
629
- t2.add(tmp1);
630
-
631
- tmp3 = h;
632
- h = g;
633
- g = f;
634
- f = e;
635
- d.add(t1);
636
- e = d;
637
- d = c;
638
- c = b;
639
- b = a;
640
- tmp3.assign(t1);
641
- tmp3.add(t2);
642
- a = tmp3;
643
- }
644
- h0.add(a);
645
- h1.add(b);
646
- h2.add(c);
647
- h3.add(d);
648
- h4.add(e);
649
- h5.add(f);
650
- h6.add(g);
651
- h7.add(h);
652
- }
653
-
654
- let result;
655
- if (!mode384) {
656
- result = new Uint8Array(64);
657
- h0.copyTo(result, 0);
658
- h1.copyTo(result, 8);
659
- h2.copyTo(result, 16);
660
- h3.copyTo(result, 24);
661
- h4.copyTo(result, 32);
662
- h5.copyTo(result, 40);
663
- h6.copyTo(result, 48);
664
- h7.copyTo(result, 56);
665
- } else {
666
- result = new Uint8Array(48);
667
- h0.copyTo(result, 0);
668
- h1.copyTo(result, 8);
669
- h2.copyTo(result, 16);
670
- h3.copyTo(result, 24);
671
- h4.copyTo(result, 32);
672
- h5.copyTo(result, 40);
673
- }
674
- return result;
675
- }
676
-
677
- return hash;
678
- })();
679
-
680
- function calculateSHA384(data, offset, length) {
681
- return calculateSHA512(data, offset, length, /* mode384 = */ true);
682
- }
683
-
684
- class NullCipher {
685
- decryptBlock(data) {
686
- return data;
687
- }
688
-
689
- encrypt(data) {
690
- return data;
691
- }
692
- }
693
-
694
- class AESBaseCipher {
695
- constructor() {
696
- if (this.constructor === AESBaseCipher) {
697
- unreachable("Cannot initialize AESBaseCipher.");
698
- }
699
-
700
- this._s = new Uint8Array([
701
- 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
702
- 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
703
- 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
704
- 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
705
- 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
706
- 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
707
- 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
708
- 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
709
- 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
710
- 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
711
- 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
712
- 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
713
- 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
714
- 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
715
- 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
716
- 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
717
- 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
718
- 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
719
- 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
720
- 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
721
- 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
722
- 0xb0, 0x54, 0xbb, 0x16,
723
- ]);
724
-
725
- this._inv_s = new Uint8Array([
726
- 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
727
- 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
728
- 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
729
- 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
730
- 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
731
- 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
732
- 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
733
- 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
734
- 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
735
- 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
736
- 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
737
- 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
738
- 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
739
- 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
740
- 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
741
- 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
742
- 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
743
- 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
744
- 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
745
- 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
746
- 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
747
- 0x55, 0x21, 0x0c, 0x7d,
748
- ]);
749
-
750
- this._mix = new Uint32Array([
751
- 0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927,
752
- 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45,
753
- 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb,
754
- 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381,
755
- 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf,
756
- 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66,
757
- 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28,
758
- 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012,
759
- 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec,
760
- 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e,
761
- 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd,
762
- 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7,
763
- 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89,
764
- 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b,
765
- 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815,
766
- 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f,
767
- 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa,
768
- 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8,
769
- 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36,
770
- 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c,
771
- 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742,
772
- 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea,
773
- 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4,
774
- 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e,
775
- 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360,
776
- 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502,
777
- 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87,
778
- 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd,
779
- 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3,
780
- 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621,
781
- 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f,
782
- 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55,
783
- 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26,
784
- 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844,
785
- 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba,
786
- 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480,
787
- 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce,
788
- 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67,
789
- 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929,
790
- 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713,
791
- 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed,
792
- 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f,
793
- 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3,
794
- ]);
795
-
796
- this._mixCol = new Uint8Array(256);
797
- for (let i = 0; i < 256; i++) {
798
- if (i < 128) {
799
- this._mixCol[i] = i << 1;
800
- } else {
801
- this._mixCol[i] = (i << 1) ^ 0x1b;
802
- }
803
- }
804
-
805
- this.buffer = new Uint8Array(16);
806
- this.bufferPosition = 0;
807
- }
808
-
809
- _expandKey(cipherKey) {
810
- unreachable("Cannot call `_expandKey` on the base class");
811
- }
812
-
813
- _decrypt(input, key) {
814
- let t, u, v;
815
- const state = new Uint8Array(16);
816
- state.set(input);
817
-
818
- // AddRoundKey
819
- for (let j = 0, k = this._keySize; j < 16; ++j, ++k) {
820
- state[j] ^= key[k];
821
- }
822
- for (let i = this._cyclesOfRepetition - 1; i >= 1; --i) {
823
- // InvShiftRows
824
- t = state[13];
825
- state[13] = state[9];
826
- state[9] = state[5];
827
- state[5] = state[1];
828
- state[1] = t;
829
- t = state[14];
830
- u = state[10];
831
- state[14] = state[6];
832
- state[10] = state[2];
833
- state[6] = t;
834
- state[2] = u;
835
- t = state[15];
836
- u = state[11];
837
- v = state[7];
838
- state[15] = state[3];
839
- state[11] = t;
840
- state[7] = u;
841
- state[3] = v;
842
- // InvSubBytes
843
- for (let j = 0; j < 16; ++j) {
844
- state[j] = this._inv_s[state[j]];
845
- }
846
- // AddRoundKey
847
- for (let j = 0, k = i * 16; j < 16; ++j, ++k) {
848
- state[j] ^= key[k];
849
- }
850
- // InvMixColumns
851
- for (let j = 0; j < 16; j += 4) {
852
- const s0 = this._mix[state[j]];
853
- const s1 = this._mix[state[j + 1]];
854
- const s2 = this._mix[state[j + 2]];
855
- const s3 = this._mix[state[j + 3]];
856
- t =
857
- s0 ^
858
- (s1 >>> 8) ^
859
- (s1 << 24) ^
860
- (s2 >>> 16) ^
861
- (s2 << 16) ^
862
- (s3 >>> 24) ^
863
- (s3 << 8);
864
- state[j] = (t >>> 24) & 0xff;
865
- state[j + 1] = (t >> 16) & 0xff;
866
- state[j + 2] = (t >> 8) & 0xff;
867
- state[j + 3] = t & 0xff;
868
- }
869
- }
870
- // InvShiftRows
871
- t = state[13];
872
- state[13] = state[9];
873
- state[9] = state[5];
874
- state[5] = state[1];
875
- state[1] = t;
876
- t = state[14];
877
- u = state[10];
878
- state[14] = state[6];
879
- state[10] = state[2];
880
- state[6] = t;
881
- state[2] = u;
882
- t = state[15];
883
- u = state[11];
884
- v = state[7];
885
- state[15] = state[3];
886
- state[11] = t;
887
- state[7] = u;
888
- state[3] = v;
889
- for (let j = 0; j < 16; ++j) {
890
- // InvSubBytes
891
- state[j] = this._inv_s[state[j]];
892
- // AddRoundKey
893
- state[j] ^= key[j];
894
- }
895
- return state;
896
- }
897
-
898
- _encrypt(input, key) {
899
- const s = this._s;
900
-
901
- let t, u, v;
902
- const state = new Uint8Array(16);
903
- state.set(input);
904
-
905
- for (let j = 0; j < 16; ++j) {
906
- // AddRoundKey
907
- state[j] ^= key[j];
908
- }
909
-
910
- for (let i = 1; i < this._cyclesOfRepetition; i++) {
911
- // SubBytes
912
- for (let j = 0; j < 16; ++j) {
913
- state[j] = s[state[j]];
914
- }
915
- // ShiftRows
916
- v = state[1];
917
- state[1] = state[5];
918
- state[5] = state[9];
919
- state[9] = state[13];
920
- state[13] = v;
921
- v = state[2];
922
- u = state[6];
923
- state[2] = state[10];
924
- state[6] = state[14];
925
- state[10] = v;
926
- state[14] = u;
927
- v = state[3];
928
- u = state[7];
929
- t = state[11];
930
- state[3] = state[15];
931
- state[7] = v;
932
- state[11] = u;
933
- state[15] = t;
934
- // MixColumns
935
- for (let j = 0; j < 16; j += 4) {
936
- const s0 = state[j + 0];
937
- const s1 = state[j + 1];
938
- const s2 = state[j + 2];
939
- const s3 = state[j + 3];
940
- t = s0 ^ s1 ^ s2 ^ s3;
941
- state[j + 0] ^= t ^ this._mixCol[s0 ^ s1];
942
- state[j + 1] ^= t ^ this._mixCol[s1 ^ s2];
943
- state[j + 2] ^= t ^ this._mixCol[s2 ^ s3];
944
- state[j + 3] ^= t ^ this._mixCol[s3 ^ s0];
945
- }
946
- // AddRoundKey
947
- for (let j = 0, k = i * 16; j < 16; ++j, ++k) {
948
- state[j] ^= key[k];
949
- }
950
- }
951
-
952
- // SubBytes
953
- for (let j = 0; j < 16; ++j) {
954
- state[j] = s[state[j]];
955
- }
956
- // ShiftRows
957
- v = state[1];
958
- state[1] = state[5];
959
- state[5] = state[9];
960
- state[9] = state[13];
961
- state[13] = v;
962
- v = state[2];
963
- u = state[6];
964
- state[2] = state[10];
965
- state[6] = state[14];
966
- state[10] = v;
967
- state[14] = u;
968
- v = state[3];
969
- u = state[7];
970
- t = state[11];
971
- state[3] = state[15];
972
- state[7] = v;
973
- state[11] = u;
974
- state[15] = t;
975
- // AddRoundKey
976
- for (let j = 0, k = this._keySize; j < 16; ++j, ++k) {
977
- state[j] ^= key[k];
978
- }
979
- return state;
980
- }
981
-
982
- _decryptBlock2(data, finalize) {
983
- const sourceLength = data.length;
984
- let buffer = this.buffer,
985
- bufferLength = this.bufferPosition;
986
- const result = [];
987
- let iv = this.iv;
988
-
989
- for (let i = 0; i < sourceLength; ++i) {
990
- buffer[bufferLength] = data[i];
991
- ++bufferLength;
992
- if (bufferLength < 16) {
993
- continue;
994
- }
995
- // buffer is full, decrypting
996
- const plain = this._decrypt(buffer, this._key);
997
- // xor-ing the IV vector to get plain text
998
- for (let j = 0; j < 16; ++j) {
999
- plain[j] ^= iv[j];
1000
- }
1001
- iv = buffer;
1002
- result.push(plain);
1003
- buffer = new Uint8Array(16);
1004
- bufferLength = 0;
1005
- }
1006
- // saving incomplete buffer
1007
- this.buffer = buffer;
1008
- this.bufferLength = bufferLength;
1009
- this.iv = iv;
1010
- if (result.length === 0) {
1011
- return new Uint8Array(0);
1012
- }
1013
- // combining plain text blocks into one
1014
- let outputLength = 16 * result.length;
1015
- if (finalize) {
1016
- // undo a padding that is described in RFC 2898
1017
- const lastBlock = result[result.length - 1];
1018
- let psLen = lastBlock[15];
1019
- if (psLen <= 16) {
1020
- for (let i = 15, ii = 16 - psLen; i >= ii; --i) {
1021
- if (lastBlock[i] !== psLen) {
1022
- // Invalid padding, assume that the block has no padding.
1023
- psLen = 0;
1024
- break;
1025
- }
1026
- }
1027
- outputLength -= psLen;
1028
- result[result.length - 1] = lastBlock.subarray(0, 16 - psLen);
1029
- }
1030
- }
1031
- const output = new Uint8Array(outputLength);
1032
- for (let i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
1033
- output.set(result[i], j);
1034
- }
1035
- return output;
1036
- }
1037
-
1038
- decryptBlock(data, finalize, iv = null) {
1039
- const sourceLength = data.length;
1040
- const buffer = this.buffer;
1041
- let bufferLength = this.bufferPosition;
1042
- // If an IV is not supplied, wait for IV values. They are at the start
1043
- // of the stream.
1044
- if (iv) {
1045
- this.iv = iv;
1046
- } else {
1047
- for (
1048
- let i = 0;
1049
- bufferLength < 16 && i < sourceLength;
1050
- ++i, ++bufferLength
1051
- ) {
1052
- buffer[bufferLength] = data[i];
1053
- }
1054
- if (bufferLength < 16) {
1055
- // Need more data.
1056
- this.bufferLength = bufferLength;
1057
- return new Uint8Array(0);
1058
- }
1059
- this.iv = buffer;
1060
- data = data.subarray(16);
1061
- }
1062
- this.buffer = new Uint8Array(16);
1063
- this.bufferLength = 0;
1064
- // starting decryption
1065
- this.decryptBlock = this._decryptBlock2;
1066
- return this.decryptBlock(data, finalize);
1067
- }
1068
-
1069
- encrypt(data, iv) {
1070
- const sourceLength = data.length;
1071
- let buffer = this.buffer,
1072
- bufferLength = this.bufferPosition;
1073
- const result = [];
1074
-
1075
- if (!iv) {
1076
- iv = new Uint8Array(16);
1077
- }
1078
- for (let i = 0; i < sourceLength; ++i) {
1079
- buffer[bufferLength] = data[i];
1080
- ++bufferLength;
1081
- if (bufferLength < 16) {
1082
- continue;
1083
- }
1084
-
1085
- for (let j = 0; j < 16; ++j) {
1086
- buffer[j] ^= iv[j];
1087
- }
1088
-
1089
- // buffer is full, encrypting
1090
- const cipher = this._encrypt(buffer, this._key);
1091
- iv = cipher;
1092
- result.push(cipher);
1093
- buffer = new Uint8Array(16);
1094
- bufferLength = 0;
1095
- }
1096
- // saving incomplete buffer
1097
- this.buffer = buffer;
1098
- this.bufferLength = bufferLength;
1099
- this.iv = iv;
1100
- if (result.length === 0) {
1101
- return new Uint8Array(0);
1102
- }
1103
- // combining plain text blocks into one
1104
- const outputLength = 16 * result.length;
1105
- const output = new Uint8Array(outputLength);
1106
- for (let i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
1107
- output.set(result[i], j);
1108
- }
1109
- return output;
1110
- }
1111
- }
1112
-
1113
- class AES128Cipher extends AESBaseCipher {
1114
- constructor(key) {
1115
- super();
1116
-
1117
- this._cyclesOfRepetition = 10;
1118
- this._keySize = 160; // bits
1119
-
1120
- this._rcon = new Uint8Array([
1121
- 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
1122
- 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
1123
- 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
1124
- 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
1125
- 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
1126
- 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6,
1127
- 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72,
1128
- 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
1129
- 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10,
1130
- 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e,
1131
- 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5,
1132
- 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
1133
- 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02,
1134
- 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d,
1135
- 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d,
1136
- 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
1137
- 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb,
1138
- 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
1139
- 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
1140
- 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
1141
- 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
1142
- 0x74, 0xe8, 0xcb, 0x8d,
1143
- ]);
1144
-
1145
- this._key = this._expandKey(key);
1146
- }
1147
-
1148
- _expandKey(cipherKey) {
1149
- const b = 176;
1150
- const s = this._s;
1151
- const rcon = this._rcon;
1152
-
1153
- const result = new Uint8Array(b);
1154
- result.set(cipherKey);
1155
-
1156
- for (let j = 16, i = 1; j < b; ++i) {
1157
- // RotWord
1158
- let t1 = result[j - 3];
1159
- let t2 = result[j - 2];
1160
- let t3 = result[j - 1];
1161
- let t4 = result[j - 4];
1162
- // SubWord
1163
- t1 = s[t1];
1164
- t2 = s[t2];
1165
- t3 = s[t3];
1166
- t4 = s[t4];
1167
- // Rcon
1168
- t1 ^= rcon[i];
1169
- for (let n = 0; n < 4; ++n) {
1170
- result[j] = t1 ^= result[j - 16];
1171
- j++;
1172
- result[j] = t2 ^= result[j - 16];
1173
- j++;
1174
- result[j] = t3 ^= result[j - 16];
1175
- j++;
1176
- result[j] = t4 ^= result[j - 16];
1177
- j++;
1178
- }
1179
- }
1180
- return result;
1181
- }
1182
- }
1183
-
1184
- class AES256Cipher extends AESBaseCipher {
1185
- constructor(key) {
1186
- super();
1187
-
1188
- this._cyclesOfRepetition = 14;
1189
- this._keySize = 224; // bits
1190
-
1191
- this._key = this._expandKey(key);
1192
- }
1193
-
1194
- _expandKey(cipherKey) {
1195
- const b = 240;
1196
- const s = this._s;
1197
-
1198
- const result = new Uint8Array(b);
1199
- result.set(cipherKey);
1200
-
1201
- let r = 1;
1202
- let t1, t2, t3, t4;
1203
- for (let j = 32, i = 1; j < b; ++i) {
1204
- if (j % 32 === 16) {
1205
- t1 = s[t1];
1206
- t2 = s[t2];
1207
- t3 = s[t3];
1208
- t4 = s[t4];
1209
- } else if (j % 32 === 0) {
1210
- // RotWord
1211
- t1 = result[j - 3];
1212
- t2 = result[j - 2];
1213
- t3 = result[j - 1];
1214
- t4 = result[j - 4];
1215
- // SubWord
1216
- t1 = s[t1];
1217
- t2 = s[t2];
1218
- t3 = s[t3];
1219
- t4 = s[t4];
1220
- // Rcon
1221
- t1 ^= r;
1222
- if ((r <<= 1) >= 256) {
1223
- r = (r ^ 0x1b) & 0xff;
1224
- }
1225
- }
1226
-
1227
- for (let n = 0; n < 4; ++n) {
1228
- result[j] = t1 ^= result[j - 32];
1229
- j++;
1230
- result[j] = t2 ^= result[j - 32];
1231
- j++;
1232
- result[j] = t3 ^= result[j - 32];
1233
- j++;
1234
- result[j] = t4 ^= result[j - 32];
1235
- j++;
1236
- }
1237
- }
1238
- return result;
1239
- }
1240
- }
1241
-
1242
- class PDF17 {
1243
- checkOwnerPassword(password, ownerValidationSalt, userBytes, ownerPassword) {
1244
- const hashData = new Uint8Array(password.length + 56);
1245
- hashData.set(password, 0);
1246
- hashData.set(ownerValidationSalt, password.length);
1247
- hashData.set(userBytes, password.length + ownerValidationSalt.length);
1248
- const result = calculateSHA256(hashData, 0, hashData.length);
1249
- return isArrayEqual(result, ownerPassword);
1250
- }
1251
-
1252
- checkUserPassword(password, userValidationSalt, userPassword) {
1253
- const hashData = new Uint8Array(password.length + 8);
1254
- hashData.set(password, 0);
1255
- hashData.set(userValidationSalt, password.length);
1256
- const result = calculateSHA256(hashData, 0, hashData.length);
1257
- return isArrayEqual(result, userPassword);
1258
- }
1259
-
1260
- getOwnerKey(password, ownerKeySalt, userBytes, ownerEncryption) {
1261
- const hashData = new Uint8Array(password.length + 56);
1262
- hashData.set(password, 0);
1263
- hashData.set(ownerKeySalt, password.length);
1264
- hashData.set(userBytes, password.length + ownerKeySalt.length);
1265
- const key = calculateSHA256(hashData, 0, hashData.length);
1266
- const cipher = new AES256Cipher(key);
1267
- return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
1268
- }
1269
-
1270
- getUserKey(password, userKeySalt, userEncryption) {
1271
- const hashData = new Uint8Array(password.length + 8);
1272
- hashData.set(password, 0);
1273
- hashData.set(userKeySalt, password.length);
1274
- // `key` is the decryption key for the UE string.
1275
- const key = calculateSHA256(hashData, 0, hashData.length);
1276
- const cipher = new AES256Cipher(key);
1277
- return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
1278
- }
1279
- }
1280
-
1281
- const PDF20 = (function PDF20Closure() {
1282
- function calculatePDF20Hash(password, input, userBytes) {
1283
- // This refers to Algorithm 2.B as defined in ISO 32000-2.
1284
- let k = calculateSHA256(input, 0, input.length).subarray(0, 32);
1285
- let e = [0];
1286
- let i = 0;
1287
- while (i < 64 || e[e.length - 1] > i - 32) {
1288
- const combinedLength = password.length + k.length + userBytes.length,
1289
- combinedArray = new Uint8Array(combinedLength);
1290
- let writeOffset = 0;
1291
- combinedArray.set(password, writeOffset);
1292
- writeOffset += password.length;
1293
- combinedArray.set(k, writeOffset);
1294
- writeOffset += k.length;
1295
- combinedArray.set(userBytes, writeOffset);
1296
-
1297
- const k1 = new Uint8Array(combinedLength * 64);
1298
- for (let j = 0, pos = 0; j < 64; j++, pos += combinedLength) {
1299
- k1.set(combinedArray, pos);
1300
- }
1301
- // AES128 CBC NO PADDING with first 16 bytes of k as the key
1302
- // and the second 16 as the iv.
1303
- const cipher = new AES128Cipher(k.subarray(0, 16));
1304
- e = cipher.encrypt(k1, k.subarray(16, 32));
1305
- // Now we have to take the first 16 bytes of an unsigned big endian
1306
- // integer and compute the remainder modulo 3. That is a fairly large
1307
- // number and JavaScript isn't going to handle that well, so we're using
1308
- // a trick that allows us to perform modulo math byte by byte.
1309
- let remainder = 0;
1310
- for (let z = 0; z < 16; z++) {
1311
- remainder *= 256 % 3;
1312
- remainder %= 3;
1313
- remainder += (e[z] >>> 0) % 3;
1314
- remainder %= 3;
1315
- }
1316
- if (remainder === 0) {
1317
- k = calculateSHA256(e, 0, e.length);
1318
- } else if (remainder === 1) {
1319
- k = calculateSHA384(e, 0, e.length);
1320
- } else if (remainder === 2) {
1321
- k = calculateSHA512(e, 0, e.length);
1322
- }
1323
- i++;
1324
- }
1325
- return k.subarray(0, 32);
1326
- }
1327
-
1328
- // eslint-disable-next-line no-shadow
1329
- class PDF20 {
1330
- hash(password, concatBytes, userBytes) {
1331
- return calculatePDF20Hash(password, concatBytes, userBytes);
1332
- }
1333
-
1334
- checkOwnerPassword(
1335
- password,
1336
- ownerValidationSalt,
1337
- userBytes,
1338
- ownerPassword
1339
- ) {
1340
- const hashData = new Uint8Array(password.length + 56);
1341
- hashData.set(password, 0);
1342
- hashData.set(ownerValidationSalt, password.length);
1343
- hashData.set(userBytes, password.length + ownerValidationSalt.length);
1344
- const result = calculatePDF20Hash(password, hashData, userBytes);
1345
- return isArrayEqual(result, ownerPassword);
1346
- }
1347
-
1348
- checkUserPassword(password, userValidationSalt, userPassword) {
1349
- const hashData = new Uint8Array(password.length + 8);
1350
- hashData.set(password, 0);
1351
- hashData.set(userValidationSalt, password.length);
1352
- const result = calculatePDF20Hash(password, hashData, []);
1353
- return isArrayEqual(result, userPassword);
1354
- }
1355
-
1356
- getOwnerKey(password, ownerKeySalt, userBytes, ownerEncryption) {
1357
- const hashData = new Uint8Array(password.length + 56);
1358
- hashData.set(password, 0);
1359
- hashData.set(ownerKeySalt, password.length);
1360
- hashData.set(userBytes, password.length + ownerKeySalt.length);
1361
- const key = calculatePDF20Hash(password, hashData, userBytes);
1362
- const cipher = new AES256Cipher(key);
1363
- return cipher.decryptBlock(ownerEncryption, false, new Uint8Array(16));
1364
- }
1365
-
1366
- getUserKey(password, userKeySalt, userEncryption) {
1367
- const hashData = new Uint8Array(password.length + 8);
1368
- hashData.set(password, 0);
1369
- hashData.set(userKeySalt, password.length);
1370
- // `key` is the decryption key for the UE string.
1371
- const key = calculatePDF20Hash(password, hashData, []);
1372
- const cipher = new AES256Cipher(key);
1373
- return cipher.decryptBlock(userEncryption, false, new Uint8Array(16));
1374
- }
1375
- }
1376
-
1377
- return PDF20;
1378
- })();
1379
-
1380
- class CipherTransform {
1381
- constructor(stringCipherConstructor, streamCipherConstructor) {
1382
- this.StringCipherConstructor = stringCipherConstructor;
1383
- this.StreamCipherConstructor = streamCipherConstructor;
1384
- }
1385
-
1386
- createStream(stream, length) {
1387
- const cipher = new this.StreamCipherConstructor();
1388
- return new DecryptStream(
1389
- stream,
1390
- length,
1391
- function cipherTransformDecryptStream(data, finalize) {
1392
- return cipher.decryptBlock(data, finalize);
1393
- }
1394
- );
1395
- }
1396
-
1397
- decryptString(s) {
1398
- const cipher = new this.StringCipherConstructor();
1399
- let data = stringToBytes(s);
1400
- data = cipher.decryptBlock(data, true);
1401
- return bytesToString(data);
1402
- }
1403
-
1404
- encryptString(s) {
1405
- const cipher = new this.StringCipherConstructor();
1406
- if (cipher instanceof AESBaseCipher) {
1407
- // Append some chars equal to "16 - (M mod 16)"
1408
- // where M is the string length (see section 7.6.2 in PDF specification)
1409
- // to have a final string where the length is a multiple of 16.
1410
- const strLen = s.length;
1411
- const pad = 16 - (strLen % 16);
1412
- if (pad !== 16) {
1413
- s = s.padEnd(16 * Math.ceil(strLen / 16), String.fromCharCode(pad));
1414
- }
1415
-
1416
- // Generate an initialization vector
1417
- const iv = new Uint8Array(16);
1418
- if (typeof crypto !== "undefined") {
1419
- crypto.getRandomValues(iv);
1420
- } else {
1421
- for (let i = 0; i < 16; i++) {
1422
- iv[i] = Math.floor(256 * Math.random());
1423
- }
1424
- }
1425
-
1426
- let data = stringToBytes(s);
1427
- data = cipher.encrypt(data, iv);
1428
-
1429
- const buf = new Uint8Array(16 + data.length);
1430
- buf.set(iv);
1431
- buf.set(data, 16);
1432
-
1433
- return bytesToString(buf);
1434
- }
1435
-
1436
- let data = stringToBytes(s);
1437
- data = cipher.encrypt(data);
1438
- return bytesToString(data);
1439
- }
1440
- }
1441
-
1442
- const CipherTransformFactory = (function CipherTransformFactoryClosure() {
1443
- const defaultPasswordBytes = new Uint8Array([
1444
- 0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, 0x56,
1445
- 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80,
1446
- 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a,
1447
- ]);
1448
-
1449
- function createEncryptionKey20(
1450
- revision,
1451
- password,
1452
- ownerPassword,
1453
- ownerValidationSalt,
1454
- ownerKeySalt,
1455
- uBytes,
1456
- userPassword,
1457
- userValidationSalt,
1458
- userKeySalt,
1459
- ownerEncryption,
1460
- userEncryption,
1461
- perms
1462
- ) {
1463
- if (password) {
1464
- const passwordLength = Math.min(127, password.length);
1465
- password = password.subarray(0, passwordLength);
1466
- } else {
1467
- password = [];
1468
- }
1469
- let pdfAlgorithm;
1470
- if (revision === 6) {
1471
- pdfAlgorithm = new PDF20();
1472
- } else {
1473
- pdfAlgorithm = new PDF17();
1474
- }
1475
-
1476
- if (
1477
- pdfAlgorithm.checkUserPassword(password, userValidationSalt, userPassword)
1478
- ) {
1479
- return pdfAlgorithm.getUserKey(password, userKeySalt, userEncryption);
1480
- } else if (
1481
- password.length &&
1482
- pdfAlgorithm.checkOwnerPassword(
1483
- password,
1484
- ownerValidationSalt,
1485
- uBytes,
1486
- ownerPassword
1487
- )
1488
- ) {
1489
- return pdfAlgorithm.getOwnerKey(
1490
- password,
1491
- ownerKeySalt,
1492
- uBytes,
1493
- ownerEncryption
1494
- );
1495
- }
1496
-
1497
- return null;
1498
- }
1499
-
1500
- function prepareKeyData(
1501
- fileId,
1502
- password,
1503
- ownerPassword,
1504
- userPassword,
1505
- flags,
1506
- revision,
1507
- keyLength,
1508
- encryptMetadata
1509
- ) {
1510
- const hashDataSize = 40 + ownerPassword.length + fileId.length;
1511
- const hashData = new Uint8Array(hashDataSize);
1512
- let i = 0,
1513
- j,
1514
- n;
1515
- if (password) {
1516
- n = Math.min(32, password.length);
1517
- for (; i < n; ++i) {
1518
- hashData[i] = password[i];
1519
- }
1520
- }
1521
- j = 0;
1522
- while (i < 32) {
1523
- hashData[i++] = defaultPasswordBytes[j++];
1524
- }
1525
- // as now the padded password in the hashData[0..i]
1526
- for (j = 0, n = ownerPassword.length; j < n; ++j) {
1527
- hashData[i++] = ownerPassword[j];
1528
- }
1529
- hashData[i++] = flags & 0xff;
1530
- hashData[i++] = (flags >> 8) & 0xff;
1531
- hashData[i++] = (flags >> 16) & 0xff;
1532
- hashData[i++] = (flags >>> 24) & 0xff;
1533
- for (j = 0, n = fileId.length; j < n; ++j) {
1534
- hashData[i++] = fileId[j];
1535
- }
1536
- if (revision >= 4 && !encryptMetadata) {
1537
- hashData[i++] = 0xff;
1538
- hashData[i++] = 0xff;
1539
- hashData[i++] = 0xff;
1540
- hashData[i++] = 0xff;
1541
- }
1542
- let hash = calculateMD5(hashData, 0, i);
1543
- const keyLengthInBytes = keyLength >> 3;
1544
- if (revision >= 3) {
1545
- for (j = 0; j < 50; ++j) {
1546
- hash = calculateMD5(hash, 0, keyLengthInBytes);
1547
- }
1548
- }
1549
- const encryptionKey = hash.subarray(0, keyLengthInBytes);
1550
- let cipher, checkData;
1551
-
1552
- if (revision >= 3) {
1553
- for (i = 0; i < 32; ++i) {
1554
- hashData[i] = defaultPasswordBytes[i];
1555
- }
1556
- for (j = 0, n = fileId.length; j < n; ++j) {
1557
- hashData[i++] = fileId[j];
1558
- }
1559
- cipher = new ARCFourCipher(encryptionKey);
1560
- checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i));
1561
- n = encryptionKey.length;
1562
- const derivedKey = new Uint8Array(n);
1563
- for (j = 1; j <= 19; ++j) {
1564
- for (let k = 0; k < n; ++k) {
1565
- derivedKey[k] = encryptionKey[k] ^ j;
1566
- }
1567
- cipher = new ARCFourCipher(derivedKey);
1568
- checkData = cipher.encryptBlock(checkData);
1569
- }
1570
- for (j = 0, n = checkData.length; j < n; ++j) {
1571
- if (userPassword[j] !== checkData[j]) {
1572
- return null;
1573
- }
1574
- }
1575
- } else {
1576
- cipher = new ARCFourCipher(encryptionKey);
1577
- checkData = cipher.encryptBlock(defaultPasswordBytes);
1578
- for (j = 0, n = checkData.length; j < n; ++j) {
1579
- if (userPassword[j] !== checkData[j]) {
1580
- return null;
1581
- }
1582
- }
1583
- }
1584
- return encryptionKey;
1585
- }
1586
-
1587
- function decodeUserPassword(password, ownerPassword, revision, keyLength) {
1588
- const hashData = new Uint8Array(32);
1589
- let i = 0;
1590
- const n = Math.min(32, password.length);
1591
- for (; i < n; ++i) {
1592
- hashData[i] = password[i];
1593
- }
1594
- let j = 0;
1595
- while (i < 32) {
1596
- hashData[i++] = defaultPasswordBytes[j++];
1597
- }
1598
- let hash = calculateMD5(hashData, 0, i);
1599
- const keyLengthInBytes = keyLength >> 3;
1600
- if (revision >= 3) {
1601
- for (j = 0; j < 50; ++j) {
1602
- hash = calculateMD5(hash, 0, hash.length);
1603
- }
1604
- }
1605
-
1606
- let cipher, userPassword;
1607
- if (revision >= 3) {
1608
- userPassword = ownerPassword;
1609
- const derivedKey = new Uint8Array(keyLengthInBytes);
1610
- for (j = 19; j >= 0; j--) {
1611
- for (let k = 0; k < keyLengthInBytes; ++k) {
1612
- derivedKey[k] = hash[k] ^ j;
1613
- }
1614
- cipher = new ARCFourCipher(derivedKey);
1615
- userPassword = cipher.encryptBlock(userPassword);
1616
- }
1617
- } else {
1618
- cipher = new ARCFourCipher(hash.subarray(0, keyLengthInBytes));
1619
- userPassword = cipher.encryptBlock(ownerPassword);
1620
- }
1621
- return userPassword;
1622
- }
1623
-
1624
- const identityName = Name.get("Identity");
1625
-
1626
- function buildObjectKey(num, gen, encryptionKey, isAes = false) {
1627
- const key = new Uint8Array(encryptionKey.length + 9);
1628
- const n = encryptionKey.length;
1629
- let i;
1630
- for (i = 0; i < n; ++i) {
1631
- key[i] = encryptionKey[i];
1632
- }
1633
- key[i++] = num & 0xff;
1634
- key[i++] = (num >> 8) & 0xff;
1635
- key[i++] = (num >> 16) & 0xff;
1636
- key[i++] = gen & 0xff;
1637
- key[i++] = (gen >> 8) & 0xff;
1638
- if (isAes) {
1639
- key[i++] = 0x73;
1640
- key[i++] = 0x41;
1641
- key[i++] = 0x6c;
1642
- key[i++] = 0x54;
1643
- }
1644
- const hash = calculateMD5(key, 0, i);
1645
- return hash.subarray(0, Math.min(encryptionKey.length + 5, 16));
1646
- }
1647
-
1648
- function buildCipherConstructor(cf, name, num, gen, key) {
1649
- if (!isName(name)) {
1650
- throw new FormatError("Invalid crypt filter name.");
1651
- }
1652
- const cryptFilter = cf.get(name.name);
1653
- let cfm;
1654
- if (cryptFilter !== null && cryptFilter !== undefined) {
1655
- cfm = cryptFilter.get("CFM");
1656
- }
1657
- if (!cfm || cfm.name === "None") {
1658
- return function cipherTransformFactoryBuildCipherConstructorNone() {
1659
- return new NullCipher();
1660
- };
1661
- }
1662
- if (cfm.name === "V2") {
1663
- return function cipherTransformFactoryBuildCipherConstructorV2() {
1664
- return new ARCFourCipher(
1665
- buildObjectKey(num, gen, key, /* isAes = */ false)
1666
- );
1667
- };
1668
- }
1669
- if (cfm.name === "AESV2") {
1670
- return function cipherTransformFactoryBuildCipherConstructorAESV2() {
1671
- return new AES128Cipher(
1672
- buildObjectKey(num, gen, key, /* isAes = */ true)
1673
- );
1674
- };
1675
- }
1676
- if (cfm.name === "AESV3") {
1677
- return function cipherTransformFactoryBuildCipherConstructorAESV3() {
1678
- return new AES256Cipher(key);
1679
- };
1680
- }
1681
- throw new FormatError("Unknown crypto method");
1682
- }
1683
-
1684
- // eslint-disable-next-line no-shadow
1685
- class CipherTransformFactory {
1686
- constructor(dict, fileId, password) {
1687
- const filter = dict.get("Filter");
1688
- if (!isName(filter, "Standard")) {
1689
- throw new FormatError("unknown encryption method");
1690
- }
1691
- this.dict = dict;
1692
- const algorithm = dict.get("V");
1693
- if (
1694
- !Number.isInteger(algorithm) ||
1695
- (algorithm !== 1 &&
1696
- algorithm !== 2 &&
1697
- algorithm !== 4 &&
1698
- algorithm !== 5)
1699
- ) {
1700
- throw new FormatError("unsupported encryption algorithm");
1701
- }
1702
- this.algorithm = algorithm;
1703
- let keyLength = dict.get("Length");
1704
- if (!keyLength) {
1705
- // Spec asks to rely on encryption dictionary's Length entry, however
1706
- // some PDFs don't have it. Trying to recover.
1707
- if (algorithm <= 3) {
1708
- // For 1 and 2 it's fixed to 40-bit, for 3 40-bit is a minimal value.
1709
- keyLength = 40;
1710
- } else {
1711
- // Trying to find default handler -- it usually has Length.
1712
- const cfDict = dict.get("CF");
1713
- const streamCryptoName = dict.get("StmF");
1714
- if (isDict(cfDict) && isName(streamCryptoName)) {
1715
- cfDict.suppressEncryption = true; // See comment below.
1716
- const handlerDict = cfDict.get(streamCryptoName.name);
1717
- keyLength = (handlerDict && handlerDict.get("Length")) || 128;
1718
- if (keyLength < 40) {
1719
- // Sometimes it's incorrect value of bits, generators specify
1720
- // bytes.
1721
- keyLength <<= 3;
1722
- }
1723
- }
1724
- }
1725
- }
1726
- if (
1727
- !Number.isInteger(keyLength) ||
1728
- keyLength < 40 ||
1729
- keyLength % 8 !== 0
1730
- ) {
1731
- throw new FormatError("invalid key length");
1732
- }
1733
-
1734
- // prepare keys
1735
- const ownerPassword = stringToBytes(dict.get("O")).subarray(0, 32);
1736
- const userPassword = stringToBytes(dict.get("U")).subarray(0, 32);
1737
- const flags = dict.get("P");
1738
- const revision = dict.get("R");
1739
- // meaningful when V is 4 or 5
1740
- const encryptMetadata =
1741
- (algorithm === 4 || algorithm === 5) &&
1742
- dict.get("EncryptMetadata") !== false;
1743
- this.encryptMetadata = encryptMetadata;
1744
-
1745
- const fileIdBytes = stringToBytes(fileId);
1746
- let passwordBytes;
1747
- if (password) {
1748
- if (revision === 6) {
1749
- try {
1750
- password = utf8StringToString(password);
1751
- } catch (ex) {
1752
- warn(
1753
- "CipherTransformFactory: " +
1754
- "Unable to convert UTF8 encoded password."
1755
- );
1756
- }
1757
- }
1758
- passwordBytes = stringToBytes(password);
1759
- }
1760
-
1761
- let encryptionKey;
1762
- if (algorithm !== 5) {
1763
- encryptionKey = prepareKeyData(
1764
- fileIdBytes,
1765
- passwordBytes,
1766
- ownerPassword,
1767
- userPassword,
1768
- flags,
1769
- revision,
1770
- keyLength,
1771
- encryptMetadata
1772
- );
1773
- } else {
1774
- const ownerValidationSalt = stringToBytes(dict.get("O")).subarray(
1775
- 32,
1776
- 40
1777
- );
1778
- const ownerKeySalt = stringToBytes(dict.get("O")).subarray(40, 48);
1779
- const uBytes = stringToBytes(dict.get("U")).subarray(0, 48);
1780
- const userValidationSalt = stringToBytes(dict.get("U")).subarray(
1781
- 32,
1782
- 40
1783
- );
1784
- const userKeySalt = stringToBytes(dict.get("U")).subarray(40, 48);
1785
- const ownerEncryption = stringToBytes(dict.get("OE"));
1786
- const userEncryption = stringToBytes(dict.get("UE"));
1787
- const perms = stringToBytes(dict.get("Perms"));
1788
- encryptionKey = createEncryptionKey20(
1789
- revision,
1790
- passwordBytes,
1791
- ownerPassword,
1792
- ownerValidationSalt,
1793
- ownerKeySalt,
1794
- uBytes,
1795
- userPassword,
1796
- userValidationSalt,
1797
- userKeySalt,
1798
- ownerEncryption,
1799
- userEncryption,
1800
- perms
1801
- );
1802
- }
1803
- if (!encryptionKey && !password) {
1804
- throw new PasswordException(
1805
- "No password given",
1806
- PasswordResponses.NEED_PASSWORD
1807
- );
1808
- } else if (!encryptionKey && password) {
1809
- // Attempting use the password as an owner password
1810
- const decodedPassword = decodeUserPassword(
1811
- passwordBytes,
1812
- ownerPassword,
1813
- revision,
1814
- keyLength
1815
- );
1816
- encryptionKey = prepareKeyData(
1817
- fileIdBytes,
1818
- decodedPassword,
1819
- ownerPassword,
1820
- userPassword,
1821
- flags,
1822
- revision,
1823
- keyLength,
1824
- encryptMetadata
1825
- );
1826
- }
1827
-
1828
- if (!encryptionKey) {
1829
- throw new PasswordException(
1830
- "Incorrect Password",
1831
- PasswordResponses.INCORRECT_PASSWORD
1832
- );
1833
- }
1834
-
1835
- this.encryptionKey = encryptionKey;
1836
-
1837
- if (algorithm >= 4) {
1838
- const cf = dict.get("CF");
1839
- if (isDict(cf)) {
1840
- // The 'CF' dictionary itself should not be encrypted, and by setting
1841
- // `suppressEncryption` we can prevent an infinite loop inside of
1842
- // `XRef_fetchUncompressed` if the dictionary contains indirect
1843
- // objects (fixes issue7665.pdf).
1844
- cf.suppressEncryption = true;
1845
- }
1846
- this.cf = cf;
1847
- this.stmf = dict.get("StmF") || identityName;
1848
- this.strf = dict.get("StrF") || identityName;
1849
- this.eff = dict.get("EFF") || this.stmf;
1850
- }
1851
- }
1852
-
1853
- createCipherTransform(num, gen) {
1854
- if (this.algorithm === 4 || this.algorithm === 5) {
1855
- return new CipherTransform(
1856
- buildCipherConstructor(
1857
- this.cf,
1858
- this.stmf,
1859
- num,
1860
- gen,
1861
- this.encryptionKey
1862
- ),
1863
- buildCipherConstructor(
1864
- this.cf,
1865
- this.strf,
1866
- num,
1867
- gen,
1868
- this.encryptionKey
1869
- )
1870
- );
1871
- }
1872
- // algorithms 1 and 2
1873
- const key = buildObjectKey(
1874
- num,
1875
- gen,
1876
- this.encryptionKey,
1877
- /* isAes = */ false
1878
- );
1879
- const cipherConstructor = function buildCipherCipherConstructor() {
1880
- return new ARCFourCipher(key);
1881
- };
1882
- return new CipherTransform(cipherConstructor, cipherConstructor);
1883
- }
1884
- }
1885
-
1886
- return CipherTransformFactory;
1887
- })();
1888
-
1889
- export {
1890
- AES128Cipher,
1891
- AES256Cipher,
1892
- ARCFourCipher,
1893
- calculateMD5,
1894
- calculateSHA256,
1895
- calculateSHA384,
1896
- calculateSHA512,
1897
- CipherTransformFactory,
1898
- PDF17,
1899
- PDF20,
1900
- };