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