encryption
ormar gives you a way to encrypt only fields in a database. The encryption backend provided allows one-way encryption (HASH backend) as well as two-way encryption/decryption (FERNET backend).
!!!Warning Note that in order for encryption to work, you need to install optional encryption packages.
1 | |
!!! Warning Note that adding encrypt_backend changes the database column type to TEXT, which needs to be reflected in db either through migration (alembic) or manual changes
Define field encryption
To encrypt a field, you need to pass at least the encrypt_secret and encrypt_backend parameters.
1 2 3 4 5 6 7 8 9 10 11 12 | |
!!!warns that you can encrypt all field types except primary_key columns and close series (ForeignKey and ManyToMany). Check backend details for more information.
Available backend
hash
HASH is a one-way hash (such as a password) that is never decrypted when retrieved
To set it, pass the appropriate backend value.
1 2 3 4 | |
Note that since this backend never decrypts stored values, it only applies to string fields. The hash used is a sha512 hash, so the field length must be>=128.
Warning Note that in the HASH backend, you can filter by full values, but filters like contains will not work because encrypted values are compared.
!!!note Note that the encryption_secret provided hashes itself first and acts as salt, so you need to recreate this step in order to compare it with the stored string. Order_by will not work because comparing encrypted strings, so you cannot sort reliably.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
Fenette
FERNET is a bidirectional encryption/decryption backend
To set it, pass the appropriate backend value.
1 2 3 | |
Values are encrypted on the way to the database and decrypted on the way out. Can be used for all types because the return value is parsed to the corresponding python type.
!!! Warning Note that in the FERNET backend, you completely lose the possibility of filtering because part of the encrypted value is a timestamp. The same is true for order_by, because encrypted strings are compared, so you cannot sort reliably.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
custom backend
If you want to support other types of encryption (i.e. AES), you can provide your own EncryptionBackend.
To set up the backend, all you need to do is subclass the ormar.fields.EncryptBackend class and provide the required backend.
An example virtual backend (which does nothing) might look like this:
1 2 3 4 5 6 7 8 9 | |
To use this backend, set encrypt_backend to CUSTOM and provide your backend as a parameter through encrypt_custom_backend.
1 2 3 4 5 6 7 8 9 | |