o
    i7                  
   @   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ZraisingN)
r   newr
   r	   r   r   r   excRuntimeErrorbuffer)pkskrc r   O/home/app/Keep/.python/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   


r/   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   )r/   r'   r   r   r.   	plaintextresr   r   r   r2   t   s   


r2   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   r6      s   


r6   r7   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'   r7   r.   r/   r   r   r   r   r<      s   


r<   c                 C   r8   )	
    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   r1   r   N)r#   r   r   r$   r   r   r   r   r   crypto_box_open_afternmr   r3   r   r   )r/   r'   r7   r.   r4   r5   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_clenr/   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 )r0   r*   r+   r,   )Input ciphertext must be at least {} longr   r      r   r1   N)r#   r   r   r$   r
   r	   r   r   formatr"   r   r   maxr   crypto_box_open_easyr3   r   )r/   r'   r   r   rA   r@   r4   r5   r   r   r   rF     s.   


rF   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 )r9   r:   r;   r   r   r   r   N)r#   r   r   r$   r   r   r   r   r   crypto_box_easy_afternmr   r   r   )r&   r'   r7   r@   rA   r/   r   r   r   r   rG   6  s   


rG   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;   rB   r   r   rC   r   r1   N)r#   r   r   r$   r   r   r   rD   r"   r   r   rE   r   crypto_box_open_easy_afternmr3   r   )r/   r'   r7   rA   r@   r4   r5   r   r   r   rH   Q  s.   


rH   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   r@   rA   r/   r   r   r   r   rJ   }  s    
rJ   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   rI   zsecret key must be bytesr+   r,   rB   r   rC   r   r1   N)r   r    r!   r"   r#   r
   r   r$   r	   r   rD   r   r   rE   r   crypto_box_seal_openr3   r   )r/   r   r   rA   r@   r4   r5   r   r   r   rK     s@   

rK   N)-typingr   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   __all__Zcrypto_box_secretkeybytesr	   int__annotations__Zcrypto_box_publickeybytesr
   Zcrypto_box_seedbytesr   Zcrypto_box_noncebytesr   Zcrypto_box_zerobytesr   Zcrypto_box_boxzerobytesr   Zcrypto_box_beforenmbytesr   Zcrypto_box_sealbytesr   Zcrypto_box_macbytesr   r!   r   r%   r   r2   r6   r<   r>   r?   rF   rG   rH   rJ   rK   r   r   r   r   <module>   s   "
#

!
.
,&