[Django] #34246: Feature: Add new type of fields - cryptofields

5 views
Skip to first unread message

Django

unread,
Jan 8, 2023, 8:08:55 AM1/8/23
to django-...@googlegroups.com
#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.

Django

unread,
Jan 8, 2023, 6:14:28 PM1/8/23
to django-...@googlegroups.com
#34246: Feature: Add new type of fields - cryptofields
-------------------------------------+-------------------------------------

Reporter: Nikolay Fedorov | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.1
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix
* component: Forms => Database layer (models, ORM)


Comment:

I think we'd rather see specialized fields like this implemented in third-
party packages, e.g. https://pypi.org/project/django-crypto-fields/.

For future reference, it's better to raise a proposal like this on the
DevelopersMailingList to see if there's consensus about adding it.

--
Ticket URL: <https://code.djangoproject.com/ticket/34246#comment:1>

Reply all
Reply to author
Forward
0 new messages