mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-27 17:33:45 +00:00
Bug fix, le chiffrement/déchiffrement AES marche en python2 aussi
This commit is contained in:
parent
de04a7ce40
commit
7e0d5fdbce
1 changed files with 14 additions and 19 deletions
|
@ -37,8 +37,8 @@ from django.db import models
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
EOD = '`%EofD%`' # This should be something that will not occur in strings
|
EOD_asbyte = b'`%EofD%`' # This should be something that will not occur in strings
|
||||||
|
EOD = EOD_asbyte.decode('utf-8')
|
||||||
|
|
||||||
def genstring(length=16, chars=string.printable):
|
def genstring(length=16, chars=string.printable):
|
||||||
""" Generate a random string of length `length` and composed of
|
""" Generate a random string of length `length` and composed of
|
||||||
|
@ -46,23 +46,23 @@ def genstring(length=16, chars=string.printable):
|
||||||
return ''.join([choice(chars) for i in range(length)])
|
return ''.join([choice(chars) for i in range(length)])
|
||||||
|
|
||||||
|
|
||||||
def encrypt(key, s):
|
def encrypt(key, secret):
|
||||||
""" AES Encrypt a secret `s` with the key `key` """
|
""" AES Encrypt a secret with the key `key` """
|
||||||
obj = AES.new(key)
|
obj = AES.new(key)
|
||||||
datalength = len(s) + len(EOD)
|
datalength = len(secret) + len(EOD)
|
||||||
if datalength < 16:
|
if datalength < 16:
|
||||||
saltlength = 16 - datalength
|
saltlength = 16 - datalength
|
||||||
else:
|
else:
|
||||||
saltlength = 16 - datalength % 16
|
saltlength = 16 - datalength % 16
|
||||||
ss = ''.join([s, EOD, genstring(saltlength)])
|
encrypted_secret = ''.join([secret, EOD, genstring(saltlength)])
|
||||||
return obj.encrypt(ss)
|
return obj.encrypt(encrypted_secret)
|
||||||
|
|
||||||
|
|
||||||
def decrypt(key, s):
|
def decrypt(key, secret):
|
||||||
""" AES Decrypt a secret `s` with the key `key` """
|
""" AES Decrypt a secret with the key `key` """
|
||||||
obj = AES.new(key)
|
obj = AES.new(key)
|
||||||
ss = obj.decrypt(s)
|
uncrypted_secret = obj.decrypt(secret)
|
||||||
return ss.split(bytes(EOD, 'utf-8'))[0]
|
return uncrypted_secret.split(EOD_asbyte)[0]
|
||||||
|
|
||||||
|
|
||||||
class AESEncryptedFormField(forms.CharField):
|
class AESEncryptedFormField(forms.CharField):
|
||||||
|
@ -81,8 +81,7 @@ class AESEncryptedField(models.CharField):
|
||||||
if value is None:
|
if value is None:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
return decrypt(settings.AES_KEY,
|
return decrypt(settings.AES_KEY, binascii.a2b_base64(value)).decode('utf-8')
|
||||||
binascii.a2b_base64(value)).decode('utf-8')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(value)
|
raise ValueError(value)
|
||||||
|
|
||||||
|
@ -90,18 +89,14 @@ class AESEncryptedField(models.CharField):
|
||||||
if value is None:
|
if value is None:
|
||||||
return value
|
return value
|
||||||
try:
|
try:
|
||||||
return decrypt(settings.AES_KEY,
|
return decrypt(settings.AES_KEY, binascii.a2b_base64(value)).decode('utf-8')
|
||||||
binascii.a2b_base64(value)).decode('utf-8')
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(value)
|
raise ValueError(value)
|
||||||
|
|
||||||
def get_prep_value(self, value):
|
def get_prep_value(self, value):
|
||||||
if value is None:
|
if value is None:
|
||||||
return value
|
return value
|
||||||
return binascii.b2a_base64(encrypt(
|
return binascii.b2a_base64(encrypt(settings.AES_KEY, value)).decode('utf-8')
|
||||||
settings.AES_KEY,
|
|
||||||
value
|
|
||||||
)).decode('utf-8')
|
|
||||||
|
|
||||||
def formfield(self, **kwargs):
|
def formfield(self, **kwargs):
|
||||||
defaults = {'form_class': AESEncryptedFormField}
|
defaults = {'form_class': AESEncryptedFormField}
|
||||||
|
|
Loading…
Reference in a new issue