Package: django-secured-fields
Django Secured FieldsDjango encrypted fields with search enabled.FeaturesAutomatically encrypt/decrypt field value usingcryptography'sFernetBuilt-in search lookup on the encrypted fields fromhashlib'sSHA-256hash value.inandisnulllookup also supported.Supports most of available Django fields includingBinaryField,JSONField, andFileField.Installationpipinstalldjango-secured-fieldsSetupAddsecured_fieldsintoINSTALLED_APPS# settings.pyINSTALLED_APPS=[...'secured_fields',]Generate a new key using for encryption$pythonmanage.pygenerate_key
KEY:TtY8MAeXuhdKDd1HfGUwim-vQ8H7fXyRQ9J8pTi_-lg=HASH_SALT:500d492ePut generated key and hash salt in settings# settings.pySECURED_FIELDS_KEY='TtY8MAeXuhdKDd1HfGUwim-vQ8H7fXyRQ9J8pTi_-lg='SECURED_FIELDS_HASH_SALT='500d492e'# optionalUsageSimple Usage# models.pyimportsecured_fieldsphone_number=secured_fields.EncryptedCharField(max_length=10)Enable Searching# models.pyimportsecured_fieldsid_card_number=secured_fields.EncryptedCharField(max_length=18,searchable=True)Supported FieldsEncryptedBinaryFieldEncryptedBooleanFieldEncryptedCharFieldEncryptedDateFieldEncryptedDateTimeFieldEncryptedDecimalFieldEncryptedFileFieldEncryptedImageFieldEncryptedIntegerFieldEncryptedJSONFieldEncryptedTextFieldSettingsKeyRequiredDefaultDescriptionSECURED_FIELDS_KEYYesKey for using in encryption/decryption with Fernet. Usually generated frompython manage.py generate_key.SECURED_FIELDS_HASH_SALTNo''Salt to append after the field value before hashing. Usually generated frompython manage.py generate_key.SECURED_FIELDS_FILE_STORAGENo'secured_fields.storage.EncryptedFileSystemStorage'File storage class used for storing encrypted file/image fields. SeeEncryptedStorageMixinAPIsField ArgumentsNameTypeRequiredDefaultDescriptionsearchableboolNoFalseEnable search functionEncryption>fromsecured_fields.fernetimportget_fernet>data=b'test'>encrypted_data=get_fernet().encrypt(data)>encrypted_datab'gAAAAABh2_Ry_thxLTuFFXeMc9hNttah82979JPuMSjnssRB0DmbgwdtEU5dapBgISOST_a_egDc66EG_ZtVu_EqF_69djJwuA=='>get_fernet().decrypt(encrypted_data)b'test'EncryptedMixinIf you have a field which does not supported by the package, you can useEncryptedMixinto enable encryption and search functionality for that custom field.importsecured_fieldsfromdjango.dbimportmodelsclassEncryptedUUIDField(secured_fields.EncryptedMixin,models.UUIDField):passtask_id=EncryptedUUIDField(searchable=True)EncryptedStorageMixinIf you use a custom file storage class (e.g. defined insettings.py'sDEFAULT_FILE_STORAGE), you can enable file encryption usingEncryptedStorageMixin.importsecured_fieldsfromminio_storage.storageimportMinioMediaStorageclassEncryptedMinioMediaStorage(secured_fields.EncryptedStorageMixin,MinioMediaStorage,):passKnown Limitationinlookup onJSONFieldis not availableLarge files are not performance-friendly at the moment (see#2)Search onBinaryFielddoes not supported at the moment (see#6)Changingsearchablevalue in a field with the records in the database is not supported (see#7)DevelopmentRequirementsDockerPoetryMySQL Clientbrew install mysql-clientecho 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profileRunning ProjectStart backend databasesmakeup-dbRun tests (see:Testing)LintingmakelintTestingmaketestFix Formattingmakeyapf