Skip to content
Merged
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
12 changes: 6 additions & 6 deletions sqlalchemy_utils/types/encrypted/encrypted_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import os

import six
from sqlalchemy.types import LargeBinary, String, TypeDecorator
from sqlalchemy.types import String, TypeDecorator

from sqlalchemy_utils.exceptions import ImproperlyConfigured
from sqlalchemy_utils.types.encrypted.padding import PADDING_MECHANISM
Expand Down Expand Up @@ -110,7 +110,7 @@ def encrypt(self, value):
encryptor = self.cipher.encryptor()
encrypted = encryptor.update(value) + encryptor.finalize()
encrypted = base64.b64encode(encrypted)
return encrypted
return encrypted.decode('utf-8')

def decrypt(self, value):
if isinstance(value, six.text_type):
Expand Down Expand Up @@ -164,7 +164,7 @@ def encrypt(self, value):
encrypted = encryptor.update(value) + encryptor.finalize()
assert len(encryptor.tag) == self.TAG_SIZE_BYTES
encrypted = base64.b64encode(iv + encryptor.tag + encrypted)
return encrypted
return encrypted.decode('utf-8')

def decrypt(self, value):
if isinstance(value, six.text_type):
Expand Down Expand Up @@ -208,12 +208,12 @@ def encrypt(self, value):
value = str(value)
value = value.encode()
encrypted = self.fernet.encrypt(value)
return encrypted
return encrypted.decode('utf-8')

def decrypt(self, value):
if isinstance(value, six.text_type):
value = str(value)
decrypted = self.fernet.decrypt(value)
decrypted = self.fernet.decrypt(value.encode())
if not isinstance(decrypted, six.string_types):
decrypted = decrypted.decode('utf-8')
return decrypted
Expand Down Expand Up @@ -344,7 +344,7 @@ class User(Base):

"""

impl = LargeBinary
impl = String

def __init__(self, type_in=None, key=None,
engine=None, padding=None, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions tests/types/test_encrypted.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def test_modified_iv_fails_to_decrypt(self):
# 3rd char will be IV. Modify it
POS = 3
encrypted = encrypted[:POS] + \
(b'A' if encrypted[POS] != b'A' else b'B') + \
('A' if encrypted[POS] != 'A' else 'B') + \
encrypted[POS + 1:]
with pytest.raises(InvalidCiphertextError):
self.engine.decrypt(encrypted)
Expand All @@ -479,7 +479,7 @@ def test_modified_tag_fails_to_decrypt(self):
# 19th char will be tag. Modify it
POS = 19
encrypted = encrypted[:POS] + \
(b'A' if encrypted[POS] != b'A' else b'B') + \
('A' if encrypted[POS] != 'A' else 'B') + \
encrypted[POS + 1:]
with pytest.raises(InvalidCiphertextError):
self.engine.decrypt(encrypted)
Expand All @@ -490,7 +490,7 @@ def test_modified_ciphertext_fails_to_decrypt(self):
# 43rd char will be ciphertext. Modify it
POS = 43
encrypted = encrypted[:POS] + \
(b'A' if encrypted[POS] != b'A' else b'B') + \
('A' if encrypted[POS] != 'A' else 'B') + \
encrypted[POS + 1:]
with pytest.raises(InvalidCiphertextError):
self.engine.decrypt(encrypted)
Expand Down