# update: 2021-6-30-14
import base64
import hashlib


def str_to_b64(string):
    """b64加密"""
    try:
        return str(base64.b64encode(str(string).encode('utf-8')), 'utf-8')
    except Exception as exception:
        print(f"exception: {exception.__class__.__name__}")
        return str()


def b64_to_str(string):
    """b64加密"""
    try:
        return str(base64.b64decode(str(string).encode('utf-8')), 'utf-8')
    except Exception as exception:
        print(f"exception: {exception.__class__.__name__}")
        return str()


def text_to_byte(text, mode='utf-8'):
    return text.encode(mode)


def byte_to_text(byte, mode='utf-8'):
    return byte.decode(mode)


def text_to_b64(text):
    """b64编码"""
    return str(base64.b64encode(str(text).encode('utf-8')), 'utf-8')


def b64_to_text(text):
    """b64解码"""
    return str(base64.b64decode(str(text).encode('utf-8')), 'utf-8')


def byte_to_b64(byte):
    """b64加密"""
    return base64.b64encode(byte)


def byte_to_md5(byte):
    """计算md5"""
    m = hashlib.md5()
    m.update(byte)
    return m.hexdigest()