|
|
|
@ -3,8 +3,9 @@
|
|
|
|
|
import json
|
|
|
|
|
from django.db import models
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
from django.utils.encoding import force_text
|
|
|
|
|
|
|
|
|
|
from ..utils import signer
|
|
|
|
|
from ..utils import signer, aes_crypto
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
@ -114,11 +115,22 @@ class EncryptMixin:
|
|
|
|
|
def from_db_value(self, value, expression, connection, context):
|
|
|
|
|
if value is None:
|
|
|
|
|
return value
|
|
|
|
|
value = signer.unsign(value)
|
|
|
|
|
value = force_text(value)
|
|
|
|
|
|
|
|
|
|
plain_value = ''
|
|
|
|
|
# 优先采用 aes 解密
|
|
|
|
|
try:
|
|
|
|
|
plain_value = aes_crypto.decrypt(value)
|
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# 如果没有解开,使用原来的signer解密
|
|
|
|
|
if not plain_value:
|
|
|
|
|
plain_value = signer.unsign(value) or ''
|
|
|
|
|
sp = super()
|
|
|
|
|
if hasattr(sp, 'from_db_value'):
|
|
|
|
|
return sp.from_db_value(value, expression, connection, context)
|
|
|
|
|
return value
|
|
|
|
|
plain_value = sp.from_db_value(plain_value, expression, connection, context)
|
|
|
|
|
return plain_value
|
|
|
|
|
|
|
|
|
|
def get_prep_value(self, value):
|
|
|
|
|
if value is None:
|
|
|
|
@ -126,7 +138,9 @@ class EncryptMixin:
|
|
|
|
|
sp = super()
|
|
|
|
|
if hasattr(sp, 'get_prep_value'):
|
|
|
|
|
value = sp.get_prep_value(value)
|
|
|
|
|
return signer.sign(value)
|
|
|
|
|
value = force_text(value)
|
|
|
|
|
# 替换新的加密方式
|
|
|
|
|
return aes_crypto.encrypt(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EncryptTextField(EncryptMixin, models.TextField):
|
|
|
|
|