o
    FD©i§7  ã                
   @   s(  U d dl mZ d dlmZ d dlmZmZ d dlm	Z	 ddg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< deeef fdd„Z dedeeef fdd„Z!dededededef
dd„Z"dededededef
dd„Z#dededefdd„Z$dedededefd d!„Z%dedededefd"d#„Z&dededededef
d$d%„Z'dededededef
d&d'„Z(dedededefd(d)„Z)dedededefd*d+„Z*dededefd,d-„Z+dedededefd.d/„Z,d0S )1é    )ÚTuple)Ú
exceptions)ÚffiÚlib)ÚensureÚcrypto_box_keypairÚ
crypto_boxÚcrypto_box_SECRETKEYBYTESÚcrypto_box_PUBLICKEYBYTESÚcrypto_box_SEEDBYTESÚcrypto_box_NONCEBYTESÚcrypto_box_ZEROBYTESÚcrypto_box_BOXZEROBYTESÚcrypto_box_BEFORENMBYTESÚcrypto_box_SEALBYTESÚcrypto_box_MACBYTESÚreturnc                  C   s`   t  dt¡} t  dt¡}t | |¡}t|dkdtjd t  	| t¡dd… t  	|t¡dd… fS )zq
    Returns a randomly generated public and secret key.

    :rtype: (bytes(public_key), bytes(secret_key))
    úunsigned char[]r   úUnexpected library error©ÚraisingN)
r   Únewr
   r	   r   r   r   ÚexcÚRuntimeErrorÚbuffer)ÚpkÚskÚrc© r   úQ/var/www/Datamplify/venv/lib/python3.10/site-packages/nacl/bindings/crypto_box.pyr   #   s   þÚseedc                 C   sŒ   t t| tƒdtd t| ƒtkrt d¡‚t 	dt
¡}t 	dt¡}t ||| ¡}t |dkdtjd t |t
¡dd… t |t¡dd… fS )a  
    Returns a (public, secret) key pair deterministically generated
    from an input ``seed``.

    .. warning:: The seed **must** be high-entropy; therefore,
        its generator **must** be a cryptographic quality
        random function like, for example, :func:`~nacl.utils.random`.

    .. warning:: The seed **must** be protected and remain secret.
        Anyone who knows the seed is really in possession of
        the corresponding PrivateKey.


    :param seed: bytes
    :rtype: (bytes(public_key), bytes(secret_key))
    zseed must be bytesr   zInvalid seedr   r   r   N)r   Ú
isinstanceÚbytesÚ	TypeErrorÚlenr   r   Ú
ValueErrorr   r   r
   r	   r   Úcrypto_box_seed_keypairr   r   )r    r   r   r   r   r   r   r&   5   s   
þr&   ÚmessageÚnoncer   r   c                 C   ó¢   t |ƒtkrt d¡‚t |ƒtkrt d¡‚t |ƒtkr!t d¡‚dt |  }t dt |ƒ¡}t	 
||t |ƒ|||¡}t|dkdtjd t |t |ƒ¡td	… S )
úð
    Encrypts and returns a message ``message`` using the secret key ``sk``,
    public key ``pk``, and the nonce ``nonce``.

    :param message: bytes
    :param nonce: bytes
    :param pk: bytes
    :param sk: bytes
    :rtype: bytes
    úInvalid nonce sizeúInvalid public keyúInvalid secret keyó    r   r   r   r   N)r$   r   r   r%   r
   r	   r   r   r   r   r   r   r   r   r   )r'   r(   r   r   ÚpaddedÚ
ciphertextr   r   r   r   r   W   s   


r0   c                 C   r)   )
á  
    Decrypts and returns an encrypted message ``ciphertext``, using the secret
    key ``sk``, public key ``pk``, and the nonce ``nonce``.

    :param ciphertext: bytes
    :param nonce: bytes
    :param pk: bytes
    :param sk: bytes
    :rtype: bytes
    r+   r,   r-   r.   r   r   ú/An error occurred trying to decrypt the messager   N)r$   r   r   r%   r
   r	   r   r   r   r   Úcrypto_box_openr   ÚCryptoErrorr   r   )r0   r(   r   r   r/   Ú	plaintextÚresr   r   r   r3   t   s   


ýr3   c                 C   sn   t | ƒtkrt d¡‚t |ƒtkrt d¡‚t dt¡}t 	|| |¡}t
|dkdtjd t |t¡dd… S )a  
    Computes and returns the shared key for the public key ``pk`` and the
    secret key ``sk``. This can be used to speed up operations where the same
    set of keys is going to be used multiple times.

    :param pk: bytes
    :param sk: bytes
    :rtype: bytes
    r,   r-   r   r   r   r   N)r$   r
   r   r%   r	   r   r   r   r   Úcrypto_box_beforenmr   r   r   )r   r   Úkr   r   r   r   r7   —   s   


r7   r8   c                 C   óŠ   t |ƒtkrt d¡‚t |ƒtkrt d¡‚dt |  }t dt |ƒ¡}t 	||t |ƒ||¡}t
|dkdtjd t |t |ƒ¡td… S )	úÇ
    Encrypts and returns the message ``message`` using the shared key ``k`` and
    the nonce ``nonce``.

    :param message: bytes
    :param nonce: bytes
    :param k: bytes
    :rtype: bytes
    úInvalid nonceúInvalid shared keyr.   r   r   r   r   N)r$   r   r   r%   r   r   r   r   r   Úcrypto_box_afternmr   r   r   r   )r'   r(   r8   r/   r0   r   r   r   r   r=   ¯   s   


r=   c                 C   r9   )	úØ
    Decrypts and returns the encrypted message ``ciphertext``, using the shared
    key ``k`` and the nonce ``nonce``.

    :param ciphertext: bytes
    :param nonce: bytes
    :param k: bytes
    :rtype: bytes
    r;   r<   r.   r   r   r2   r   N)r$   r   r   r%   r   r   r   r   r   Úcrypto_box_open_afternmr   r4   r   r   )r0   r(   r8   r/   r5   r6   r   r   r   r?   È   s   

ýr?   c                 C   sš   t |ƒtkrt d¡‚t |ƒtkrt d¡‚t |ƒtkr!t d¡‚t | ƒ}t| }t d|¡}t	 
|| ||||¡}t|dkdtjd t ||¡dd… S )	r*   r+   r,   r-   r   r   r   r   N)r$   r   r   r%   r
   r	   r   r   r   r   Úcrypto_box_easyr   r   r   )r'   r(   r   r   Ú_mlenÚ_clenr0   r   r   r   r   r@   ç   s   


r@   c                 C   sº   t |ƒtkrt d¡‚t |ƒtkrt d¡‚t |ƒtkr!t d¡‚t | ƒ}t|tkd t¡tj	d |t }t
 dtd|ƒ¡}t || ||||¡}t|dkd	tjd t
 ||¡d
d
… S )r1   r+   r,   r-   ú)Input ciphertext must be at least {} longr   r   é   r   r2   N)r$   r   r   r%   r
   r	   r   r   Úformatr#   r   r   Úmaxr   Úcrypto_box_open_easyr4   r   )r0   r(   r   r   rB   rA   r5   r6   r   r   r   rG     s.   


ÿûýrG   c                 C   s‚   t |ƒtkrt d¡‚t |ƒtkrt d¡‚t | ƒ}t| }t d|¡}t 	|| |||¡}t
|dkdtjd t ||¡dd… S )r:   r;   r<   r   r   r   r   N)r$   r   r   r%   r   r   r   r   r   Úcrypto_box_easy_afternmr   r   r   )r'   r(   r8   rA   rB   r0   r   r   r   r   rH   6  s   


rH   c                 C   s¢   t |ƒtkrt d¡‚t |ƒtkrt d¡‚t | ƒ}t|tkd t¡tjd |t }t	 
dtd|ƒ¡}t || |||¡}t|dkdtjd t	 ||¡d	d	… S )
r>   r;   r<   rC   r   r   rD   r   r2   N)r$   r   r   r%   r   r   r   rE   r#   r   r   rF   r   Úcrypto_box_open_easy_afternmr4   r   )r0   r(   r8   rB   rA   r5   r6   r   r   r   rI   Q  s.   

ÿû
ÿýrI   c                 C   s’   t t| tƒdtd t t|tƒdtd t|ƒtkrt d¡‚t| ƒ}t| }t	 
d|¡}t || ||¡}t |dkdtjd t	 ||¡dd… S )	a€  
    Encrypts and returns a message ``message`` using an ephemeral secret key
    and the public key ``pk``.
    The ephemeral public key, which is embedded in the sealed box, is also
    used, in combination with ``pk``, to derive the nonce needed for the
    underlying box construct.

    :param message: bytes
    :param pk: bytes
    :rtype: bytes

    .. versionadded:: 1.2
    zinput message must be bytesr   úpublic key must be bytesr,   r   r   r   N)r   r!   r"   r#   r$   r
   r   r%   r   r   r   r   Úcrypto_box_sealr   r   )r'   r   rA   rB   r0   r   r   r   r   rK   }  s    ýÿ
rK   c                 C   sÞ   t t| tƒdtd t t|tƒdtd t t|tƒdtd t|ƒtkr)t d¡‚t|ƒtkr4t d¡‚t| ƒ}t |t	kd 
t	¡tjd |t	 }t dtd	|ƒ¡}t || |||¡}t |d
kdtjd t ||¡dd… S )a”  
    Decrypts and returns an encrypted message ``ciphertext``, using the
    recipent's secret key ``sk`` and the sender's ephemeral public key
    embedded in the sealed box. The box construct nonce is derived from
    the recipient's public key ``pk`` and the sender's public key.

    :param ciphertext: bytes
    :param pk: bytes
    :param sk: bytes
    :rtype: bytes

    .. versionadded:: 1.2
    zinput ciphertext must be bytesr   rJ   zsecret key must be bytesr,   r-   rC   r   rD   r   r2   N)r   r!   r"   r#   r$   r
   r   r%   r	   r   rE   r   r   rF   r   Úcrypto_box_seal_openr4   r   )r0   r   r   rB   rA   r5   r6   r   r   r   rL   £  s@   ýÿÿ

ÿûýrL   N)-Útypingr   Únaclr   r   Únacl._sodiumr   r   Únacl.exceptionsr   Ú__all__Úcrypto_box_secretkeybytesr	   ÚintÚ__annotations__Úcrypto_box_publickeybytesr
   Úcrypto_box_seedbytesr   Úcrypto_box_noncebytesr   Úcrypto_box_zerobytesr   Úcrypto_box_boxzerobytesr   Úcrypto_box_beforenmbytesr   Úcrypto_box_sealbytesr   Úcrypto_box_macbytesr   r"   r   r&   r   r3   r7   r=   r?   r@   rG   rH   rI   rK   rL   r   r   r   r   Ú<module>   s’   "ÿÿÿÿ
þ#ÿÿÿ
þÿÿÿÿ
þ!ÿÿÿÿ
þ.ÿÿÿ
þ,&