Skip to content
This repository has been archived by the owner on Feb 10, 2024. It is now read-only.

Commit

Permalink
Support python 3 only
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamhar committed Oct 3, 2019
1 parent 3e81915 commit cdf38c1
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sudo: false
language: python
python:
- "2.7"
- "3.7"
addons:
apt:
packages:
Expand Down
12 changes: 7 additions & 5 deletions encrypteddict/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ def __init__(self, gpg_home=None):

def decrypt_gpg(self, value):
try:
encrypted_bytes = io.BytesIO('{}'.format(base64.b64decode(value)))
encrypted_string = base64.b64decode(value)
encrypted_bytes = io.BytesIO(encrypted_string)
encrypted_bytes.seek(0)
decrypted_bytes = io.BytesIO()
self.ctx.decrypt(encrypted_bytes, decrypted_bytes)
return decrypted_bytes.getvalue()
return decrypted_bytes.getvalue().decode("utf-8")
except gpgme.GpgmeError as e:
print(e)
raise DecryptionError(e)

def decrypt_match_group(self, value):
Expand All @@ -43,7 +45,7 @@ def decrypt_match_group(self, value):

def decrypt_all(self, decrypt_this):
if type(decrypt_this) == dict:
for key, value in decrypt_this.iteritems():
for key, value in decrypt_this.items():
decrypt_this[key] = self.decrypt_all(value)
return decrypt_this
elif type(decrypt_this) == list:
Expand All @@ -57,7 +59,7 @@ def decrypt_all(self, decrypt_this):
def encrypt_gpg(self, value, recipients):
try:
keys = [ self.ctx.get_key(recipient) for recipient in recipients ]
decrypted_bytes = io.BytesIO('{}'.format(value))
decrypted_bytes = io.BytesIO(bytes('{}'.format(value).encode()))
encrypted_bytes = io.BytesIO()
decrypted_bytes.seek(0)
self.ctx.encrypt(keys, 1, decrypted_bytes, encrypted_bytes )
Expand All @@ -77,7 +79,7 @@ def encrypt_all(self, encrypt_this, recipients=None):
if recipients:
self.recipients = recipients
if type(encrypt_this) == dict:
for key, value in encrypt_this.iteritems():
for key, value in encrypt_this.items():
encrypt_this[key] = self.encrypt_all(value)
return encrypt_this
elif type(encrypt_this) == list:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
],
keywords='encryption gpg dict',
packages=find_packages(exclude=['static', 'docs', 'tests']),
Expand Down
6 changes: 3 additions & 3 deletions tests/test_decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_decrypt_with_no_dict_raises_exception(self):
self.test_crypter.decrypt_all()

def test_decrypt_with_unknown_key_raises_exception(self):
with open('tests/static/unknown_key.yaml', 'ro') as yaml_file:
with open('tests/static/unknown_key.yaml', 'r') as yaml_file:
test_dict = yaml.load(yaml_file)
with self.assertRaises(DecryptionError):
self.test_crypter.decrypt_all(test_dict)
Expand All @@ -47,7 +47,7 @@ def test_decrypt_a_value(self):
self.assertEqual(self.test_crypter.decrypt_gpg('hQIMAyCeTdU57g8QAQ/9GoRLUllutEJR7avVaRilafGs+UnoDO9y/7p86H9PtRUXxsRzLgb7xHDstP7xNTXHh/eY/7eQbQGulhmJ1pTpq+F9Vwbr3R9XKXDiYvFTQq+NQPD/+9QzFYI8HjYVU2JEj1FOX/VFJ2rF9YPY7FKu6kBVUT0RFjZ7qSlQmDgzTUpBRTLGLy7Xm1u7AwYMFrERJB3GK/HbnTnzPTUdzxeztnmjMzAxcz+IjFrCosjxW9zioKJKAKO4F5g4k0/IIMTBsoeq2hZAaB1jRE+d+dL0eTOtGlxR5YggurIvCSt486jziDrM3qZM525CA1ApFf0MfCZItFBo7xknpvWxhjSOXh/QkiA8I7+A1MGMfeQOevfBZGQxKKtwDsjKYIpip+0KXATwlCR26chpx8X9pPjVizQJQLFKiz6Z1ULT9nUMKit76NMh91UDk6jUkryt0qQd70oX5XzgUMspjEKYmJk8OBLnV4ZAHTwkNkdvS2MaMUqR7OIbruJUQykMQMI6jDcEeQvyfrwTlSv8YUvj2GF9vm9wRljZsR3pSk898nK0QmCCq6yLUrBmE5VD6D5EJOCfeeTuku8tmzPA6cSRAMGK1aAnofQ0M3QjE6AMsit+kYnyrB06A+DTddzBLyL8MBZQadTcV8GvWa1AOkIbzTNC+41mfmF0AwyxjFSQOHK+pE7SQAHlVgmj5cYOJ1YSAgekv6aPxGYicCZ8XEwztBh4K+AAY08d6Bz/Fn68MmhqdCqWgmzqaOdCWkTm74jWuTB0nrk='),'thing')

def test_decrypt_yaml_returns_valid_dict(self):
with open('tests/static/test_decrypt.yaml', 'ro') as yaml_file:
with open('tests/static/test_decrypt.yaml', 'r') as yaml_file:
test_dict = yaml.load(yaml_file)
print self.test_crypter.decrypt_all(test_dict)
print(self.test_crypter.decrypt_all(test_dict))
self.assertEqual(self.test_crypter.decrypt_all(test_dict),{'simple_plain_number': 1, 'hash_of_hash': {'hash1': {'leaf1': 'thing1', 'leaf2': 'thing2'}, 'hash2': {'leaf1': 'thing1', 'leaf3': 'plain', 'leaf2': 'thing2'}}, 'multiline_5_lines': 'firstline\nsecondline\nthirdline\nforthline\nfithline\n', 'simple_plain_key': 'plain', 'simple_list': ['thing1', 'thing2', 'notencrypted'], 'simple_key': 'thing', 'hash_with_list': {'leaf1': ['thing1', 'thing2', 'plain']}, 'simple_hash': {'leaf1': 'thing1', 'leaf3': 'notencrypted', 'leaf2': 'thing2'}})

0 comments on commit cdf38c1

Please sign in to comment.