Skip to content

Commit

Permalink
minor encoding issue
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed Jun 7, 2016
1 parent 109e82b commit 9c79d40
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 12 additions & 0 deletions django_crypto_fields/field_cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def decrypt(self, hash_with_prefix):
hash_with_prefix = hash_prefix+hash."""
plaintext = None
try:
hash_with_prefix = hash_with_prefix.encode(ENCODING)
except AttributeError:
pass
if hash_with_prefix:
if self.is_encrypted(hash_with_prefix, has_secret=False):
hashed_value = self.get_hash(hash_with_prefix)
Expand Down Expand Up @@ -177,6 +181,10 @@ def get_prep_value(self, value):

def get_hash(self, ciphertext):
"""Returns the hashed_value given a ciphertext or None."""
try:
ciphertext.encode(ENCODING)
except AttributeError:
pass
return ciphertext[len(HASH_PREFIX):][:self.hash_size] or None

def get_secret(self, ciphertext):
Expand Down Expand Up @@ -213,6 +221,10 @@ def is_encrypted(self, value, has_secret=None):
is_encrypted = False
else:
is_encrypted = False
try:
value = value.encode(ENCODING)
except AttributeError:
pass
if (value[:len(HASH_PREFIX)] == HASH_PREFIX.encode(ENCODING) and not
value[:len(CIPHER_PREFIX)] == CIPHER_PREFIX.encode(ENCODING)):
value = self.verify_value(value, has_secret=False)
Expand Down
9 changes: 6 additions & 3 deletions django_crypto_fields/fields/base_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ..exceptions import CipherError, EncryptionError, MalformedCiphertextError
from ..field_cryptor import FieldCryptor
from django_crypto_fields.exceptions import EncryptionLookupError
from django_crypto_fields.constants import ENCODING


class BaseField(models.Field):
Expand All @@ -23,7 +24,9 @@ def __init__(self, algorithm, mode, *args, **kwargs):
self.help_text = '{} (Encryption: {} {})'.format(
self.help_text.split(' (Encryption:')[0], algorithm.upper(), mode)
self.field_cryptor = FieldCryptor(self.algorithm, self.mode)
self.max_length = kwargs.get('max_length', None) or len(HASH_PREFIX) + self.field_cryptor.hash_size
min_length = len(HASH_PREFIX) + self.field_cryptor.hash_size
max_length = kwargs.get('max_length', min_length)
self.max_length = min_length if max_length < min_length else max_length
if self.algorithm == RSA:
max_message_length = self.keys.rsa_key_info[self.mode]['max_message_length']
if self.max_length > max_message_length:
Expand Down Expand Up @@ -102,7 +105,7 @@ def get_prep_lookup(self, lookup_type, value):
elif lookup_type == 'in':
self.get_in_as_lookup(value)
else:
value = HASH_PREFIX.encode() + self.field_cryptor.hash(value)
value = HASH_PREFIX.encode(ENCODING) + self.field_cryptor.hash(value)
return super(BaseField, self).get_prep_lookup(lookup_type, value)

def get_isnull_as_lookup(self, value):
Expand All @@ -111,7 +114,7 @@ def get_isnull_as_lookup(self, value):
def get_in_as_lookup(self, values):
hashed_values = []
for value in values:
hashed_values.append(HASH_PREFIX.encode() + self.field_cryptor.hash(value))
hashed_values.append(HASH_PREFIX.encode(ENCODING) + self.field_cryptor.hash(value))
return hashed_values

def get_internal_type(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name='django-crypto-fields',
version='0.1.6dev0',
version='0.1.6dev1',
author=u'Erik van Widenfelt',
author_email='ew2789@gmail.com',
packages=find_packages(),
Expand Down

0 comments on commit 9c79d40

Please sign in to comment.