MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / test_encryption

Function test_encryption

tests/test_crypting.py:9–39  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7
8
9def 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

Callers

nothing calls this directly

Calls 5

new_pageMethod · 0.80
authenticateMethod · 0.80
insert_textMethod · 0.45
tobytesMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…