Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate encryption mechanism from custom model class to an existing package #4

Open
theferrit32 opened this issue Apr 3, 2018 · 0 comments

Comments

@theferrit32
Copy link
Member

custom encrypted textfield class works fine

class EncryptedTextField(models.TextField):
def __init__(self, *args, **kwargs):
if crypt.instance == None:
raise RuntimeError('crypt module not initialized')
self.crypt = crypt.instance
super().__init__(*args, **kwargs)
# invoked to convert db value to python value
def from_db_value(self, value, expression, connection):
#print('EncryptedTextField.from_db_value value: ' + str(value))
dec = self.crypt.decrypt(value)
#print('EncryptedTextField.from_db_value({}) -> {}'.format(value, dec))
return dec
# invoked before saving python value to db value
def get_prep_value(self, value):
#print('EncryptedTextField.get_prep_value value: ' + str(value))
enc = self.crypt.encrypt(value)
#print('EncryptedTextField.get_prep_value({}) -> {}'.format(value, enc))
return enc

However there are a couple of pip packages out there which can handle text along with dates and other object types, which could be nice.

https://github.com/dcwatson/django-pgcrypto

The one above additionally provides a custom lookup function for postgres which can decrypt and filter values natively in the db using postgres functions 'decrypt' and 'convert_from', instead of requiring a loop and filter on a queryset in python. django-encrypted-fields package does not. This almost certainly improves performance even though it is still having to decrypt and decode every row of the table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant