import time import jwt # pip install PyJWT import hashlib import json import base64 from pyDes import des, PAD_PKCS5, CBC SECRET_KEY = 'django-insecure-fr&716j=9#6e=osxcu5-v&v1%1(kc-(_dz5cp4b(una^^lkoo0' JWT_CONFIG = { "key": "slookup", "iss": "han", "aud": "han", "exp": 3600 * 8, "algorithm": 'HS256', } DES_CONFIG = { "des_key": "_361Du$Z", # Key "des_iv": [1, 2, 3, 4, 5, 6, 7, 8] # 自定IV向量 } def des_encrypt(data, key=DES_CONFIG["des_key"]): """ 加密密码 :param data: :return: """ k = des(key, CBC, DES_CONFIG["des_iv"], pad=None, padmode=PAD_PKCS5) encrypt_str = k.encrypt(json.dumps(data)) return base64.b64encode(encrypt_str).decode("utf-8") def md5_encrypt(md5_key, salt=None): if isinstance(md5_key, str): md5_key = md5_key.encode() m = hashlib.md5(md5_key) if salt is not None: m.update(salt.encode()) data = m.hexdigest() return data def jwt_encrypt(info, exp=None): info["iat"] = int(time.time()) if exp is None: info["exp"] = int(time.time()) + JWT_CONFIG["exp"] else: info["exp"] = exp payload_key = md5_encrypt(json.dumps(info), SECRET_KEY) info["payload_key"] = payload_key payload = { 'key': JWT_CONFIG["key"], 'iss': JWT_CONFIG["iss"], 'aud': JWT_CONFIG["aud"], 'iat': int(time.time()), # 'data': info, # 'data': des_encrypt(json.dumps(info)), 'data': des_encrypt(info), 'exp': int(time.time()) + JWT_CONFIG["exp"], } jwt_token = jwt.encode(payload, JWT_CONFIG["key"], algorithm=JWT_CONFIG["algorithm"]) return jwt_token if __name__ == '__main__': exp = int(time.time()) - 60 * 5 info = { "user_id": 22132, "exp": exp } print(jwt_encrypt(info, exp=exp))
标签:info,encrypt,key,Python,Jwt,Token,exp,time,CONFIG From: https://www.cnblogs.com/SlookUp/p/16998710.html