{"record":{"id":"d38c56b0b1e76540","repo":"iflytek/astron-agent","slug":"hmac-sha1-encryption-error-e","errorCode":null,"errorMessage":"HMAC-SHA1 encryption error: {e}","messagePattern":"HMAC-SHA1 encryption error: (.+?)","errorType":"exception","errorClass":"ThirdPartyException","httpStatus":null,"severity":"error","filePath":"core/knowledge/utils/spark_signature.py","lineNumber":68,"sourceCode":"def 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\")\n    except Exception as e:\n        logger.error(f\"HMAC-SHA1 encryption failed: {e}\")\n        raise ThirdPartyException(f\"HMAC-SHA1 encryption error: {e}\")\n","sourceCodeStart":50,"sourceCodeEnd":69,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/core/knowledge/utils/spark_signature.py#L50-L69","documentation":"hmac_sha1_encrypt computes base64(HMAC-SHA1(key, text)) used for Spark API auth signatures. Failures during key/text encoding or hmac construction are re-raised as ThirdPartyException via get_signature. Like md5(), the underlying operations rarely fail; the wrapper surfaces unexpected errors consistently.","triggerScenarios":"Calling get_signature() where encrypt_key or encrypt_text is not a str (e.g. None or bytes), causing .encode('utf-8') to fail.","commonSituations":"Config returns the API secret as bytes from env parsing, or the text to sign is None because an upstream field was missing.","solutions":["Verify encrypt_key and encrypt_text are str (convert bytes with .decode())","Check that the API key/secret is configured and not None","Inspect the logged 'HMAC-SHA1 encryption failed' message for the real exception"],"exampleFix":"// before\nmac = hmac_sha1_encrypt(text, key)  # key loaded as bytes\n// after\nif isinstance(key, bytes): key = key.decode('utf-8')\nmac = hmac_sha1_encrypt(text, key)","handlingStrategy":"validation","validationCode":"if not isinstance(encrypt_key, str) or not encrypt_key:\n    raise ValueError('encrypt_key must be a non-empty str')\nif not isinstance(encrypt_text, str):\n    encrypt_text = str(encrypt_text)","typeGuard":"def is_signable(text, key) -> bool: return isinstance(text, str) and isinstance(key, str) and bool(key)","tryCatchPattern":"try:\n    sig = get_signature(...)\nexcept ThirdPartyException as e:\n    raise AuthConfigError('invalid Spark signing inputs') from e","preventionTips":["Store API key/secret as str, decode bytes loaded from env/files","Validate key presence at startup, not at request time","Keep the signing text free of None fields"],"tags":["python","hmac","signature"],"backgroundTag":"missing-credentials","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"}