{"record":{"id":"b9f44db23261811d","repo":"iflytek/astron-agent","slug":"md5-computation-error-e","errorCode":null,"errorMessage":"MD5 computation error: {e}","messagePattern":"MD5 computation error: (.+?)","errorType":"exception","errorClass":"ThirdPartyException","httpStatus":null,"severity":"error","filePath":"core/knowledge/utils/spark_signature.py","lineNumber":47,"sourceCode":"\ndef md5(cipher_text: str) -> str:\n    \"\"\"\n    Generate MD5 hash value.\n\n    Args:\n        cipher_text: Text to be hashed\n\n    Returns:\n        MD5 hash string\n    \"\"\"\n    try:\n        data = cipher_text.encode(\"utf-8\")\n        md = hashlib.md5()\n        md.update(data)\n        return md.hexdigest()\n    except Exception as e:\n        logger.error(f\"MD5 computation failed: {e}\")\n        raise ThirdPartyException(f\"MD5 computation error: {e}\")\n\n\ndef hmac_sha1_encrypt(encrypt_text: str, encrypt_key: str) -> str:\n    \"\"\"\n    Encrypt text using HMAC-SHA1.\n\n    Args:\n        encrypt_text: Text to be encrypted\n        encrypt_key: Encryption key\n\n    Returns:\n        Base64 encoded encryption result\n    \"\"\"\n    try:\n        secret_key = encrypt_key.encode(\"utf-8\")\n        text = encrypt_text.encode(\"utf-8\")\n        mac = hmac.new(secret_key, text, hashlib.sha1)\n        return base64.b64encode(mac.digest()).decode(\"utf-8\")","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/utils/spark_signature.py#L29-L65","documentation":"md5() in core/knowledge/utils/spark_signature.py wraps hashlib.md5 hexdigest computation for Spark/讯飞 API signatures. Any failure encoding the cipher text or hashing raises ThirdPartyException so callers of get_signature see a normalized third-party error. In practice md5 over utf-8 bytes essentially never fails; this guards unexpected runtime conditions.","triggerScenarios":"Calling get_signature() when cipher_text contains characters that break encode('utf-8') (surrogates from bad decoding) or any unexpected exception inside the hashing block.","commonSituations":"Upstream text decoded with errors='surrogateescape' from a malformed response, or monkeypatched/unavailable hashlib in restricted environments.","solutions":["Check the origin of cipher_text; decode bytes with errors='replace' before passing it in","Ensure text is a str, not bytes or None, before calling get_signature","Log the inner exception (already logged via logger.error) to find the true cause"],"exampleFix":"// before\nsig = md5(raw_bytes)  # raises ThirdPartyException\n// after\nsig = md5(raw_bytes.decode('utf-8', errors='replace'))","handlingStrategy":"try-catch","validationCode":"if not isinstance(cipher_text, str): raise TypeError('cipher_text must be str')\ncipher_text.encode('utf-8')  # pre-validate encodability","typeGuard":"def is_valid_text(v) -> bool: return isinstance(v, str)","tryCatchPattern":"try:\n    sig = get_signature(...)\nexcept ThirdPartyException as e:\n    logger.warning('signature failed: %s', e); sig = None","preventionTips":["Ensure all inputs are decoded utf-8 str before signing","Surround signing with a narrow try/except and log the inner cause","Keep hashlib usage unpatched in restricted environments"],"tags":["python","hashing","signature"],"backgroundTag":"checksum-mismatch","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}