Skip to content

Commit

Permalink
MAINT: Remove private _security submodule (#1935)
Browse files Browse the repository at this point in the history
This PR was made to avoid breaking changes the original PR introduced.
We want to remove the outdated parameters, but not at the moment.
See https://pypdf.readthedocs.io/en/latest/dev/deprecations.html

Full credit for the work goes to exiledkingcc via PR #1816

Co-authored-by: exiledkingcc <exiledkingcc@gmail.com>
  • Loading branch information
MartinThoma and exiledkingcc authored Jul 2, 2023
1 parent 71be38b commit c3279f7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 380 deletions.
314 changes: 0 additions & 314 deletions pypdf/_security.py

This file was deleted.

36 changes: 12 additions & 24 deletions pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,8 @@ def original_bytes(self) -> bytes:
def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
bytearr = self
if encryption_key:
from .._security import RC4_encrypt

bytearr = RC4_encrypt(encryption_key, bytearr) # type: ignore
stream.write(b"<")
stream.write(binascii.hexlify(bytearr))
stream.write(binascii.hexlify(self))
stream.write(b">")

def writeToStream(
Expand Down Expand Up @@ -571,24 +566,17 @@ def write_to_stream(
self, stream: StreamType, encryption_key: Union[None, str, bytes] = None
) -> None:
bytearr = self.get_encoded_bytes()
if encryption_key:
from .._security import RC4_encrypt

bytearr = RC4_encrypt(encryption_key, bytearr)
obj = ByteStringObject(bytearr)
obj.write_to_stream(stream)
else:
stream.write(b"(")
for c in bytearr:
if not chr(c).isalnum() and c != b" ":
# This:
# stream.write(b_(rf"\{c:0>3o}"))
# gives
# https://github.com/davidhalter/parso/issues/207
stream.write(b_("\\%03o" % c))
else:
stream.write(b_(chr(c)))
stream.write(b")")
stream.write(b"(")
for c in bytearr:
if not chr(c).isalnum() and c != b" ":
# This:
# stream.write(b_(rf"\{c:0>3o}"))
# gives
# https://github.com/davidhalter/parso/issues/207
stream.write(b_("\\%03o" % c))
else:
stream.write(b_(chr(c)))
stream.write(b")")

def writeToStream(
self, stream: StreamType, encryption_key: Union[None, str, bytes]
Expand Down
7 changes: 1 addition & 6 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,12 +802,7 @@ def write_to_stream(
DictionaryObject.write_to_stream(self, stream, encryption_key)
del self[SA.LENGTH]
stream.write(b"\nstream\n")
data = self._data
if encryption_key:
from .._security import RC4_encrypt

data = RC4_encrypt(encryption_key, data)
stream.write(data)
stream.write(self._data)
stream.write(b"\nendstream")

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_bytestringobject():
stream = BytesIO(b"")
bo.write_to_stream(stream, encryption_key="foobar")
stream.seek(0, 0)
assert stream.read() == b"<1cdd628b972e>" # TODO: how can we verify this?
assert stream.read() == b"<73747265616d>" # TODO: how can we verify this?


def test_dictionaryobject_key_is_no_pdfobject():
Expand Down
Loading

0 comments on commit c3279f7

Please sign in to comment.