utils 源代码

import hashlib
import socket
import psutil
import gmalg
from Crypto.Util.Padding import unpad


[文档] def get_sign(dynamicSecret, params): """ 获取sign值 :param str dynamicSecret: login后自动获取,来自 login-token 请求 :param str params: URL请求参数 :return: sign值 :rtype: str """ paramsDict = {} for param in params.split("&"): if param.split("=")[0] == "timestamp": timestamp = param.split("=")[1] elif param.split("=")[0] == "random": random = param.split("=")[1] else: paramsDict[param.split("=")[0]] = param.split("=")[1] paramsDict = dict(sorted(paramsDict.items())) original = f"{dynamicSecret}|" for key in paramsDict: original += f"{paramsDict[key]}|" original += f"{timestamp}|{random}" sign = hashlib.md5(original.encode("utf-8")).hexdigest().upper() return sign
def _kget(kwargs, key, default=None): return kwargs[key] if key in kwargs else default
[文档] def get_ip_by_interface(interface): addresses = psutil.net_if_addrs() if interface in addresses: for addr in addresses[interface]: if addr.family == socket.AF_INET: return addr.address return None
[文档] def get_default_interface(): net_if_addrs = psutil.net_if_addrs() net_if_stats = psutil.net_if_stats() for interface, addrs in net_if_addrs.items(): if net_if_stats[interface].isup: for addr in addrs: if addr.family == socket.AF_INET: return interface return None
[文档] def sm4_decrypt_ecb(ciphertext: bytes, key: bytes): """ SM4 解密,ECB模式 :param bytes ciphertext: 密文 :param bytes key: 密钥 :return: 明文 Hex :rtype: str """ sm4 = gmalg.SM4(key) block_size = 16 decrypted_padded = b"" for i in range(0, len(ciphertext), block_size): block = ciphertext[i : i + block_size] decrypted_padded += sm4.decrypt(block) decrypted = unpad(decrypted_padded, block_size) return decrypted.decode()