Module plibs.encrypt

Expand source code
import base64
import hashlib
import os

from Cryptodome.Cipher import AES
from Cryptodome.Util.Padding import pad, unpad

from plibs.core import join_params


class AESUtil:
    """
    aes 加密与解密
    """

    def __init__(self, key: str, style='pkcs7', mode=AES.MODE_ECB):
        self.mode = mode
        self.style = style
        self.key = base64.b64decode(key.encode())

    def encrypt_data(self, data: str):
        data = data.encode()
        aes = AES.new(self.key, self.mode)
        pad_data = pad(data, AES.block_size, style=self.style)
        return str(base64.encodebytes(aes.encrypt(pad_data)), encoding='utf8').replace('\n', '')

    def decrypt_data(self, data: str):
        data = data.encode()
        aes = AES.new(self.key, self.mode)
        pad_data = pad(data, AES.block_size, style=self.style)
        return str(unpad(aes.decrypt(base64.decodebytes(pad_data)), block_size=AES.block_size).decode('utf8'))

    @staticmethod
    def generate_key(length=256) -> str:
        random_key = os.urandom(length)
        private_key = hashlib.sha256(random_key).digest()
        return base64.b64encode(private_key).decode()


def md5_encode(s):
    """
    md5加密
    """
    m = hashlib.md5(s.encode(encoding='utf-8'))
    return m.hexdigest()


def sha1_encode(s: str) -> str:
    """
    sha1加密
    """
    m = hashlib.sha1(s.encode(encoding='utf-8'))
    return m.hexdigest()


class SignAuth:
    """
    内部签名工具
    """

    def __init__(self, private_key):
        self.private_key = private_key

    def verify(self, params, sign):
        """
        校验sign
        """
        sign_tmp = self.generate_sign(params)
        return sign.upper() == sign_tmp.upper()

    def generate_sign(self, params):
        """
        生成sign
        """
        sign_str = join_params(params, key=self.private_key, sep='', exclude_keys=['sign'])
        return md5_encode(sign_str)

Functions

def md5_encode(s)

md5加密

Expand source code
def md5_encode(s):
    """
    md5加密
    """
    m = hashlib.md5(s.encode(encoding='utf-8'))
    return m.hexdigest()
def sha1_encode(s)

sha1加密

Expand source code
def sha1_encode(s: str) -> str:
    """
    sha1加密
    """
    m = hashlib.sha1(s.encode(encoding='utf-8'))
    return m.hexdigest()

Classes

class AESUtil (key, style='pkcs7', mode=1)

aes 加密与解密

Expand source code
class AESUtil:
    """
    aes 加密与解密
    """

    def __init__(self, key: str, style='pkcs7', mode=AES.MODE_ECB):
        self.mode = mode
        self.style = style
        self.key = base64.b64decode(key.encode())

    def encrypt_data(self, data: str):
        data = data.encode()
        aes = AES.new(self.key, self.mode)
        pad_data = pad(data, AES.block_size, style=self.style)
        return str(base64.encodebytes(aes.encrypt(pad_data)), encoding='utf8').replace('\n', '')

    def decrypt_data(self, data: str):
        data = data.encode()
        aes = AES.new(self.key, self.mode)
        pad_data = pad(data, AES.block_size, style=self.style)
        return str(unpad(aes.decrypt(base64.decodebytes(pad_data)), block_size=AES.block_size).decode('utf8'))

    @staticmethod
    def generate_key(length=256) -> str:
        random_key = os.urandom(length)
        private_key = hashlib.sha256(random_key).digest()
        return base64.b64encode(private_key).decode()

Static methods

def generate_key(length=256)
Expand source code
@staticmethod
def generate_key(length=256) -> str:
    random_key = os.urandom(length)
    private_key = hashlib.sha256(random_key).digest()
    return base64.b64encode(private_key).decode()

Methods

def decrypt_data(self, data)
Expand source code
def decrypt_data(self, data: str):
    data = data.encode()
    aes = AES.new(self.key, self.mode)
    pad_data = pad(data, AES.block_size, style=self.style)
    return str(unpad(aes.decrypt(base64.decodebytes(pad_data)), block_size=AES.block_size).decode('utf8'))
def encrypt_data(self, data)
Expand source code
def encrypt_data(self, data: str):
    data = data.encode()
    aes = AES.new(self.key, self.mode)
    pad_data = pad(data, AES.block_size, style=self.style)
    return str(base64.encodebytes(aes.encrypt(pad_data)), encoding='utf8').replace('\n', '')
class SignAuth (private_key)

内部签名工具

Expand source code
class SignAuth:
    """
    内部签名工具
    """

    def __init__(self, private_key):
        self.private_key = private_key

    def verify(self, params, sign):
        """
        校验sign
        """
        sign_tmp = self.generate_sign(params)
        return sign.upper() == sign_tmp.upper()

    def generate_sign(self, params):
        """
        生成sign
        """
        sign_str = join_params(params, key=self.private_key, sep='', exclude_keys=['sign'])
        return md5_encode(sign_str)

Methods

def generate_sign(self, params)

生成sign

Expand source code
def generate_sign(self, params):
    """
    生成sign
    """
    sign_str = join_params(params, key=self.private_key, sep='', exclude_keys=['sign'])
    return md5_encode(sign_str)
def verify(self, params, sign)

校验sign

Expand source code
def verify(self, params, sign):
    """
    校验sign
    """
    sign_tmp = self.generate_sign(params)
    return sign.upper() == sign_tmp.upper()