
    i                    6    d Z ddlmZ ddlZddlZd
dZdd	ZdS )zHAES-256-GCM utilities for QQBot scan-to-configure credential decryption.    )annotationsNreturnstrc                 r    t          j        t          j        d                                                    S )a  Generate a 256-bit random AES key and return it as base64.

    The key is passed to ``create_bind_task`` so the server can encrypt
    the bot's *client_secret* before returning it.  Only this CLI holds
    the key, ensuring the secret never travels in plaintext.
        )base64	b64encodeosurandomdecode     C/home/ubuntu/.hermes/hermes-agent/gateway/platforms/qqbot/crypto.pygenerate_bind_keyr   	   s)     BJrNN++22444r   encrypted_base64
key_base64c                    ddl m} t          j        |          }t          j        |           }|dd         }|dd         } ||          }|                    ||d          }|                    d          S )u  Decrypt a base64-encoded AES-256-GCM ciphertext.

    Ciphertext layout (after base64-decoding)::

        IV (12 bytes) ‖ ciphertext (N bytes) ‖ AuthTag (16 bytes)

    Args:
        encrypted_base64: The ``bot_encrypt_secret`` value from
            ``poll_bind_result``.
        key_base64: The base64 AES key generated by
            :func:`generate_bind_key`.

    Returns:
        The decrypted *client_secret* as a UTF-8 string.
    r   )AESGCMN   zutf-8)+cryptography.hazmat.primitives.ciphers.aeadr   r   	b64decodedecryptr   )	r   r   r   keyrawivciphertext_with_tagaesgcm	plaintexts	            r   decrypt_secretr      s      CBBBBB

:
&
&C

+
,
,C	SbSBbcc(VC[[Fr#6==IG$$$r   )r   r   )r   r   r   r   r   r   )__doc__
__future__r   r   r
   r   r   r   r   r   <module>r"      sa    N N " " " " " "  				5 5 5 5% % % % % %r   