Password encryption
This commit is contained in:
parent
40b718b45c
commit
2d50a85caa
3 changed files with 66 additions and 12 deletions
63
settings/aes_field.py
Normal file
63
settings/aes_field.py
Normal file
|
@ -0,0 +1,63 @@
|
|||
import string
|
||||
import binascii
|
||||
from random import choice
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
|
||||
EOD = '`%EofD%`' # This should be something that will not occur in strings
|
||||
|
||||
|
||||
def genstring(length=16, chars=string.printable):
|
||||
return ''.join([choice(chars) for i in range(length)])
|
||||
|
||||
|
||||
def encrypt(key, s):
|
||||
obj = AES.new(key)
|
||||
datalength = len(s) + len(EOD)
|
||||
if datalength < 16:
|
||||
saltlength = 16 - datalength
|
||||
else:
|
||||
saltlength = 16 - datalength % 16
|
||||
ss = ''.join([s, EOD, genstring(saltlength)])
|
||||
return obj.encrypt(ss)
|
||||
|
||||
|
||||
def decrypt(key, s):
|
||||
obj = AES.new(key)
|
||||
ss = obj.decrypt(s)
|
||||
print(ss)
|
||||
return ss.split(bytes(EOD, 'utf-8'))[0]
|
||||
|
||||
|
||||
class AESEncryptedField(models.CharField):
|
||||
def save_form_data(self, instance, data):
|
||||
setattr(instance, self.name,
|
||||
binascii.b2a_base64(encrypt(settings.AES_KEY, data)))
|
||||
|
||||
def to_python(self, value):
|
||||
if value is None:
|
||||
return None
|
||||
return decrypt(settings.AES_KEY,
|
||||
binascii.a2b_base64(value)).decode('utf-8')
|
||||
|
||||
def from_db_value(self, value, expression, connection, *args):
|
||||
print('from db')
|
||||
print(value)
|
||||
if value is None:
|
||||
return value
|
||||
return decrypt(settings.AES_KEY,
|
||||
binascii.a2b_base64(value)).decode('utf-8')
|
||||
|
||||
def get_prep_value(self, value):
|
||||
print('get prep value')
|
||||
print(value)
|
||||
print(binascii.b2a_base64(encrypt(
|
||||
settings.AES_KEY,
|
||||
value
|
||||
)))
|
||||
return binascii.b2a_base64(encrypt(
|
||||
settings.AES_KEY,
|
||||
value
|
||||
))
|
|
@ -1,17 +1,6 @@
|
|||
from django.db import models
|
||||
import binascii
|
||||
from site_tps import qaes
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class AESEncryptedField(models.CharField):
|
||||
def save_form_data(self, instance, data):
|
||||
setattr(instance, self.name,
|
||||
binascii.b2a_base64(qaes.encrypt(settings.AES_KEY, data)))
|
||||
|
||||
def value_from_object(self, obj):
|
||||
return qaes.decrypt(settings.AES_KEY,
|
||||
binascii.a2b_base64(getattr(obj, self.attname)))
|
||||
from .aes_field import AESEncryptedField
|
||||
|
||||
|
||||
class ContentSettings(models.Model):
|
||||
|
|
|
@ -22,6 +22,8 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = '1o5n7!p2@@bcd9om*6^d^=@es==*yf2^^cax=j(ij4s3#9-y(m'
|
||||
|
||||
AES_KEY = "0123456789ABCDEF"
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
|
|
Loading…
Reference in a new issue