
    ,h-                         d dl mZ d dlmZ d dlmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d	 Z ed
dd      Z G d de      Zd ZdZy)    )	unhexlify)ChaCha20)
_HChaCha20)Poly1305BLAKE2s)get_random_bytes)long_to_bytes)_copy_bytesbord)	is_bufferc                      t        dd|       S )NEnum )type)enumss    a/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/Crypto/Cipher/ChaCha20_Poly1305.py_enumr   ,   s    E""             )PROCESSING_AUTH_DATAPROCESSING_CIPHERTEXTPROCESSING_DONEc                   \    e Zd ZdZd Zd Zd ZddZddZd Z	d	 Z
d
 Zd Zd Zd Zd Zy)ChaCha20Poly1305CipherzChaCha20-Poly1305 and XChaCha20-Poly1305 cipher object.
    Do not create it directly. Use :py:func:`new` instead.

    :var nonce: The nonce with length 8, 12 or 24 bytes
    :vartype nonce: byte string
    c                    d| _         t        j                  ||t              | _        t        j                  ||      | _        | j
                  j                  d       d| _        d| _        d| _	        t        j                  | _        y)z`Initialize a ChaCha20-Poly1305 AEAD cipher object

        See also `new()` at the module level.)updateencryptdecryptdigestverify)keynoncecipher)r#   r$   @   r   N)_nextr   newr   _authenticator_cipherseek_len_aad_len_ct_mac_tag_CipherStatusr   _status)selfr#   r$   s      r   __init__zChaCha20Poly1305Cipher.__init__=   sh    
 
 'lls%Q||59"$99r   c                     d| j                   vrt        d      | xj                  t        |      z  c_        | j                  j                  |       y)a  Protect the associated data.

        Associated data (also known as *additional authenticated data* - AAD)
        is the piece of the message that must stay in the clear, while
        still allowing the receiver to verify its integrity.
        An example is packet headers.

        The associated data (possibly split into multiple segments) is
        fed into :meth:`update` before any call to :meth:`decrypt` or :meth:`encrypt`.
        If there is no associated data, :meth:`update` is not called.

        :param bytes/bytearray/memoryview assoc_data:
            A piece of associated data. There are no restrictions on its size.
        r   z update() method cannot be calledN)r'   	TypeErrorr,   lenr)   r   )r1   datas     r   r   zChaCha20Poly1305Cipher.updateO   sB      4::%>??T"""4(r   c                     | j                   t        j                  k(  sJ | j                  dz  r.| j                  j                  dd| j                  dz  z
  z         t        j                  | _         y )N          )r0   r/   r   r,   r)   r   r   r1   s    r   _pad_aadzChaCha20Poly1305Cipher._pad_aade   sW    ||}AAAB==4&&w"8L2M'NO$::r   Nc                    d| j                   vrt        d      | j                  t        j                  k(  r| j                          d| _         | j                  j                  ||      }| xj                  t        |      z  c_        || j                  j                  |       |S | j                  j                  |       |S )a  Encrypt a piece of data.

        Args:
          plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the ciphertext
            is written to. If ``None``, the ciphertext is returned.
        Returns:
          If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
          Otherwise, ``None``.
        r   z!encrypt() method cannot be calledr   r!   output)r'   r4   r0   r/   r   r<   r*   r   r-   r5   r)   r   )r1   	plaintextr@   results       r   r   zChaCha20Poly1305Cipher.encryptl   s     DJJ&?@@<<====MMO*
%%i%?I&>&&v.  &&v.r   c                 H   d| j                   vrt        d      | j                  t        j                  k(  r| j                          d| _         | xj                  t        |      z  c_        | j                  j                  |       | j                  j                  ||      S )a  Decrypt a piece of data.

        Args:
          ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the plaintext
            is written to. If ``None``, the plaintext is returned.
        Returns:
          If ``output`` is ``None``, the plaintext is returned as ``bytes``.
          Otherwise, ``None``.
        r    z!decrypt() method cannot be calledr    r"   r?   )r'   r4   r0   r/   r   r<   r-   r5   r)   r   r*   r    )r1   
ciphertextr@   s      r   r    zChaCha20Poly1305Cipher.decrypt   s~     DJJ&?@@<<====MMO*
J'"":.||##Jv#>>r   c                    | j                   r+| j                  t        j                  k(  sJ | j                   S | j                  t        j                  k7  sJ | j                  t        j                  k(  r| j                          | j                  dz  r.| j                  j                  dd| j                  dz  z
  z         t        j                  | _        | j                  j                  t        | j                  d      ddd          | j                  j                  t        | j                  d      ddd          | j                  j                         | _         | j                   S )z=Finalize the cipher (if not done already) and return the MAC.r8   r9   r:      N)r.   r0   r/   r   r   r<   r-   r)   r   r	   r,   r!   r;   s    r   _compute_macz#ChaCha20Poly1305Cipher._compute_mac   s
    ==<<=#@#@@A== ||}<<<=<<====MMO<<$&&w"t8K2L'MN$44""=#B4R4#HI""=q#A$B$#GH++224}}r   c                 b    d| j                   vrt        d      d| _         | j                         S )zgCompute the *binary* authentication tag (MAC).

        :Return: the MAC tag, as 16 ``bytes``.
        r!   z digest() method cannot be called)r!   )r'   r4   rI   r;   s    r   r!   zChaCha20Poly1305Cipher.digest   s2     4::%>?? 
  ""r   c           	      ~    dj                  | j                         D cg c]  }dt        |      z   c}      S c c}w )zCompute the *printable* authentication tag (MAC).

        This method is like :meth:`digest`.

        :Return: the MAC tag, as a hexadecimal string.
         z%02x)joinr!   r   )r1   xs     r   	hexdigestz ChaCha20Poly1305Cipher.hexdigest   s0     ww$++-@Qa(@AA@s   :c                 F   d| j                   vrt        d      d| _         t        d      }| j                          t	        j
                  d|| j                        }t	        j
                  d||      }|j                         |j                         k7  rt        d      y)	a  Validate the *binary* authentication tag (MAC).

        The receiver invokes this method at the very end, to
        check if the associated data (if any) and the decrypted
        messages are valid.

        :param bytes/bytearray/memoryview received_mac_tag:
            This is the 16-byte *binary* MAC, as received from the sender.
        :Raises ValueError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        r"   z3verify() cannot be called when encrypting a message)r"   r:      )digest_bitsr#   r6   zMAC check failedN)	r'   r4   r   rI   r   r(   r.   r!   
ValueError)r1   received_mac_tagsecretmac1mac2s        r   r"   zChaCha20Poly1305Cipher.verify   s     4::% 9 : : 
!"%{{s $/{{s 02 ;;=DKKM)/00 *r   c                 8    | j                  t        |             y)a@  Validate the *printable* authentication tag (MAC).

        This method is like :meth:`verify`.

        :param string hex_mac_tag:
            This is the *printable* MAC.
        :Raises ValueError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        N)r"   r   )r1   hex_mac_tags     r   	hexverifyz ChaCha20Poly1305Cipher.hexverify   s     	Ik*+r   c                 D    | j                  |      | j                         fS )aH  Perform :meth:`encrypt` and :meth:`digest` in one step.

        :param plaintext: The data to encrypt, of any size.
        :type plaintext: bytes/bytearray/memoryview
        :return: a tuple with two ``bytes`` objects:

            - the ciphertext, of equal length as the plaintext
            - the 16-byte MAC tag
        r>   )r1   rA   s     r   encrypt_and_digestz)ChaCha20Poly1305Cipher.encrypt_and_digest   s     ||I&55r   c                 J    | j                  |      }| j                  |       |S )a  Perform :meth:`decrypt` and :meth:`verify` in one step.

        :param ciphertext: The piece of data to decrypt.
        :type ciphertext: bytes/bytearray/memoryview
        :param bytes received_mac_tag:
            This is the 16-byte *binary* MAC, as received from the sender.
        :return: the decrypted data (as ``bytes``)
        :raises ValueError:
            if the MAC does not match. The message has been tampered with
            or the key is incorrect.
        rD   )r1   rE   rT   rA   s       r   decrypt_and_verifyz)ChaCha20Poly1305Cipher.decrypt_and_verify  s%     LL,	$%r   )N)__name__
__module____qualname____doc__r2   r   r<   r   r    rI   r!   rO   r"   rZ   r\   r^   r   r   r   r   r   5   sD    :$),;:?2,
#B1>,6r   r   c                     	 | j                  d      }t        |      dk7  rt	        d      | j                  dd      }|t        d      }t        |      dv r|}n1t        |      d	k(  rt        ||dd
       }d|d
d z   }nt	        d      t        |      st        d      | rt        dt        |       z         t        ||      }t        dd|      |_        |S # t        $ r}t        d|z        d}~ww xY w)a  Create a new ChaCha20-Poly1305 or XChaCha20-Poly1305 AEAD cipher.

    :keyword key: The secret key to use. It must be 32 bytes long.
    :type key: byte string

    :keyword nonce:
        A value that must never be reused for any other encryption
        done with this key.

        For ChaCha20-Poly1305, it must be 8 or 12 bytes long.

        For XChaCha20-Poly1305, it must be 24 bytes long.

        If not provided, 12 ``bytes`` will be generated randomly
        (you can find them back in the ``nonce`` attribute).
    :type nonce: bytes, bytearray, memoryview

    :Return: a :class:`Crypto.Cipher.ChaCha20.ChaCha20Poly1305Cipher` object
    r#   zMissing parameter %sN    zKey must be 32 bytes longr$      )rG   re      r:   s       z$Nonce must be 8, 12 or 24 bytes longz,nonce must be bytes, bytearray or memoryviewzUnknown parameters: )popKeyErrorr4   r5   rS   r   r   r   strr   r
   r$   )kwargsr#   er$   chacha20_poly1305_noncer%   s         r   r(   r(     s   *4jj 3x2~455JJw%E} $
5zW"'	Ur	eCRj)"5bc
"B?@@UFGG.V<==#C)@AFtT51FLM5  4.2334s   C 	C7$C22C7rd   N)binasciir   Crypto.Cipherr   Crypto.Cipher.ChaCha20r   Crypto.Hashr   r   Crypto.Randomr   Crypto.Util.numberr	   Crypto.Util.py3compatr
   r   Crypto.Util._raw_apir   r   r/   objectr   r(   key_sizer   r   r   <module>rw      sT   >  " - ) * , 3 *# 1,-&')
aV aH1j r   