#34246 closed New feature (wontfix)

Feature: Add new type of fields - cryptofields

Reported by: Nikolay Fedorov Owned by: nobody
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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

Change History (1)

comment:1 by Tim Graham, 21 months ago

Component: FormsDatabase layer (models, ORM)
Resolution: wontfix
Status: newclosed

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.

Note: See TracTickets for help on using tickets.
Back to Top