from __future__ import annotations import base64 import hashlib import struct def verify_signature(token: str, timestamp: str, nonce: str, signature: str) -> bool: params = sorted([token, timestamp, nonce]) sha1 = hashlib.sha1("".join(params).encode()).hexdigest() return sha1 == signature def decrypt_message( encrypted_msg: str, encoding_aes_key: str, app_id: str, ) -> str: import socket from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes key = base64.b64decode(encoding_aes_key + "=") ciphertext = base64.b64decode(encrypted_msg) cipher = Cipher(algorithms.AES(key), modes.CBC(key[:16])) decryptor = cipher.decryptor() plaintext = decryptor.update(ciphertext) + decryptor.finalize() pad = plaintext[-1] plaintext = plaintext[:-pad] content_len = socket.ntohl(struct.unpack("I", plaintext[16:20])[0]) content = plaintext[20 : 20 + content_len].decode("utf-8") return content def verify_url_echostr(token: str, timestamp: str, nonce: str, echostr: str, signature: str) -> tuple[bool, str]: if not verify_signature(token, timestamp, nonce, signature): return False, "" return True, echostr