comparison tests/test_crypting.py @ 1:1d09e1dec1d9 upstream

ADD: PyMuPDF v1.26.4: the original sdist. It does not yet contain MuPDF. This normally will be downloaded when building PyMuPDF.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:37:51 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 1:1d09e1dec1d9
1 """
2 Check PDF encryption:
3 * make a PDF with owber and user passwords
4 * open and decrypt as owner or user
5 """
6 import pymupdf
7
8
9 def test_encryption():
10 text = "some secret information" # keep this data secret
11 perm = int(
12 pymupdf.PDF_PERM_ACCESSIBILITY # always use this
13 | pymupdf.PDF_PERM_PRINT # permit printing
14 | pymupdf.PDF_PERM_COPY # permit copying
15 | pymupdf.PDF_PERM_ANNOTATE # permit annotations
16 )
17 owner_pass = "owner" # owner password
18 user_pass = "user" # user password
19 encrypt_meth = pymupdf.PDF_ENCRYPT_AES_256 # strongest algorithm
20 doc = pymupdf.open() # empty pdf
21 page = doc.new_page() # empty page
22 page.insert_text((50, 72), text) # insert the data
23 tobytes = doc.tobytes(
24 encryption=encrypt_meth, # set the encryption method
25 owner_pw=owner_pass, # set the owner password
26 user_pw=user_pass, # set the user password
27 permissions=perm, # set permissions
28 )
29 doc.close()
30 doc = pymupdf.open("pdf", tobytes)
31 assert doc.needs_pass
32 assert doc.is_encrypted
33 rc = doc.authenticate("owner")
34 assert rc == 4
35 assert not doc.is_encrypted
36 doc.close()
37 doc = pymupdf.open("pdf", tobytes)
38 rc = doc.authenticate("user")
39 assert rc == 2