forked from brake/peewee_encrypted_field
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peewee_encrypted_field.py
83 lines (67 loc) · 2.99 KB
/
peewee_encrypted_field.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
# Name: peewee_encrypted_field.py
# Module: peewee_encrypted_field
#
# Created: 02.12.2015 14:01
# Copyright: (c) Constantin Roganov, 2015 - 2016
# MIT License
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-------------------------------------------------------------------------------
#!/usr/bin/env python
"""
Encrypted field for Peewee.
Saves data to the DB as as AES128 bytestream
Re-implemented with cryptography fernet module albeit with a cutdown in fuctionality.
Idea taken from brake/peewee_encrypted_field which was at the time having issues with setup drawing down
https://pypi.org/project/fernet/
rather than
https://github.com/heroku/fernet-py
"""
from cryptography.fernet import MultiFernet
from cryptography.fernet import Fernet
from peewee import *
class EncryptedField(Field):
"""Encrypted field.
Encrypted content is a Fernet token.
Field maintains a list of keys as map of maps where the first key is
model_class and second key is id(self_field).
"""
class KeyIsUndefined(RuntimeError):
pass
class KeyAlreadyExists(RuntimeError):
pass
_Tokens = []
db_field = 'text'
def __init__(self, Key=None, **kwargs):
if Key is None:
Key = MultiFernet.generate_key()
print('No key supplied pesudo-random key generated : {}\nPlease update field with this key value before next run'.format(Key))
EncryptedField._Tokens.append(Fernet(Key))
Field.__init__(self, **kwargs)
@property
def Fernet(self):
try:
return MultiFernet(EncryptedField._Tokens)
except:
raise EncryptedField.KeyIsUndefined
def db_value(self, value):
return self.Fernet.encrypt(bytes(value, 'utf-8'))
def python_value(self, value):
return self.Fernet.decrypt(value).decode('utf8')