iflytek/astron-agent · error · ThirdPartyException

HMAC-SHA1 encryption error

Error message

HMAC-SHA1 encryption error: {e}

What it means

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.

Solutions

  1. Verify encrypt_key and encrypt_text are str (convert bytes with .decode())
  2. Check that the API key/secret is configured and not None
  3. Inspect the logged 'HMAC-SHA1 encryption failed' message for the real exception

Example fix

// before
mac = hmac_sha1_encrypt(text, key)  # key loaded as bytes
// after
if isinstance(key, bytes): key = key.decode('utf-8')
mac = hmac_sha1_encrypt(text, key)
Defensive patterns

Strategy: validation

Validate before calling

if not isinstance(encrypt_key, str) or not encrypt_key:
    raise ValueError('encrypt_key must be a non-empty str')
if not isinstance(encrypt_text, str):
    encrypt_text = str(encrypt_text)

Type guard

def is_signable(text, key) -> bool: return isinstance(text, str) and isinstance(key, str) and bool(key)

Try / catch

try:
    sig = get_signature(...)
except ThirdPartyException as e:
    raise AuthConfigError('invalid Spark signing inputs') from e

Prevention

When it happens

Trigger: Calling get_signature() where encrypt_key or encrypt_text is not a str (e.g. None or bytes), causing .encode('utf-8') to fail.

Common situations: Config returns the API secret as bytes from env parsing, or the text to sign is None because an upstream field was missing.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/d38c56b0b1e76540. Report an issue: GitHub.

Appendix: source

Thrown at core/knowledge/utils/spark_signature.py:68

def hmac_sha1_encrypt(encrypt_text: str, encrypt_key: str) -> str:
    """
    Encrypt text using HMAC-SHA1.

    Args:
        encrypt_text: Text to be encrypted
        encrypt_key: Encryption key

    Returns:
        Base64 encoded encryption result
    """
    try:
        secret_key = encrypt_key.encode("utf-8")
        text = encrypt_text.encode("utf-8")
        mac = hmac.new(secret_key, text, hashlib.sha1)
        return base64.b64encode(mac.digest()).decode("utf-8")
    except Exception as e:
        logger.error(f"HMAC-SHA1 encryption failed: {e}")
        raise ThirdPartyException(f"HMAC-SHA1 encryption error: {e}")

View on GitHub (pinned to 5e758547a8)