Package: django-crypto-fields
django-crypto-fieldsPython 3.8, 3.9, 3.10, Django 3.2, 4.0, 4.1 using mysqlUsespycryptodomexThis module has known problems withpostgres.Add encrypted field classes to your Django models whereunique=Trueandunique_togetherattributes work as expected.For example:fromdjango.dbimportmodelsfromdjango_crypto_fields.fieldsimportEncryptedTextField,FirstnameField,IdentityFieldclassPatientModel(models.Model):first_name=FirstnameField(verbose_name="First Name")identity=IdentityField(verbose_name="Identity",unique=True)comment=EncryptedTextField(max_length=500)Installationadd to INSTALLED_APPS:INSTALLED_APPS=(...'django_crypto_fields.apps.AppConfig',...)Add KEY_PATH to the folder in settings:# folder where the encryption keys are stored# Do not set for testsKEY_PATH='/etc/myproject/django_crypto_fields')Add KEY_PREFIX (optional, the default is “user”):# optional filename prefix for encryption keys files:KEY_PREFIX='bhp066'Runmigrateto create thedjango_crypto_fields.crypttable:pythonmanage.pymigratedjango_crypto_fieldsEncryption keysTake care of the encryption keys!In your tests you can setsettings.DEBUG = Trueandsettings.AUTO_CREATE_KEYS = Trueso that keys are generated for your tests. Encryption keys to will not automatically generate on a production system (DEBUG=False). SeeAppConfig.auto_create_keys.Historydjango-crypto-fieldshas been used in our audited research projects that use our “Edc” for data collection and management. Data collected in our Edc are considered “source documents”.django-crypto-fieldadds field level encryption for sensitive field values such as names, identifiers, dob, etc (PII). Authorized study personnel accessing the data through the application can see PII. Downstream data management staff and statisticians accessing the database directly cannot.FeaturesAll values are stored as a pair of hash (hashlib.pbkdf2_hmac) and secret (rsaoraes);A model using adjango-crypto-fieldsfield class stores the hash only;A separate table relates the hash to it’s secret and is referenced internally by the field class;AdvantagesAutomatically creates encryption key sets (RSA, AES and salt) and stores them in theKEY_PATHfolder;Supports unique constraints and compound constraints that including encrypted fields. The hash is stored in the model’s db_table and not the secret. Theunique=Trueandunique_togetherattributes work as expected;The dataset is de-identified at rest. This has many advantages but helps us work well with our analysis team. The data analysis team do not need to see PII. They just want a de-identified dataset. A de-identified dataset is one where PII fields are encrypted and others not. With the RSA keys removed, the dataset is effectively de-identified;Datasets from other systems with shared PII values, such as identity numbers, can be prepared for meta-analysis using the same keys and algorithms;The dataset can be permanently obscured by dropping theCrypttable from the DB (it has all the secrets);By default field classes exist for two sets of keys. You can customizeKEY_FILENAMESto create as many sets as needed. With multiple sets of keys you have more control over who gets to see what.DisadvantagesLimited support for lookup types. The “query value” is the hash not the decrypted secret, so Django lookups like['startswith', 'istartswith', 'endswith', 'iendswith', 'contains', 'icontains', 'iexact']are not supported.Hashing with a secret may be considered less secure than just a “secret”. You decide what your requirements are. For systems that collect PII in fields classes fromdjango-crypto-fields, we take all the basic security precautions: OS and application-level password protection, Full-Drive encryption, physical security and so on.Other encrypted field modules are available if you just want to use encrypted field classes in Django models and do not need unique constraints nor plan to join tables on encrypted fields for analysis.ContributeIssue Tracker: github.com/erikvw/django-crypto-fields/issuesSource Code: github.com/erikvw/django-crypto-fields