U
    ÎZj�+  ã                   @   sj  U d dl mZmZmZmZ d dlmZ d dlm	Z	m
Z
 d dlmZ e
 ¡ Zeed< e
 ¡ Zeed< e
 ¡ Zeed< e
 ¡ Zeed< e
 ¡ Zeed	< e
 ¡ Zeed
< e
 ¡ Zeed< e
 ¡ Zeed< e
 ¡ Z eed< e!dœdd„Z"G dd„ dƒZ#e#e!e!dœdd„Z$defe#e!ee! ee!dœdd„Z%e#e!e!ddœdd„Z&d#e#e!ee! ee!ef dœdd„Z'e#dd œd!d"„Z(dS )$é    )Ú
ByteStringÚOptionalÚTupleÚcast)Ú
exceptions)ÚffiÚlib)ÚensureÚ,crypto_secretstream_xchacha20poly1305_ABYTESÚ1crypto_secretstream_xchacha20poly1305_HEADERBYTESÚ.crypto_secretstream_xchacha20poly1305_KEYBYTESÚ6crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAXÚ0crypto_secretstream_xchacha20poly1305_STATEBYTESÚ1crypto_secretstream_xchacha20poly1305_TAG_MESSAGEÚ.crypto_secretstream_xchacha20poly1305_TAG_PUSHÚ/crypto_secretstream_xchacha20poly1305_TAG_REKEYÚ/crypto_secretstream_xchacha20poly1305_TAG_FINAL©Úreturnc                  C   s(   t  dt¡} t | ¡ t  | ¡dd… S )zd
    Generate a key for use with
    :func:`.crypto_secretstream_xchacha20poly1305_init_push`.

    úunsigned char[]N)r   Únewr   r   Ú,crypto_secretstream_xchacha20poly1305_keygenÚbuffer)Zkeybuf© r   úd/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/nacl/bindings/crypto_secretstream.pyr   4   s    þ
r   c                   @   s(   e Zd ZdZdddgZddœdd„ZdS )	Ú+crypto_secretstream_xchacha20poly1305_statezN
    An object wrapping the crypto_secretstream_xchacha20poly1305 state.

    ÚstatebufÚrawbufÚtagbufNr   c                 C   s   t  dt¡| _d| _d| _dS )z Initialize a clean state object.r   N)r   r   r   r   r   r   )Úselfr   r   r   Ú__init__J   s    þz4crypto_secretstream_xchacha20poly1305_state.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	__slots__r    r   r   r   r   r   B   s   
r   )ÚstateÚkeyr   c                 C   s†   t t| tƒdtjd� t t|tƒdtjd� t t|ƒtkdtjd� t	 
dt¡}t | j||¡}t |dkdtjd� t	 |¡dd… S )	a\  
    Initialize a crypto_secretstream_xchacha20poly1305 encryption buffer.

    :param state: a secretstream state object
    :type state: crypto_secretstream_xchacha20poly1305_state
    :param key: must be
                :data:`.crypto_secretstream_xchacha20poly1305_KEYBYTES` long
    :type key: bytes
    :return: header
    :rtype: bytes

    úBState must be a crypto_secretstream_xchacha20poly1305_state object©ZraisingúKey must be a bytes sequenceúInvalid key lengthzunsigned char []r   úUnexpected failureN)r	   Ú
isinstancer   ÚexcÚ	TypeErrorÚbytesÚlenr   Ú
ValueErrorr   r   r   r   Ú/crypto_secretstream_xchacha20poly1305_init_pushr   ÚRuntimeErrorr   )r&   r'   Z	headerbufÚrcr   r   r   r3   U   s4    ýý
ýþ  ÿr3   N)r&   ÚmÚadÚtagr   c              
   C   sü   t t| tƒdtjd� t t|tƒdtjd� t t|ƒtkdtjd� t |dkpVt|tƒdtjd� t|ƒt	 }| j
dks†t| j
ƒ|k r”t d|¡| _
|dkr¨tj}d}nt|ƒ}t | j| j
tj|t|ƒ|||¡}t |dkd	tjd� t | j
|¡dd… S )
aÈ  
    Add an encrypted message to the secret stream.

    :param state: a secretstream state object
    :type state: crypto_secretstream_xchacha20poly1305_state
    :param m: the message to encrypt, the maximum length of an individual
              message is
              :data:`.crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX`.
    :type m: bytes
    :param ad: additional data to include in the authentication tag
    :type ad: bytes or None
    :param tag: the message tag, usually
                :data:`.crypto_secretstream_xchacha20poly1305_TAG_MESSAGE` or
                :data:`.crypto_secretstream_xchacha20poly1305_TAG_FINAL`.
    :type tag: int
    :return: ciphertext
    :rtype: bytes

    r(   r)   zMessage is not byteszMessage is too longNú%Additional data must be bytes or Noner   r   r,   )r	   r-   r   r.   r/   r0   r1   r   r2   r
   r   r   r   ÚNULLr   Ú*crypto_secretstream_xchacha20poly1305_pushr   r4   r   )r&   r6   r7   r8   ZclenÚadlenr5   r   r   r   r;   �   sF    ý
ýýø
r;   )r&   Úheaderr'   r   c                 C   s°   t t| tƒdtjd� t t|tƒdtjd� t t|ƒtkdtjd� t t|tƒdtjd� t t|ƒt	kdtjd� | j
dkrˆt d¡| _
t | j||¡}t |d	kd
tjd� dS )a¹  
    Initialize a crypto_secretstream_xchacha20poly1305 decryption buffer.

    :param state: a secretstream state object
    :type state: crypto_secretstream_xchacha20poly1305_state
    :param header: must be
                :data:`.crypto_secretstream_xchacha20poly1305_HEADERBYTES` long
    :type header: bytes
    :param key: must be
                :data:`.crypto_secretstream_xchacha20poly1305_KEYBYTES` long
    :type key: bytes

    r(   r)   zHeader must be a bytes sequencezInvalid header lengthr*   r+   Nzunsigned char *r   r,   )r	   r-   r   r.   r/   r0   r1   r   r2   r   r   r   r   r   Ú/crypto_secretstream_xchacha20poly1305_init_pullr   r4   )r&   r=   r'   r5   r   r   r   r>   Ä   sB    ýý
ýý
ý
  ÿr>   )r&   Úcr7   r   c              
   C   sD  t t| tƒdtjd� t | jdk	dtjd� t t|tƒdtjd� t t|ƒt	kdtjd� t t|ƒt
t	 kdtjd� t |dkpˆt|tƒdtjd� t|ƒt	 }| jdks¸t| jƒ|k rÆt d	|¡| _|dkrÚtj}d
}nt|ƒ}t | j| jtj| j|t|ƒ||¡}t |d
kdtjd� t | j|¡dd… ttt| jƒd
 ƒfS )aM  
    Read a decrypted message from the secret stream.

    :param state: a secretstream state object
    :type state: crypto_secretstream_xchacha20poly1305_state
    :param c: the ciphertext to decrypt, the maximum length of an individual
              ciphertext is
              :data:`.crypto_secretstream_xchacha20poly1305_MESSAGEBYTES_MAX` +
              :data:`.crypto_secretstream_xchacha20poly1305_ABYTES`.
    :type c: bytes
    :param ad: additional data to include in the authentication tag
    :type ad: bytes or None
    :return: (message, tag)
    :rtype: (bytes, int)

    r(   r)   NzOState must be initialized using crypto_secretstream_xchacha20poly1305_init_pullzCiphertext is not byteszCiphertext is too shortzCiphertext is too longr9   r   r   r,   )r	   r-   r   r.   r/   r   r2   r0   r1   r
   r   r   r   r   r:   r   Ú*crypto_secretstream_xchacha20poly1305_pullr   r4   r   Úintr   )r&   r?   r7   Úmlenr<   r5   r   r   r   r@   ù   sn    ýúý
ýÿþù	ýø
þr@   )r&   r   c                 C   s&   t t| tƒdtjd� t | j¡ dS )aÄ  
    Explicitly change the encryption key in the stream.

    Normally the stream is re-keyed as needed or an explicit ``tag`` of
    :data:`.crypto_secretstream_xchacha20poly1305_TAG_REKEY` is added to a
    message to ensure forward secrecy, but this method can be used instead
    if the re-keying is controlled without adding the tag.

    :param state: a secretstream state object
    :type state: crypto_secretstream_xchacha20poly1305_state

    r(   r)   N)r	   r-   r   r.   r/   r   Ú+crypto_secretstream_xchacha20poly1305_rekeyr   )r&   r   r   r   rC   Q  s    ýrC   )N))Útypingr   r   r   r   Znaclr   r.   Znacl._sodiumr   r   Znacl.exceptionsr	   Z,crypto_secretstream_xchacha20poly1305_abytesr
   rA   Ú__annotations__Z1crypto_secretstream_xchacha20poly1305_headerbytesr   Z.crypto_secretstream_xchacha20poly1305_keybytesr   Z6crypto_secretstream_xchacha20poly1305_messagebytes_maxr   Z0crypto_secretstream_xchacha20poly1305_statebytesr   Z1crypto_secretstream_xchacha20poly1305_tag_messager   Z.crypto_secretstream_xchacha20poly1305_tag_pushr   Z/crypto_secretstream_xchacha20poly1305_tag_rekeyr   Z/crypto_secretstream_xchacha20poly1305_tag_finalr   r0   r   r   r3   r;   r>   r@   rC   r   r   r   r   Ú<module>   sh    ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
ÿ
 þ/üûDü8 ý
üYþ