Ë
    #CeL/  ã                   ó    — d dl mZmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ  G d„ dej                  e
«      Z G d„ d	ej                  e
«      Zy)
é    )ÚClassVarÚOptionalN)Úencoding)Ú
exceptions)ÚEncryptedMessageÚStringFixerÚrandomc            	       óâ  — e Zd ZU dZej
                  j                  Zee	   e
d<   ej
                  j                  Zee	   e
d<   ej
                  j                  Zee	   e
d<   ej
                  j                  Zee	   e
d<   ej$                  fdedej(                  fd„Zd	efd
„Zdej$                  fdedee   dej(                  d	efd„Zdej$                  fdedee   dej(                  d	efd„Zy)Ú	SecretBoxuS  
    The SecretBox class encrypts and decrypts messages using the given secret
    key.

    The ciphertexts generated by :class:`~nacl.secret.Secretbox` include a 16
    byte authenticator which is checked as part of the decryption. An invalid
    authenticator will cause the decrypt function to raise an exception. The
    authenticator is not a signature. Once you've decrypted the message you've
    demonstrated the ability to create arbitrary valid message, so messages you
    send are repudiable. For non-repudiable messages, sign them after
    encryption.

    Encryption is done using `XSalsa20-Poly1305`_, and there are no practical
    limits on the number or size of messages (up to 2â¶â´ messages, each up to 2â¶â´
    bytes).

    .. _XSalsa20-Poly1305: https://doc.libsodium.org/secret-key_cryptography/secretbox#algorithm-details

    :param key: The secret key used to encrypt and decrypt messages
    :param encoder: The encoder class used to decode the given key

    :cvar KEY_SIZE: The size that the key is required to be.
    :cvar NONCE_SIZE: The size that the nonce is required to be.
    :cvar MACBYTES: The size of the authentication MAC tag in bytes.
    :cvar MESSAGEBYTES_MAX: The maximum size of a message which can be
                            safely encrypted with a single key/nonce
                            pair.
    ÚKEY_SIZEÚ
NONCE_SIZEÚMACBYTESÚMESSAGEBYTES_MAXÚkeyÚencoderc                 óò   — |j                  |«      }t        |t        «      st        j                  d«      ‚t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚|| _        y )Nz'SecretBox must be created from 32 bytesú%The key must be exactly %s bytes long©	ÚdecodeÚ
isinstanceÚbytesÚexcÚ	TypeErrorÚlenr   Ú
ValueErrorÚ_key©Úselfr   r   s      ú+lib/python3.12/site-packages/nacl/secret.pyÚ__init__zSecretBox.__init__;   sb   € ð n‰n˜SÓ!ˆÜ˜#œuÔ%Ü—-‘-Ð IÓJÐJäˆs‹8t—}‘}Ò$Ü—.‘.Ø7¸$¿-¹-ÑGóð ð ˆ	ó    Úreturnc                 ó   — | j                   S ©N©r   ©r   s    r   Ú	__bytes__zSecretBox.__bytes__I   ó   € Øy‰yÐr!   NÚ	plaintextÚnoncec                 ó  — |€t        | j                  «      }t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚t
        j                  j                  ||| j                  «      }|j                  |«      }|j                  |«      }t        j                  |||j                  ||z   «      «      S )aL  
        Encrypts the plaintext message using the given `nonce` (or generates
        one randomly if omitted) and returns the ciphertext encoded with the
        encoder.

        .. warning:: It is **VITALLY** important that the nonce is a nonce,
            i.e. it is a number used only once for any given key. If you fail
            to do this, you compromise the privacy of the messages encrypted.
            Give your nonces a different prefix, or have one side use an odd
            counter and one an even counter. Just make sure they are different.

        :param plaintext: [:class:`bytes`] The plaintext message to encrypt
        :param nonce: [:class:`bytes`] The nonce to use in the encryption
        :param encoder: The encoder to use to encode the ciphertext
        :rtype: [:class:`nacl.utils.EncryptedMessage`]
        ú'The nonce must be exactly %s bytes long)r	   r   r   r   r   ÚnaclÚbindingsÚcrypto_secretboxr   Úencoder   Ú_from_parts)r   r)   r*   r   Ú
ciphertextÚencoded_nonceÚencoded_ciphertexts          r   ÚencryptzSecretBox.encryptL   s®   € ð, ˆ=Ü˜4Ÿ?™?Ó+ˆEäˆu‹:˜Ÿ™Ò(Ü—.‘.Ø9¸D¿O¹OÑKóð ô —]‘]×3Ñ3Øu˜dŸi™ió
ˆ
ð  Ÿ™ uÓ-ˆØ$Ÿ^™^¨JÓ7Ðä×+Ñ+ØØØN‰N˜5 :Ñ-Ó.ó
ð 	
r!   r2   c                 ó2  — |j                  |«      }|€|d| j                   }|| j                  d }t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚t
        j                  j                  ||| j                  «      }|S ©aá  
        Decrypts the ciphertext using the `nonce` (explicitly, when passed as a
        parameter or implicitly, when omitted, as part of the ciphertext) and
        returns the plaintext message.

        :param ciphertext: [:class:`bytes`] The encrypted message to decrypt
        :param nonce: [:class:`bytes`] The nonce used when encrypting the
            ciphertext
        :param encoder: The encoder used to decode the ciphertext.
        :rtype: [:class:`bytes`]
        Nr,   )	r   r   r   r   r   r-   r.   Úcrypto_secretbox_openr   )r   r2   r*   r   r)   s        r   ÚdecryptzSecretBox.decryptw   s   € ð$ —^‘^ JÓ/ˆ
àˆ=àÐ0 §¡Ð1ˆEØ# D§O¡OÐ$5Ð6ˆJäˆu‹:˜Ÿ™Ò(Ü—.‘.Ø9¸D¿O¹OÑKóð ô —M‘M×7Ñ7Ø˜˜tŸy™yó
ˆ	ð Ðr!   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r-   r.   Úcrypto_secretbox_KEYBYTESr   r   ÚintÚ__annotations__Úcrypto_secretbox_NONCEBYTESr   Úcrypto_secretbox_MACBYTESr   Ú!crypto_secretbox_MESSAGEBYTES_MAXr   r   Ú
RawEncoderr   ÚEncoderr    r'   r   r   r5   r9   © r!   r   r   r      s-  … ñð: #Ÿm™m×EÑE€Hˆhs‰mÓEØ $§¡× IÑ I€J˜‘ÓIØ"Ÿm™m×EÑE€Hˆhs‰mÓEð 	‰×7Ñ7ð hØñó 8ð
 7?×6IÑ6IñØðØ#+×#3Ñ#3óð˜5ó ð "&Ø$,×$7Ñ$7ñ	)
àð)
ð ˜‰ð)
ð ×!Ñ!ð	)
ð
 
ó)
ð\ "&Ø$,×$7Ñ$7ñ	"àð"ð ˜‰ð"ð ×!Ñ!ð	"ð
 
ô"r!   r   c                   ó¬  — e Zd ZdZej
                  j                  Zej
                  j                  Z	ej
                  j                  Zej
                  j                  Zej                  fdedej"                  fd„Zdefd„Zddej                  fd	ed
edee   dej"                  def
d„Zddej                  fded
edee   dej"                  def
d„Zy)ÚAeaduñ  
    The AEAD class encrypts and decrypts messages using the given secret key.

    Unlike :class:`~nacl.secret.SecretBox`, AEAD supports authenticating
    non-confidential data received alongside the message, such as a length
    or type tag.

    Like :class:`~nacl.secret.Secretbox`, this class provides authenticated
    encryption. An inauthentic message will cause the decrypt function to raise
    an exception.

    Likewise, the authenticator should not be mistaken for a (public-key)
    signature: recipients (with the ability to decrypt messages) are capable of
    creating arbitrary valid message; in particular, this means AEAD messages
    are repudiable. For non-repudiable messages, sign them after encryption.

    The cryptosystem used is `XChacha20-Poly1305`_ as specified for
    `standardization`_. There are `no practical limits`_ to how much can safely
    be encrypted under a given key (up to 2â¶â´ messages each containing up
    to 2â¶â´ bytes).

    .. _standardization: https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-xchacha
    .. _XChacha20-Poly1305: https://doc.libsodium.org/secret-key_cryptography/aead#xchacha-20-poly1305
    .. _no practical limits: https://doc.libsodium.org/secret-key_cryptography/aead#limitations

    :param key: The secret key used to encrypt and decrypt messages
    :param encoder: The encoder class used to decode the given key

    :cvar KEY_SIZE: The size that the key is required to be.
    :cvar NONCE_SIZE: The size that the nonce is required to be.
    :cvar MACBYTES: The size of the authentication MAC tag in bytes.
    :cvar MESSAGEBYTES_MAX: The maximum size of a message which can be
                            safely encrypted with a single key/nonce
                            pair.
    r   r   c                 óò   — |j                  |«      }t        |t        «      st        j                  d«      ‚t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚|| _        y )Nz"AEAD must be created from 32 bytesr   r   r   s      r   r    zAead.__init__È   sb   € ð
 n‰n˜SÓ!ˆÜ˜#œuÔ%Ü—-‘-Ð DÓEÐEäˆs‹8t—}‘}Ò$Ü—.‘.Ø7¸$¿-¹-ÑGóð ð ˆ	r!   r"   c                 ó   — | j                   S r$   r%   r&   s    r   r'   zAead.__bytes__Ø   r(   r!   r!   Nr)   Úaadr*   c                 ó’  — |€t        | j                  «      }t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚t
        j                  j                  |||| j                  «      }|j                  |«      }|j                  |«      }t        j                  |||j                  ||z   «      «      S )a@  
        Encrypts the plaintext message using the given `nonce` (or generates
        one randomly if omitted) and returns the ciphertext encoded with the
        encoder.

        .. warning:: It is vitally important for :param nonce: to be unique.
            By default, it is generated randomly; [:class:`Aead`] uses XChacha20
            for extended (192b) nonce size, so the risk of reusing random nonces
            is negligible.  It is *strongly recommended* to keep this behaviour,
            as nonce reuse will compromise the privacy of encrypted messages.
            Should implicit nonces be inadequate for your application, the
            second best option is using split counters; e.g. if sending messages
            encrypted under a shared key between 2 users, each user can use the
            number of messages it sent so far, prefixed or suffixed with a 1bit
            user id.  Note that the counter must **never** be rolled back (due
            to overflow, on-disk state being rolled back to an earlier backup,
            ...)

        :param plaintext: [:class:`bytes`] The plaintext message to encrypt
        :param nonce: [:class:`bytes`] The nonce to use in the encryption
        :param encoder: The encoder to use to encode the ciphertext
        :rtype: [:class:`nacl.utils.EncryptedMessage`]
        r,   )r	   r   r   r   r   r-   r.   Ú*crypto_aead_xchacha20poly1305_ietf_encryptr   r0   r   r1   )r   r)   rK   r*   r   r2   r3   r4   s           r   r5   zAead.encryptÛ   s°   € ð< ˆ=Ü˜4Ÿ?™?Ó+ˆEäˆu‹:˜Ÿ™Ò(Ü—.‘.Ø9¸D¿O¹OÑKóð ô —]‘]×MÑMØs˜E 4§9¡9ó
ˆ
ð  Ÿ™ uÓ-ˆØ$Ÿ^™^¨JÓ7Ðä×+Ñ+ØØØN‰N˜5 :Ñ-Ó.ó
ð 	
r!   r2   c                 ó4  — |j                  |«      }|€|d| j                   }|| j                  d }t        |«      | j                  k7  r"t        j                  d| j                  z  «      ‚t
        j                  j                  |||| j                  «      }|S r7   )	r   r   r   r   r   r-   r.   Ú*crypto_aead_xchacha20poly1305_ietf_decryptr   )r   r2   rK   r*   r   r)   s         r   r9   zAead.decrypt  s‘   € ð& —^‘^ JÓ/ˆ
àˆ=àÐ0 §¡Ð1ˆEØ# D§O¡OÐ$5Ð6ˆJäˆu‹:˜Ÿ™Ò(Ü—.‘.Ø9¸D¿O¹OÑKóð ô —M‘M×LÑLØ˜˜U D§I¡Ió
ˆ	ð Ðr!   )r:   r;   r<   r=   r-   r.   Ú+crypto_aead_xchacha20poly1305_ietf_KEYBYTESr   Ú,crypto_aead_xchacha20poly1305_ietf_NPUBBYTESr   Ú)crypto_aead_xchacha20poly1305_ietf_ABYTESr   Ú3crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAXr   r   rD   r   rE   r    r'   r   r   r5   r9   rF   r!   r   rH   rH   œ   s  „ ñ"ðH }‰}×HÑH€HØ—‘×KÑK€JØ}‰}×FÑF€Hà‰×IÑIð ð %-×$7Ñ$7ñàðð ×!Ñ!óð ˜5ó ð Ø!%Ø$,×$7Ñ$7ñ1
àð1
ð ð1
ð ˜‰ð	1
ð
 ×!Ñ!ð1
ð 
ó1
ðl Ø!%Ø$,×$7Ñ$7ñ#àð#ð ð#ð ˜‰ð	#ð
 ×!Ñ!ð#ð 
ô#r!   rH   )Útypingr   r   Únacl.bindingsr-   r   r   r   Ú
nacl.utilsr   r   r	   Ú	Encodabler   rH   rF   r!   r   ú<module>rX      sF   ð÷ &ã Ý Ý "ß <Ñ <ôC×"Ñ" Kô CôLUˆ8×Ñ˜{õ Ur!   