#34246: Feature: Add new type of fields - cryptofields
-------------------------------------------+------------------------
               Reporter:  Nikolay Fedorov  |          Owner:  nobody
                   Type:  New feature      |         Status:  new
              Component:  Forms            |        Version:  4.1
               Severity:  Normal           |       Keywords:
           Triage Stage:  Unreviewed       |      Has patch:  0
    Needs documentation:  0                |    Needs tests:  0
Patch needs improvement:  0                |  Easy pickings:  0
                  UI/UX:  0                |
-------------------------------------------+------------------------
 **Idea**
 Add new type of fields - cryptofields which store data in the database in
 encrypted form (bytes) and crypt and decrypt data on the fly.
 As example with Fernet (symmetric encryption).
 {{{
 from django.db import models
 from cryptography.fernet import Fernet

 CRYPTO_KEY = b'99lectrHf-urwE8CEXAqCf2UofCb-K-rEiT_VdRWhXY='

 class CryptoCharField(models.CharField):

     description = _("Crypto char field")

     def value_from_object(self, obj):
         val = getattr(obj, self.attname)
         if val not in (None, "", b""):
             val = settings.CYPHER.decrypt(val).decode('utf-8')
         return val

     def get_internal_type(self):
         return "BinaryField"

     def get_db_prep_value(self, value, connection, prepared=False):
         value = super().get_db_prep_value(value, connection, prepared)
         if value is not None:
             return
 connection.Database.Binary(Fernet(CRYPTO_KEY).encrypt(value.encode('utf-8')))
         return value
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34246>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070185917f884e-8d58c46c-83db-437a-a683-c1f789f52b91-000000%40eu-central-1.amazonses.com.

Reply via email to