Skip to content

Commit

Permalink
MAINT: make cryptography as default clearly
Browse files Browse the repository at this point in the history
  • Loading branch information
exiledkingcc committed Sep 11, 2023
1 parent fb35485 commit eff1f45
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ jobs:
strategy:
matrix:
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
use-crypto-lib: ["pycryptodome"]
use-crypto-lib: ["cryptography"]
include:
- python-version: "3.9"
use-crypto-lib: "cryptography"
- python-version: "3.6"
use-crypto-lib: "pycryptodome"
- python-version: "3.10"
use-crypto-lib: ""
steps:
Expand Down
2 changes: 1 addition & 1 deletion pypdf/_crypt_providers/_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from pypdf._crypt_providers._base import CryptBase
from pypdf.errors import DependencyError

_DEPENDENCY_ERROR_STR = "PyCryptodome is required for AES algorithm"
_DEPENDENCY_ERROR_STR = "cryptography>=3.1 is required for AES algorithm"


crypt_provider = ("local_crypt_fallback", "0.0.0")
Expand Down
27 changes: 8 additions & 19 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,14 @@

import pypdf
from pypdf import PasswordType, PdfReader, PdfWriter
from pypdf._crypt_providers import crypt_provider
from pypdf._crypt_providers._fallback import _DEPENDENCY_ERROR_STR
from pypdf._encryption import AlgV5, CryptAES, CryptRC4
from pypdf.errors import DependencyError, PdfReadError

try:
from Crypto.Cipher import AES # noqa: F401

HAS_PYCRYPTODOME = True
except ImportError:
HAS_PYCRYPTODOME = False

try:
from cryptography.hazmat.primitives import padding # noqa: F401

HAS_CRYPTOGRAPHY = True
except ImportError:
HAS_CRYPTOGRAPHY = False


HAS_AES = HAS_CRYPTOGRAPHY or HAS_PYCRYPTODOME
USE_CRYPTOGRAPHY = crypt_provider[0] == "cryptography"
USE_PYCRYPTODOME = crypt_provider[0] == "pycryptodome"
HAS_AES = USE_CRYPTOGRAPHY or USE_PYCRYPTODOME
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
Expand Down Expand Up @@ -95,7 +84,7 @@ def test_encryption(name, requires_aes):
ipdf = pypdf.PdfReader(inputfile)
ipdf.decrypt("asdfzxcv")
dd = dict(ipdf.metadata)
assert exc.value.args[0] == "PyCryptodome is required for AES algorithm"
assert exc.value.args[0] == _DEPENDENCY_ERROR_STR
return
else:
ipdf = pypdf.PdfReader(inputfile)
Expand Down Expand Up @@ -193,7 +182,7 @@ def test_merge_encrypted_pdfs(names):


@pytest.mark.skipif(
HAS_CRYPTOGRAPHY,
USE_CRYPTOGRAPHY,
reason="Limitations of cryptography. see https://github.com/pyca/cryptography/issues/2494",
)
@pytest.mark.parametrize(
Expand Down Expand Up @@ -286,7 +275,7 @@ def test_pdf_encrypt(pdf_file_path, alg, requires_aes):
)
with open(pdf_file_path, "wb") as output_stream:
writer.write(output_stream)
assert exc.value.args[0] == "PyCryptodome is required for AES algorithm"
assert exc.value.args[0] == _DEPENDENCY_ERROR_STR
return

writer.encrypt(
Expand Down
11 changes: 3 additions & 8 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pytest

from pypdf import PdfReader
from pypdf._crypt_providers import crypt_provider
from pypdf._reader import convert_to_int, convertToInt
from pypdf.constants import ImageAttributes as IA
from pypdf.constants import PageAttributes as PG
Expand All @@ -29,13 +30,7 @@

from . import get_data_from_url, normalize_warnings

try:
from Crypto.Cipher import AES # noqa: F401

HAS_PYCRYPTODOME = True
except ImportError:
HAS_PYCRYPTODOME = False

HAS_AES = crypt_provider[0] in ["pycryptodome", "cryptography"]
TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
Expand Down Expand Up @@ -834,7 +829,7 @@ def test_read_not_binary_mode(caplog):


@pytest.mark.enable_socket()
@pytest.mark.skipif(not HAS_PYCRYPTODOME, reason="No pycryptodome")
@pytest.mark.skipif(not HAS_AES, reason="No AES algorithm available")
def test_read_form_416():
url = (
"https://www.fda.gov/downloads/AboutFDA/ReportsManualsForms/Forms/UCM074728.pdf"
Expand Down

0 comments on commit eff1f45

Please sign in to comment.