加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tokenlib.py 981 Bytes
一键复制 编辑 原始数据 按行查看 历史
朱程炀 提交于 2021-11-20 17:26 . fix bug * inf
import json
from itsdangerous import TimedJSONWebSignatureSerializer, SignatureExpired, BadSignature
import random, hashlib
ERROR = 0
SUCCESS = 1
EXPIRED = 2
BAD = 3
def generateStrangeString():
md = hashlib.md5()
md.update(str(random.random()).encode("utf-8"))
s = md.hexdigest()
return s
SECRET_KEY = generateStrangeString()
SALT = generateStrangeString()
EXPIRES_IN = 36000000
def generateToken(data):
global SECRET_KEY, SALT, EXPIRES_IN
s = TimedJSONWebSignatureSerializer(secret_key=SECRET_KEY, expires_in=EXPIRES_IN, salt=SALT)
return s.dumps(data).decode('ascii')
def readToken(token):
global SECRET_KEY, SALT, EXPIRES_IN
global ERROR, SUCCESS, EXPIRED, BAD
s = TimedJSONWebSignatureSerializer(secret_key=SECRET_KEY, salt=SALT)
st = ERROR
data = {}
try:
data = s.loads(token)
st = SUCCESS
except SignatureExpired:
st = EXPIRED
except BadSignature:
st = BAD
return st, data
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化