Skip to main content

Python and Django utilities for encrypted fields using pgcrypto.

Project description

CI

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 (only aes 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, lte, contains, icontains, startswith, istartswith, endswith, and iendswith 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 TextFields 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

django_pgcrypto-3.0.3.tar.gz (31.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pgcrypto-3.0.3-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file django_pgcrypto-3.0.3.tar.gz.

File metadata

  • Download URL: django_pgcrypto-3.0.3.tar.gz
  • Upload date:
  • Size: 31.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.25

File hashes

Hashes for django_pgcrypto-3.0.3.tar.gz
Algorithm Hash digest
SHA256 94c94dab578b745b7fb7fba9d72c00a4ed5a506b3a14e12f506f8a73f3985119
MD5 10d870dd3b4a17cf8eb6364035b160ee
BLAKE2b-256 5de8c656ae59be5ff6ad0eaa6c4f896facc5e0c9c0b6d9af2be30edab8dc4beb

See more details on using hashes here.

File details

Details for the file django_pgcrypto-3.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_pgcrypto-3.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e5b9e799e77071a43156006031580690bb7a7f795c46d1cbae2cff12d8519c97
MD5 8604d550ef68d660303b33254b11d6da
BLAKE2b-256 6cc72b3aa34ab61cd037c8ea974c0250218ae261c3929e8640f47690264e37c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page