Skip to content

Commit

Permalink
Finished release 0.1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikvw committed May 17, 2015
2 parents b950540 + 275f3a0 commit 3a3e1bd
Show file tree
Hide file tree
Showing 183 changed files with 157 additions and 12,326 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ install:
- pip install -q -r requirements.txt --use-mirrors

before_script:
- flake8 crypto_fields
- flake8 django_crypto_fields

script:
- coverage run --source=crypto_fields manage.py test crypto_fields.tests
- coverage run --source=django_crypto_fields manage.py test django_crypto_fields.tests

after_success:
- coveralls
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ For example:
comment = EncryptedTextField(
max_length=500)

Installation
------------

pip install django-encrypted-fields

Add to INSTALLED_APPS:

INSTALLED_APPS = (
...
'django_crypto_fields',
...
)

Add KEY_PATH to the folder in settings:

# folder where the encryption keys are stored
KEY_PATH = '/Volumes/secure_drive/keys')

Add KEY_PREFIX (optional, the default is "_user_"):

# optional filename prefix for encryption keys files:
KEY_PREFIX = 'bhp066'

Run _migrate_ to create the _crypto_fields_crypt_ table:

python manage.py migrate

Generate encryption keys:

python manage.py generate_keys

History
-------
Expand Down
1 change: 0 additions & 1 deletion crypto_fields/tests/keys/test-aes-local.key

This file was deleted.

Binary file removed crypto_fields/tests/keys/test-aes-restricted.key
Binary file not shown.
9 changes: 0 additions & 9 deletions crypto_fields/tests/keys/test-rsa-irreversible-public.pem

This file was deleted.

27 changes: 0 additions & 27 deletions crypto_fields/tests/keys/test-rsa-local-private.pem

This file was deleted.

9 changes: 0 additions & 9 deletions crypto_fields/tests/keys/test-rsa-local-public.pem

This file was deleted.

27 changes: 0 additions & 27 deletions crypto_fields/tests/keys/test-rsa-restricted-private.pem

This file was deleted.

9 changes: 0 additions & 9 deletions crypto_fields/tests/keys/test-rsa-restricted-public.pem

This file was deleted.

2 changes: 0 additions & 2 deletions crypto_fields/tests/keys/test-salt-local.key

This file was deleted.

Binary file removed crypto_fields/tests/keys/test-salt-restricted.key
Binary file not shown.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .cryptor import Cryptor
from .field_cryptor import FieldCryptor
from .key_generator import KeyGenerator
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
from Crypto.Cipher import PKCS1_OAEP, AES
from Crypto.Util import number

from ..constants import KEY_FILENAMES, ENCODING
from ..exceptions import EncryptionError

from .constants import KEY_FILENAMES, ENCODING

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -76,33 +75,39 @@ def update_rsa_key_info(self, rsa_key, mode):

def load_keys(self):
logger.info('/* Loading keys ...')
# load RSA
for mode, keys in KEY_FILENAMES['rsa'].items():
for key in keys:
key_file = KEY_FILENAMES['rsa'][mode][key]
with open(key_file, 'rb') as f:
rsa_key = RSA.importKey(f.read())
rsa_key = PKCS1_OAEP.new(rsa_key)
self.KEYS['rsa'][mode][key] = rsa_key
self.update_rsa_key_info(rsa_key, mode)
try:
# load RSA
for mode, keys in KEY_FILENAMES['rsa'].items():
for key in keys:
key_file = KEY_FILENAMES['rsa'][mode][key]
with open(key_file, 'rb') as f:
rsa_key = RSA.importKey(f.read())
rsa_key = PKCS1_OAEP.new(rsa_key)
self.KEYS['rsa'][mode][key] = rsa_key
self.update_rsa_key_info(rsa_key, mode)
logger.info('(*) Loaded ' + key_file)
# decrypt and load AES
for mode in KEY_FILENAMES['aes']:
rsa_key = self.KEYS['rsa'][mode]['private']
key_file = KEY_FILENAMES['aes'][mode]['private']
with open(key_file, 'rb') as faes:
aes_key = rsa_key.decrypt(faes.read())
self.KEYS['aes'][mode]['private'] = aes_key
logger.info('(*) Loaded ' + key_file)
# decrypt and load salt
for mode in KEY_FILENAMES['salt']:
rsa_key = self.KEYS['rsa'][mode]['private']
key_file = KEY_FILENAMES['salt'][mode]['private']
with open(key_file, 'rb') as fsalt:
salt = rsa_key.decrypt(fsalt.read())
self.KEYS['salt'][mode]['private'] = salt
logger.info('(*) Loaded ' + key_file)
# decrypt and load AES
for mode in KEY_FILENAMES['aes']:
rsa_key = self.KEYS['rsa'][mode]['private']
key_file = KEY_FILENAMES['aes'][mode]['private']
with open(key_file, 'rb') as faes:
aes_key = rsa_key.decrypt(faes.read())
self.KEYS['aes'][mode]['private'] = aes_key
logger.info('(*) Loaded ' + key_file)
# decrypt and load salt
for mode in KEY_FILENAMES['salt']:
rsa_key = self.KEYS['rsa'][mode]['private']
key_file = KEY_FILENAMES['salt'][mode]['private']
with open(key_file, 'rb') as fsalt:
salt = rsa_key.decrypt(fsalt.read())
self.KEYS['salt'][mode]['private'] = salt
logger.info('(*) Loaded ' + key_file)
logger.info('Done preloading keys. */')
logger.info('Done preloading keys. */')
except FileNotFoundError:
raise FileNotFoundError(
'Unable to find keys. Check the KEY_PATH or, '
'if you do not have any keys, run the \'generate_keys\' management command '
'and try again.')

def test_rsa(self):
""" Tests keys roundtrip"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

from django.apps import apps

from ..constants import (KEY_FILENAMES, HASH_PREFIX, CIPHER_PREFIX, ENCODING, HASH_ALGORITHM, HASH_ROUNDS)
from ..exceptions import CipherError, EncryptionError, MalformedCiphertextError, EncryptionKeyError

from .constants import (KEY_FILENAMES, HASH_PREFIX, CIPHER_PREFIX, ENCODING,
HASH_ALGORITHM, HASH_ROUNDS)
from .cryptor import Cryptor


Expand All @@ -35,7 +34,7 @@ def __init__(self, algorithm, mode):
def cipher_model(self):
"""Returns the cipher model and avoids issues with model loading and field classes."""
if not self._cipher_model:
self._cipher_model = apps.get_model('crypto_fields', 'Crypt')
self._cipher_model = apps.get_model('django_crypto_fields', 'Crypt')
return self._cipher_model

def hash(self, plaintext):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
CIPHER_BUFFER_SIZE = 10

try:
KEY_PREFIX = settings.PROJECT_NUMBER
KEY_PREFIX = settings.KEY_PREFIX
except (ImproperlyConfigured, AttributeError) as e:
KEY_PREFIX = 'user'

try:
KEY_PATH = settings.KEY_PATH
except (ImproperlyConfigured, AttributeError) as e:
KEY_PATH = os.path.expanduser('~/')
KEY_PATH = os.path.join(settings.BASE_DIR, 'django_crypto_fields/tests/keys')
KEY_PREFIX = 'test'
print('Warning! Not ready for production. {}. Setting KEY_PATH to {} for testing purposes.'.format(e, KEY_PATH))

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db import models

from ..classes import FieldCryptor
from ..classes.constants import HASH_PREFIX, ENCODING
from ..constants import HASH_PREFIX, ENCODING
from ..exceptions import CipherError, EncryptionError, MalformedCiphertextError


Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.core.management.base import BaseCommand, CommandError

from ...classes.key_generator import KeyGenerator
from ...utils.key_generator import KeyGenerator


class Command(BaseCommand):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.db import models

from edc_base.base.models import BaseModel
from ..edc.base.models import BaseModel


class Crypt (BaseModel):
Expand Down Expand Up @@ -32,6 +32,6 @@ def natural_key(self):
return (self.hash, self.algorithm, self.mode,)

class Meta:
app_label = 'crypto_fields'
app_label = 'django_crypto_fields'
verbose_name = 'Crypt'
unique_together = (('hash', 'algorithm', 'mode'),)
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions django_crypto_fields/tests/keys/test-aes-local.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
j�V8;ljo�Q9��Pcs�O=s���ٶ
��l>�Q"1��n���u>�Pi�H���AĔ"�wD;�����]�ڮ'�N?!f�N�m4�w�c���ly:���$�NmwU�ͺ��BȮ$���,�ܪp����������6}�E7���;e�����U�@�9� �]�T ^�W>�1g��ӥ�V��醓�V���To d�K����]~�D�S��o�.�F���Z���� �OX��[��� h{� ی
Binary file not shown.
27 changes: 27 additions & 0 deletions django_crypto_fields/tests/keys/test-rsa-local-private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEAztfgI03cW1e3cCj4p+GD+jIHIynOoVLM9hMZ3MaiVtei6IAN
JM7lYGLEBPeB9n0xCJ7yPw1cfA8wNbADG9hxVkumC0Q/oQq0sJLbsRddNXHx8uny
f8YnpTYjM6NSIr/I029c4C/cKUHMZoKhuxhF4I7QNMABrATN390xzI2ypt/gh2Pc
u/VCAfyuno3ED+yyHazrOhSxyOXFkdD13HsBuFahHTx6NBdjJ8ZZpoaOkQkreJGs
pcBgKfy8DOpC0FYJq5HVecENjMqtzxzuF7bFEWd3FP79tenNHIdoSgvRRVth1E3f
oKAPgdAqsX2aSWsKSUiGV9NhBnTj6kjVifVHOwIDAQABAoIBAQCuik+iuuMFWSF7
zbb1w0Dg8TJAo9ivHPXDeB7Mw/jdGx1m4Bc3RjccfHs4TbsnSkEEK0vzlMzECvT9
eMdGyf/pd/BndXHT7PrIPUSRTUvj+4DVIED3PTjbGrHdsyVuGTPX5xMSQpCfZ9yZ
bAYZoMPmV9ythpBinbM0Aq0xFSj50k+jCjPisNrpk9MTcYbNQTATQS9x04msfAYt
2hfgfuD31vc7ju2wgwiidUi4fuw0sJyTk9x9JQAowJaOUEOM3Xku/fWtvJBpQ7d4
RUut9jMwgjR4iKfP37Mwn1yayogvqZMyif6vsQo+FgNS8Y98aGdPPzKgQpk6UYbZ
IaUfrGMRAoGBANcnie+ts3llUny2L4P1d5QbcPV9F3eCh6NTsd3Tq/FYPcAbVHUc
61b9d31n/uT75zPAT8LPCnVQjIfjEPgs7Mv5IvoShpq/K9zwzdU1SdM1WSSaQV1J
Wwfc4FI6TBr1Dmi9RcKLE5t7FaDoEy6rtjPUZFOlXg4snOKHwrkyFhF1AoGBAPYc
ajYsWiTZlwlego0GzWEXjNY37Mvql49EuKlsXl2feGHTClDhlPmrbI4roFBcz5mx
KeNQ9X8958ldhkq8JPhssc9EXq9ASuLsC3An/h49ifYuNO+puq/OFp+7JqQS8oQQ
qm7Tpk/Uny7NLgIeTddIPuk3gyOqqQOtiC7yPK/vAoGALvt0VIFCysSEYbVK5O7q
QM5JOWIqiK9ZPSIemgkJ5/bT7SpKpBQ/k8xflmPqNkiHsTZSdIDPkNSZC2dbIkZ1
IwlxSNhYylvqkSGIYQF08IkawTJqCZvmI+5okawnSE+w6NBD9np2OqKDE5L59VwL
un+rtNRMmJ3HWHQ64kP8q60CgYEAiTzFGVpYFg5jw6y4/e0aH5cjSN08pWU2iISY
uJkecfSYC2zEZt+z1Bl8WjcHmWh3t0tsgT7G9uJNWyNU3F3X+9OYv2xvpCgk2i8o
Ha2fbbuBMA8KI86wtJImWYHiBUIvi+dDMrPBHOkCrvVpP3MokvDNa/0xmnTy9IMS
2qLnpm8CgYEAp5fnwBdSesLNW5iKj+VdHYKZO2Fyrz8ECRq6pNWnC82lNALeqRyO
C92+3Ccg4rrt192n76AlqWZyHyy6VqjxoR9oy/kGLp6J042DZjL0+4h68/7k8RS9
BdNFgNaUBR/Y3VvTSJSauaX/Jc+8VywkFv0yD632WQNjmE54gdhpFVY=
-----END RSA PRIVATE KEY-----
9 changes: 9 additions & 0 deletions django_crypto_fields/tests/keys/test-rsa-local-public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAztfgI03cW1e3cCj4p+GD
+jIHIynOoVLM9hMZ3MaiVtei6IANJM7lYGLEBPeB9n0xCJ7yPw1cfA8wNbADG9hx
VkumC0Q/oQq0sJLbsRddNXHx8unyf8YnpTYjM6NSIr/I029c4C/cKUHMZoKhuxhF
4I7QNMABrATN390xzI2ypt/gh2Pcu/VCAfyuno3ED+yyHazrOhSxyOXFkdD13HsB
uFahHTx6NBdjJ8ZZpoaOkQkreJGspcBgKfy8DOpC0FYJq5HVecENjMqtzxzuF7bF
EWd3FP79tenNHIdoSgvRRVth1E3foKAPgdAqsX2aSWsKSUiGV9NhBnTj6kjVifVH
OwIDAQAB
-----END PUBLIC KEY-----
27 changes: 27 additions & 0 deletions django_crypto_fields/tests/keys/test-rsa-restricted-private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEArtalNlbAaSKFnHpFAX3MnCoxkIKou0Wn2bqEIkbCoMUad85W
0ndG8vZCT2xAj99GxzwlquRH27DvOEyypesDvSz1z4FB3C6fAhPR075mpKcoktE4
XfEjY4QI+OaAMbHH5GzN4FDKEWv+a9fjkpEmqhGkQKfRjQOjCQ1n1FGCo7UKzGtO
f7TeB+AiKO5zXiKju209TN5rBE2fpEupXZyOLWAbPSr48qITAfPzUuOlICIhkWRS
VrCfTXYrVlyuMFSnJWNGfFhFnnow3Md3sxOFnszNzFTiTb9pxc/oMCuYEwpT+ISR
Zms2CqtkMuTcM8bcrDhp811jDJ2v7pDRrlLULQIDAQABAoIBAB/76ktPgXF6PEEM
6RTFw6mJ2nlaPa3Mp1EgWs2+bhplFFt4Zs+nvFX0ENtgrUrkiJkn+qgRjMe2zXxa
I5BQtuVjlTA5sCesa7BAmfEHctWKMtTw60PcOiIZZF7MJAVzRWMXOG5huSWIwQlP
fDUy0oToJ0kgqigG00tzpVvk8WYrCTBMcGubrubLTDxlmHR8Pe7+ptL5U5OOTf1H
nx17QLzMYmTpeHTLltZnOGTuPrJPXIt16S4J+BakYxIHYuinRY8FHYWYRIdC3x7L
rgiLFktmPAUkCTsyvzMaMX05weBIiEcLzTQPGk6bhvl7dhOMuuFvEfOpF4H2H7TL
n8MJEi0CgYEAtxNSFPB6vj8WGoILGrC1RXQnb2+fuFVENAZfZPuUtMzgQfyq/uIb
AOqtJ6fc6YvMSlhV7lTeQYE94+hyVGvcV5DUJAusII2XK2QpA+cs1meWggz/TnTY
B1E3o/eJe069h0n/n2TYPawZXRkzLTu5QTo5udLgPPX8bvTcj/hfSOcCgYEA9Htg
DvWcraSSBURTn4ZQrrA7JyYTkyBsW6zV5vY2RAMkU3LEfYzSaVxEPNBb3+58bbNb
HowUIXD9dTjzUfkJdqDodqmEtA4aNeKj8ZTqMJUh2inMdmsAOxryZ+Sas2OrXA3b
vVTdgWA314wX+SuKBXr20mdxkmkaSgfgGokSM8sCgYAArzTOWTvMPEYj4TqTZfe3
g8Cl/P/W25K+swtmQD6FEoMAHCazfzrknPv6uvjrcpdX686ebnjKO+Z0VKkgwap4
NVVlsRl6oazf/6bbmeCkCI0afVvy+iLYnCgeF83OxTOWLuOiv+jDCkKqA28aiy+d
G8+dbzlhYPYJoCa27xHEHwKBgB+/8f6fWQSX7bqvX+jEqa+2BUiVQk+GE2xoarL+
4Jc+jTYlHm1rn1u9dsMrD/Np5lDczQWPjYG2ScQybC8GF3uHdAkNBMXZWwAKed4C
x9MdWvMZIIt0Y+hJHJfvcUPAut1Ok23oGogVtcrRT7+1Lsx9ZpZ2hqJL8fURRsJL
cMi3AoGBAKdXUJ6gXcZbhdVrdeH8shD0K+Wks0PG5aazZweF41Tziudrd6s81WJ9
gGODojA0qqCiTaH5j60yTVrKQt2OHjkSXCuVklx31sq3XRVNEm33Q2s1lobdjOGL
fl+M6a7piw5YvHw9L+DqSUdGXeWvdDKFV1U+7Xw0tLrJZE2Wew9W
-----END RSA PRIVATE KEY-----
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArtalNlbAaSKFnHpFAX3M
nCoxkIKou0Wn2bqEIkbCoMUad85W0ndG8vZCT2xAj99GxzwlquRH27DvOEyypesD
vSz1z4FB3C6fAhPR075mpKcoktE4XfEjY4QI+OaAMbHH5GzN4FDKEWv+a9fjkpEm
qhGkQKfRjQOjCQ1n1FGCo7UKzGtOf7TeB+AiKO5zXiKju209TN5rBE2fpEupXZyO
LWAbPSr48qITAfPzUuOlICIhkWRSVrCfTXYrVlyuMFSnJWNGfFhFnnow3Md3sxOF
nszNzFTiTb9pxc/oMCuYEwpT+ISRZms2CqtkMuTcM8bcrDhp811jDJ2v7pDRrlLU
LQIDAQAB
-----END PUBLIC KEY-----
Binary file added django_crypto_fields/tests/keys/test-salt-local.key
Binary file not shown.
2 changes: 2 additions & 0 deletions django_crypto_fields/tests/keys/test-salt-restricted.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
1u��zj�)s�`��>l�X�8�� Ń�T�V�Ҙ�Fsj��F꩸�.���J���?����
픘�n�V@a��ߤ�4��G���(�qҭ�>�K6�?�(���=Ύ;�fC�'�ҿ�mѾ-[��81�II�\��B%Me�}��B�(��u�R�2���7��/S#1ގ�����bWҦ>���0T1[���fu���6w �í����^bT7���@:j�H�71����g��z�3���' �;�

0 comments on commit 3a3e1bd

Please sign in to comment.