Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions eth_hash/backends/pycryptodome.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
cast,
)

from Crypto.Hash import (
keccak,
)
try:
from Cryptodome.Hash import (
keccak,
)
except ImportError:
try:
from Crypto.Hash import (
keccak,
)
except ImportError as exc:
raise ImportError(
"Could not import either 'Cryptodome' or 'Crypto' namespace. "
"Please install pycryptodome package."
) from exc

from eth_hash.abc import (
BackendAPI,
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"pycryptodome": [
"pycryptodome>=3.6.6,<4",
],
"pycryptodomex": [
"pycryptodomex>=3.6.6,<4",
],
"pysha3": [
"pysha3>=1.0.0,<2.0.0;python_version<'3.9'",
"safe-pysha3>=1.0.0;python_version>='3.9'",
Expand Down
26 changes: 24 additions & 2 deletions tests/core/test_import_and_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ def test_import_auto_empty_crash(monkeypatch):
keccak,
)

with mock.patch.dict("sys.modules", {"sha3": None, "Crypto.Hash": None}):
clean_module("eth_hash.backends.pycryptodome")
clean_module("eth_hash.backends.pysha3")

with mock.patch.dict(
"sys.modules",
{
"sha3": None,
"Crypto": None,
"Cryptodome": None,
"eth_hash.backends": mock.MagicMock(),
},
):
with pytest.raises(
ImportError, match="None of these hashing backends are installed"
):
Expand Down Expand Up @@ -53,7 +64,18 @@ def test_load_by_env(monkeypatch, backend):
)

monkeypatch.setenv("ETH_HASH_BACKEND", backend)
with mock.patch.dict("sys.modules", {"sha3": None, "Crypto.Hash": None}):
clean_module("eth_hash.backends.pycryptodome")
clean_module("eth_hash.backends.pysha3")

with mock.patch.dict(
"sys.modules",
{
"sha3": None,
"Crypto": None,
"Cryptodome": None,
"eth_hash.backends": mock.MagicMock(),
},
):
with pytest.raises(ImportError) as excinfo:
keccak(b"triggered")
expected_msg = (
Expand Down