Fix handling of None, and, encode not 'bytes'
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import base64
|
import base64
|
||||||
import six
|
import six
|
||||||
from sqlalchemy.types import TypeDecorator, String
|
from sqlalchemy.types import TypeDecorator, String, Binary
|
||||||
from sqlalchemy_utils.exceptions import ImproperlyConfigured
|
from sqlalchemy_utils.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
cryptography = None
|
cryptography = None
|
||||||
@@ -74,7 +74,7 @@ class AesEngine(EncryptionDecryptionBaseEngine):
|
|||||||
value = repr(value)
|
value = repr(value)
|
||||||
if isinstance(value, six.text_type):
|
if isinstance(value, six.text_type):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
value = six.b(value)
|
value = value.encode()
|
||||||
value = self._pad(value)
|
value = self._pad(value)
|
||||||
encryptor = self.cipher.encryptor()
|
encryptor = self.cipher.encryptor()
|
||||||
encrypted = encryptor.update(value) + encryptor.finalize()
|
encrypted = encryptor.update(value) + encryptor.finalize()
|
||||||
@@ -113,7 +113,7 @@ class FernetEngine(EncryptionDecryptionBaseEngine):
|
|||||||
value = repr(value)
|
value = repr(value)
|
||||||
if isinstance(value, six.text_type):
|
if isinstance(value, six.text_type):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
value = six.b(value)
|
value = value.encode()
|
||||||
encrypted = self.fernet.encrypt(value)
|
encrypted = self.fernet.encrypt(value)
|
||||||
return encrypted
|
return encrypted
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ class EncryptedType(TypeDecorator):
|
|||||||
engine.dispose()
|
engine.dispose()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
impl = String
|
impl = Binary # CHANGE!
|
||||||
|
|
||||||
def __init__(self, type_in=None, key=None, engine=None, **kwargs):
|
def __init__(self, type_in=None, key=None, engine=None, **kwargs):
|
||||||
"""Initialization."""
|
"""Initialization."""
|
||||||
@@ -225,9 +225,11 @@ class EncryptedType(TypeDecorator):
|
|||||||
|
|
||||||
def process_bind_param(self, value, dialect):
|
def process_bind_param(self, value, dialect):
|
||||||
"""Encrypt a value on the way in."""
|
"""Encrypt a value on the way in."""
|
||||||
|
if value is not None:
|
||||||
return self.engine.encrypt(value)
|
return self.engine.encrypt(value)
|
||||||
|
|
||||||
def process_result_value(self, value, dialect):
|
def process_result_value(self, value, dialect):
|
||||||
"""Decrypt value on the way out."""
|
"""Decrypt value on the way out."""
|
||||||
|
if value is not None:
|
||||||
decrypted_value = self.engine.decrypt(value)
|
decrypted_value = self.engine.decrypt(value)
|
||||||
return self.underlying_type.python_type(decrypted_value)
|
return self.underlying_type.python_type(decrypted_value)
|
||||||
|
Reference in New Issue
Block a user