feat: 添加aes fix: 修改时区的bug

pull/4095/head
ibuler 5 years ago
parent 4695f80172
commit 66f3706142

@ -9,6 +9,7 @@ import time
import hashlib
from io import StringIO
from itertools import chain
from Crypto.Cipher import AES
import paramiko
import sshpubkeys
@ -225,3 +226,53 @@ def model_to_json(instance, sort_keys=True, indent=2, cls=None):
if cls is None:
cls = DjangoJSONEncoder
return json.dumps(data, sort_keys=sort_keys, indent=indent, cls=cls)
class AESCrypto:
"""
AES
除了MODE_SIV模式key长度为32, 48, or 64,
其余key长度为16, 24 or 32
详细见AES内部文档
CBC模式传入iv参数
本例使用常用的ECB模式
"""
def __init__(self, key):
if len(key) > 32:
key = key[:32]
self.key = self.to_16(key)
@staticmethod
def to_16(key):
"""
转为16倍数的bytes数据
:param key:
:return:
"""
key = bytes(key, encoding="utf8")
while len(key) % 16 != 0:
key += b'\0'
return key # 返回bytes
def aes(self):
return AES.new(self.key, AES.MODE_ECB) # 初始化加密器
def encrypt(self, text):
aes = self.aes()
return str(base64.encodebytes(aes.encrypt(self.to_16(text))),
encoding='utf8').replace('\n', '') # 加密
def decrypt(self, text):
aes = self.aes()
return str(aes.decrypt(base64.decodebytes(bytes(text, encoding='utf8'))).rstrip(b'\0').decode("utf8")) # 解密
def get_aes_crypto(key=None):
if key is None:
key = settings.SECRET_KEY
a = AESCrypto(key)
return a
aes = get_aes_crypto()

@ -16,10 +16,13 @@ class TimezoneMiddleware:
def __call__(self, request):
tzname = request.META.get('HTTP_X_TZ')
if tzname:
timezone.activate(pytz.timezone(tzname))
else:
timezone.deactivate()
if not tzname or tzname == 'undefined':
return self.get_response(request)
try:
tz = pytz.timezone(tzname)
timezone.activate(tz)
except pytz.UnknownTimeZoneError:
pass
response = self.get_response(request)
return response

Loading…
Cancel
Save