Skip to content

Commit

Permalink
Merge pull request #41 from mitya57/pycryptodomex
Browse files Browse the repository at this point in the history
Migrate from pycryptodome to pycryptodomex
  • Loading branch information
jaraco authored Oct 7, 2020
2 parents c7c2680 + 10c3238 commit 74c38a0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
v4.0.0
======

#41: Instead of PyCrypto or PyCryptodome, the encrypting backend
now relies on PyCryptodomex.

v3.5.2
======

Expand Down
20 changes: 10 additions & 10 deletions keyrings/alt/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def decrypt(self, password_encrypted, assoc=None):

class Encrypted(object):
"""
PyCrypto-backed Encryption support
PyCryptodome-backed Encryption support
"""

scheme = '[PBKDF2] AES256.CFB'
Expand All @@ -44,8 +44,8 @@ def _create_cipher(self, password, salt, IV):
"""
Create the cipher object to encrypt or decrypt a payload.
"""
from Crypto.Protocol.KDF import PBKDF2
from Crypto.Cipher import AES
from Cryptodome.Protocol.KDF import PBKDF2
from Cryptodome.Cipher import AES

pw = PBKDF2(password, salt, dkLen=self.block_size)
return AES.new(pw[: self.block_size], AES.MODE_CFB, IV)
Expand All @@ -65,7 +65,7 @@ def _get_new_password(self):


class EncryptedKeyring(Encrypted, Keyring):
"""PyCrypto File Keyring"""
"""PyCryptodome File Keyring"""

filename = 'crypted_pass.cfg'
pw_prefix = 'pw:'.encode()
Expand All @@ -75,11 +75,11 @@ class EncryptedKeyring(Encrypted, Keyring):
def priority(self):
"Applicable for all platforms, but not recommended."
try:
__import__('Crypto.Cipher.AES')
__import__('Crypto.Protocol.KDF')
__import__('Crypto.Random')
__import__('Cryptodome.Cipher.AES')
__import__('Cryptodome.Protocol.KDF')
__import__('Cryptodome.Random')
except ImportError: # pragma: no cover
raise RuntimeError("PyCrypto required")
raise RuntimeError("pycryptodomex required")
if not json: # pragma: no cover
raise RuntimeError("JSON implementation such as simplejson required.")
return 0.6
Expand Down Expand Up @@ -190,10 +190,10 @@ def _lock(self):

def encrypt(self, password, assoc=None):
# encrypt password, ignore associated data
from Crypto.Random import get_random_bytes
from Cryptodome.Random import get_random_bytes

salt = get_random_bytes(self.block_size)
from Crypto.Cipher import AES
from Cryptodome.Cipher import AES

IV = get_random_bytes(AES.block_size)
cipher = self._create_cipher(self.keyring_key, salt, IV)
Expand Down
8 changes: 4 additions & 4 deletions keyrings/alt/tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

def is_crypto_supported():
try:
__import__('Crypto.Cipher.AES')
__import__('Crypto.Protocol.KDF')
__import__('Crypto.Random')
__import__('Cryptodome.Cipher.AES')
__import__('Cryptodome.Protocol.KDF')
__import__('Cryptodome.Random')
except ImportError:
return False
return True


@pytest.mark.skipif(not is_crypto_supported(), reason="Need Crypto module")
@pytest.mark.skipif(not is_crypto_supported(), reason="Need pycryptodomex module")
class TestCryptedFileKeyring(FileKeyringTests):
@pytest.fixture(autouse=True)
def mocked_getpass(self, monkeypatch):
Expand Down
2 changes: 1 addition & 1 deletion keyrings/alt/tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def test_version(self):
class TestEncryptedFileKeyring(FileKeyringTests):
@pytest.fixture(autouse=True)
def crypt_fixture(self, monkeypatch):
pytest.importorskip('Crypto')
pytest.importorskip('Cryptodome')
fake_getpass = mock.Mock(return_value='abcdef')
monkeypatch.setattr(getpass, 'getpass', fake_getpass)

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ testing =
keyring >= 20

fs>=0.5,<2
pycryptodome
pycryptodomex

# gdata doesn't currently install on Python 3
# http://code.google.com/p/gdata-python-client/issues/detail?id=229
Expand Down

0 comments on commit 74c38a0

Please sign in to comment.