Python and Django utilities for encrypted fields using pgcrypto.
Project description
django-pgcrypto
A set of utility functions for dealing with ASCII Armor (http://www.ietf.org/rfc/rfc2440.txt) and padding, and a collection of Django field classes that utilize these functions in a way that is compatible with pgcrypto functions.
Installation
pip install django-pgcrypto
Quickstart
There are several encrypted versions of Django fields that you can use (mostly) as you would use a normal Django field:
from django.db import models
import pgcrypto
class Employee (models.Model):
name = models.CharField(max_length=100)
ssn = pgcrypto.EncryptedTextField()
pay_rate = pgcrypto.EncryptedDecimalField()
date_hired = pgcrypto.EncryptedDateField(key="datekey", auto_now_add=True)
If not specified when creating the field (as in ssn
and pay_rate
above), fields are encrypted according to the following settings:
PGCRYPTO_DEFAULT_CIPHER
(onlyaes
is currently supported) - The default algorithm to use when encrypting fields.PGCRYPTO_DEFAULT_KEY
(default:settings.SECRET_KEY
) - The default key to use for encryption.
You must also make sure the pgcrypto extension is installed in your database. Django makes this easy with a CryptoExtension migration.
Querying
It is possible to filter on encrypted fields as you would normal fields via exact
, gt
, gte
, lt
, and lte
lookups. For example, querying the model above is possible like so:
Employee.objects.filter(date_hired__gt="1981-01-01", salary__lt=60000)
Caveats
This library encrypts and encodes data in a way that works with pgcrypto's raw encryption functions. All the warnings there about using direct keys and the lack of integrity checking apply here.
This library also predates Django's BinaryField, which is why the fields are essentially TextField
s that store armored encrypted data. This may or may not be ideal for your application, and a hypothetical future version might include a switch to store binary data.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_pgcrypto-3.0.1.tar.gz
.
File metadata
- Download URL: django_pgcrypto-3.0.1.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4e9be52ce76d1c0709a7e16bf868af6589b607cf86dba0959063f92e860b332a |
|
MD5 | 7a7ed6403b9c017a534f3074edec824a |
|
BLAKE2b-256 | 90868091a90e16c53c3a0eb5ee6e6585dc4bc42a409ff8c868a2284266a918e9 |
File details
Details for the file django_pgcrypto-3.0.1-py3-none-any.whl
.
File metadata
- Download URL: django_pgcrypto-3.0.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.4.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc021aa70419af497e1391f4bf499c757886d7e86decec39ab2bdcd5bb3fe933 |
|
MD5 | 5239340621f0d07f4f26b50701e3fb33 |
|
BLAKE2b-256 | 496f63a466107359f4ad9e924b722f14ca4933743a95de18d7b402effe5a3004 |