Skip to main content

peewee_encrypted_field

https://img.shields.io/badge/license-MIT-blue.svg?style=flat https://badge.fury.io/gh/brake%2Fpeewee_encrypted_field.svg https://img.shields.io/badge/Python-2.7-red.svg

Encrypted field for Peewee ORM models to save data in DB in encrypted form.

Data stored in DataBase as Fernet tokens. After you define an encryption key all cryptography will be performed transparently for your application.

Uses simple fernet implementation https://github.com/heroku/fernet-py

Idea caught from SQLAlchemy’s EncryptedType.

Contents

Implementation Details

A fernet-py package can use Pycrypto or M2Crypto as backend (details). Same belongs to this module due to its dependency from fernet-py. Note that pip uses Pycrypto as a default dependency when install fernet-py. Length of entire key is 32 bytes, 16 bytes per both signing and encryption keys, as stated in specification.

Features

You have to set key as a property of appropriate EncryptedField. You need to set this property once per Model per Model ‘s Field. If you attempt to set key property for a Field whose key already has been set, exception EncryptedField.KeyAlreadyExists will be raised. To change the key on the fly you have to explictly perform del SomeEncryptedField.key and then set this property to desired value again.

Installation

pip install peewee_encrypted_field

or, if you downloaded source,

python setup.py install

Usage

At first, import module

from peewee import *
from peewee_encrypted_field import EncryptedField

Then, define the model with EncryptedField

class SecureTable(BaseModel):
    sensitive_data = EncryptedField()

    class Meta:
        db_table = 'SecureTable'

After, configure field’s encryption key

SecureTable.sensitive_data.key = key_derivation_fn()  # a hypotetical key derivation
                                                      # function returning 32 byte key

Finally, save and retrieve data in a Peewee’s usual manner

new_secret = SecureTable(sensitive_data='My New BIG Secret')
new_secret.save()

Key Derivation Example

Example Key Derivation Function baded on Pycrypto module.

Please take look at this Gist - it can contain more actual version of code below.

from binascii import unhexlify
from functools import partial
from Crypto.Protocol import KDF
from Crypto.Hash import SHA512, HMAC

_SALT = unhexlify('48B755AB80CD1C3DA61182D3DCD2E3A2CA869B783618FF6551FB4B0CDC3B8066')  # some salt
_KEY_LENGTH = 32

key_derivation_fn = partial(
    KDF.PBKDF2,
    salt=_SALT,
    dkLen=_KEY_LENGTH,
    count=5000,
    prf=lambda p,s: HMAC.new(p,s,SHA512).digest()
)

# KDF usage
SecureTable.sensitive_data.key = key_derivation_fn(text_password)

Metadata

Release files for peewee_encrypted_field 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for peewee_encrypted_field 0.0.2
File Size Uploaded Provenance Actions
peewee_encrypted_field-0.0.2.tar.gz 4.8 kB Without provenance Details for peewee_encrypted_field-0.0.2.tar.gz

Release files / peewee_encrypted_field-0.0.2.tar.gz

Download URL peewee_encrypted_field-0.0.2.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a18ae1e2d87cf05b4c781210dbd4e56dec1b090d8f54547dbcf0fe3cfa799435
BLAKE2b-256 checksum
How to use checksums
dc080a8b530efff8c424baa81e18be116b4fd6419c6387298042b3b0cd86b320
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for peewee_encrypted_field 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file No provenance

0.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page