Skip to content

Commit

Permalink
STY: Changes to if and elif use (#2968)
Browse files Browse the repository at this point in the history
Slightly increase readability.
  • Loading branch information
j-t-1 authored Nov 27, 2024
1 parent 5b80cbb commit bd26922
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pypdf/_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def encrypt_object(self, obj: PdfObject) -> PdfObject:
if isinstance(obj, ByteStringObject):
data = self.str_crypt.encrypt(obj.original_bytes)
obj = ByteStringObject(data)
if isinstance(obj, TextStringObject):
elif isinstance(obj, TextStringObject):
data = self.str_crypt.encrypt(obj.get_encoded_bytes())
obj = ByteStringObject(data)
elif isinstance(obj, StreamObject):
Expand Down Expand Up @@ -574,7 +574,7 @@ def verify_user_password(

@staticmethod
def calculate_hash(R: int, password: bytes, salt: bytes, udata: bytes) -> bytes:
# from https://github.com/qpdf/qpdf/blob/main/libqpdf/QPDF_encryption.cc
# https://github.com/qpdf/qpdf/blob/main/libqpdf/QPDF_encryption.cc
k = hashlib.sha256(password + salt + udata).digest()
if R < 6:
return k
Expand Down Expand Up @@ -924,6 +924,7 @@ def _make_crypt_filter(self, idnum: int, generation: int) -> CryptFilter:
key_data = key[:n] + pack1 + pack2
key_hash = hashlib.md5(key_data)
rc4_key = key_hash.digest()[: min(n + 5, 16)]

# for AES-128
key_hash.update(b"sAlT")
aes128_key = key_hash.digest()[: min(n + 5, 16)]
Expand All @@ -941,14 +942,14 @@ def _make_crypt_filter(self, idnum: int, generation: int) -> CryptFilter:
def _get_crypt(
method: str, rc4_key: bytes, aes128_key: bytes, aes256_key: bytes
) -> CryptBase:
if method == "/AESV3":
return CryptAES(aes256_key)
if method == "/AESV2":
return CryptAES(aes128_key)
elif method == "/Identity":
if method == "/AESV3":
return CryptAES(aes256_key)
if method == "/Identity":
return CryptIdentity()
else:
return CryptRC4(rc4_key)

return CryptRC4(rc4_key)

@staticmethod
def _encode_password(password: Union[bytes, str]) -> bytes:
Expand Down

0 comments on commit bd26922

Please sign in to comment.