<filename>common/__init__.py
# -*- coding: utf-8 -*-
# @Time    : 2021/3/25 10:37 AM

import base64
import hashlib
import hmac
import json
import time
from hashlib import md5

from Crypto.Cipher import AES


def token(message, enc=False, expire=3600 * 24):
    """
    token加密解密算法
    :param message:
    :param enc:
    :param expire:
    :return:
    """
    # 这个盐不能改😯
    salt = '<KEY>'
    bs = 16
    pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
    unpad = lambda s: s[:-ord(s[len(s) - 1:])]
    if not enc:
        content, _time, _hmac = message[:-74], message[-74:-64], message[-64:]
        key = md5((_time + salt).encode("utf-8")).hexdigest()
        _i_hmac = hmac.new(bytes.fromhex(key), (content + _time).encode('utf-8'), hashlib.sha256).hexdigest()
        if _i_hmac != _hmac:
            return False
        if time.time() - int(_time) > expire:
            return False
        return json.loads(unpad(AES.new(key[:16], AES.MODE_CBC, key[16:]).decrypt(base64.b64decode(content))))
    else:
        # _time = str(time.time() - 1.3)[:10]
        _time = '1620110661'
        key = md5((_time + salt).encode("utf-8")).hexdigest()
        raw = pad(json.dumps(message))
        cipher = AES.new(key[:16], AES.MODE_CBC, key[16:])
        __time = _time.encode('utf-8')
        ret = base64.b64encode(cipher.encrypt(raw)) + __time
        ret_hmac = hmac.new(bytes.fromhex(key), ret, hashlib.sha256).hexdigest()

        return str(ret, encoding='utf-8') + ret_hmac


def token_uid(message):
    """
    获取uid
    :param message: uid加密信息
    :return: 用户主键
    """
    return int(token(message))


def pad(s):
    bs = 16
    return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)


if __name__ == '__main__':
    e_key = '<KEY>'
    # print(token(123456, True))
    print(token('<KEY>'))

<filename>common/__init__.py
# -*- coding: utf-8 -*-
# @Time    : 2021/3/25 10:37 AM

import base64
import hashlib
import hmac
import json
import time
from hashlib import md5

from Crypto.Cipher import AES


def token(message, enc=False, expire=3600 * 24):
    """
    token加密解密算法
    :param message:
    :param enc:
    :param expire:
    :return:
    """
    # 这个盐不能改😯
    salt = '<KEY>'
    bs = 16
    pad = lambda s: s + (bs - len(s) % bs) * chr(bs - len(s) % bs)
    unpad = lambda s: s[:-ord(s[len(s) - 1:])]
    if not enc:
        content, _time, _hmac = message[:-74], message[-74:-64], message[-64:]
        key = md5((_time + salt).encode("utf-8")).hexdigest()
        _i_hmac = hmac.new(bytes.fromhex(key), (content + _time).encode('utf-8'), hashlib.sha256).hexdigest()
        if _i_hmac != _hmac:
            return False
        if time.time() - int(_time) > expire:
            return False
        return json.loads(unpad(AES.new(key[:16], AES.MODE_CBC, key[16:]).decrypt(base64.b64decode(content))))
    else:
        # _time = str(time.time() - 1.3)[:10]
        _time = '1620110661'
        key = md5((_time + salt).encode("utf-8")).hexdigest()
        raw = pad(json.dumps(message))
        cipher = AES.new(key[:16], AES.MODE_CBC, key[16:])
        __time = _time.encode('utf-8')
        ret = base64.b64encode(cipher.encrypt(raw)) + __time
        ret_hmac = hmac.new(bytes.fromhex(key), ret, hashlib.sha256).hexdigest()

        return str(ret, encoding='utf-8') + ret_hmac


def token_uid(message):
    """
    获取uid
    :param message: uid加密信息
    :return: 用户主键
    """
    return int(token(message))


def pad(s):
    bs = 16
    return s + (bs - len(s) % bs) * chr(bs - len(s) % bs)


if __name__ == '__main__':
    e_key = '<KEY>'
    # print(token(123456, True))
    print(token('<KEY>'))

# -*- coding: utf-8 -*- # @Time    : 2021/3/25 10:37 AM token加密解密算法     :param message:     :param enc:     :param expire:     :return: # 这个盐不能改😯 # _time = str(time.time() - 1.3)[:10] 获取uid     :param message: uid加密信息     :return: 用户主键 # print(token(123456, True))