Login

EncryptField

Author:
volksman
Posted:
September 19, 2008
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

I was trying to create a custom field to use the mysql encrypt() function on some data I wanted to store in the DB. initcrash on IRC pointed me to this code which I butchered as best as my little brain could. Amazingly enough I got it working (thanks to a couple answers from initcrash).

If anyone can clean this up that would be great. I'm also trying to figure out how I can a) create password reset link in the admin interface for this field without displaying the field or b) decrypt it so that the password field is pre-populated with the decrypted password. Otherwise it is submitting the encrypted string as a new password.

Anyways, I'm only a week or two into Django with no python experience so any suggestions are very welcome.

Hope this helps someone!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from django.db import models
from django.utils.encoding import smart_unicode
from django.conf import settings
from django.utils.functional import curry
from django import forms

import urllib
import crypt

class EncryptedField(models.CharField):
    def __init__(self, verbose_name=None, cipher_key=None, **kwargs):
        self.input_type = "password"
        self.key = smart_unicode(kwargs.pop('cipher_key', settings.SECRET_KEY))
        models.CharField.__init__(self, verbose_name=verbose_name, max_length=20, **kwargs)
        
        
    def encrypt(self, cad, key=None):
 	    if not key:
 	        key = settings.SECRET_KEY
 	        assert (isinstance(cad, unicode)), "cad must be an unicode string."
 	    else:
 	        assert (isinstance(cad, unicode) and isinstance(key, unicode)), "cad and key must be unicode strings."

 	    return crypt.crypt(cad, key)

        
        
    def _encrypt(self, value):
        if value is not None:
            return self.encrypt(smart_unicode(value), self.key)
        else:
            return None



    def get_manipulator_field_objs(self):
        return [oldforms.PasswordField]

    def get_internal_type(self):
        return "TextField"

    def get_db_prep_save(self, value):
        value = self._encrypt(value)
        return super(EncryptedField, self).get_db_prep_save(value)

    def formfield(self, **kwargs):
        defaults = {'form_class': forms.CharField}
        defaults.update(kwargs)
        defaults['widget'] = forms.PasswordInput()

        return super(EncryptedField, self).formfield(**defaults)

    

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.