@@ -237,6 +237,32 @@ def fixed_iv(size):
237
237
assert_pdf_equal (pdf , HERE / "encryption_aes256.pdf" , tmp_path )
238
238
239
239
240
+ def test_encryption_aes256_with_user_password (tmp_path ):
241
+ pdf = FPDF ()
242
+
243
+ def custom_file_id ():
244
+ return pdf ._default_file_id (bytearray ([0xFF ]))
245
+
246
+ pdf .file_id = custom_file_id
247
+
248
+ def fixed_iv (size ):
249
+ return bytearray (size )
250
+
251
+ pdf .set_author ("author" )
252
+ pdf .set_subject ("string to be encrypted" )
253
+ pdf .add_page ()
254
+ pdf .set_font ("helvetica" , size = 12 )
255
+ pdf .cell (txt = "hello world" )
256
+ pdf .set_encryption (
257
+ owner_password = "fpdf2" ,
258
+ user_password = "1" * 1000 ,
259
+ encryption_method = EncryptionMethod .AES_256 ,
260
+ permissions = AccessPermission .all (),
261
+ )
262
+ pdf ._security_handler .get_random_bytes = fixed_iv
263
+ assert_pdf_equal (pdf , HERE / "encryption_aes256_user_password.pdf" , tmp_path )
264
+
265
+
240
266
def test_blank_owner_password (tmp_path ):
241
267
pdf = FPDF ()
242
268
pdf .set_encryption (
@@ -250,11 +276,12 @@ def test_blank_owner_password(tmp_path):
250
276
251
277
252
278
def test_password_prep ():
253
- # The PDF standard requires the passwords to be prepared using the stringprep algorithm
254
- # using the SASLprep as per RFC 4013
255
- # https://datatracker.ietf.org/doc/html/rfc4013
256
- # Those assertions are bases on the examples section of the RFC
257
- #
279
+ """
280
+ The PDF standard requires the passwords to be prepared using the stringprep algorithm
281
+ using the SASLprep as per RFC 4013
282
+ https://datatracker.ietf.org/doc/html/rfc4013
283
+ Those assertions are bases on the examples section of the RFC
284
+ """
258
285
assert sh .prepare_string ("I\xad X" ) == b"IX" # SOFT HYPHEN mapped to nothing
259
286
assert sh .prepare_string ("user" ) == b"user" # no transformation
260
287
assert sh .prepare_string ("USER" ) == b"USER" # case preserved
@@ -265,3 +292,4 @@ def test_password_prep():
265
292
assert str (e .value ) == "The password contains prohibited characters"
266
293
with pytest .raises (FPDFException ) as e :
267
294
sh .prepare_string ("\u0627 \x31 " ) # Error - bidirectional check
295
+ assert sh .prepare_string ("A" * 300 ) == b"A" * 127 # test cap 127 chars
0 commit comments