o
    ØÜg›'  ã                
   @   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< 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fd#d$„Z%dedededefd%d&„Z&d'S )(é    )Ú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Ú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   úR/var/www/html/api-tag/env/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) keypair 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%   4   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 )
zð
    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   V   s   


r.   c                 C   r(   )
a  
    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   )r.   r'   r   r   r-   Ú	plaintextÚresr   r   r   r0   s   s   


ýr0   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   r4   –   s   


r4   r5   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 )	zÇ
    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'   r5   r-   r.   r   r   r   r   r9   ®   s   


r9   c                 C   r6   )	zØ
    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
    r7   r8   r,   r   r   r/   r   N)r#   r   r   r$   r   r   r   r   r   Úcrypto_box_open_afternmr   r1   r   r   )r.   r'   r5   r-   r2   r3   r   r   r   r:   Ç   s   

ýr:   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   Ú_mlenÚ_clenr.   r   r   r   r   r<   æ   s    ýÿ
r<   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 contruct 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   r;   zsecret key must be bytesr*   r+   z)Input cyphertext must be at least {} longr   é   r   r/   N)r   r    r!   r"   r#   r
   r   r$   r	   r   Úformatr   r   Úmaxr   Úcrypto_box_seal_openr1   r   )r.   r   r   r>   r=   r2   r3   r   r   r   rB     s@   ýÿÿ

ÿûýrB   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   r!   r   r%   r   r0   r4   r9   r:   r<   rB   r   r   r   r   Ú<module>   sP   "ÿÿÿÿ
þ#ÿÿÿ
þ&