U
    ֫[0                     @   s   d dl 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	 Ze	 Ze	 Ze	 Ze	 Zdd Zdd Zd	S )
    )absolute_importdivisionprint_function)
exceptions)ffilib)ensurec                 C   s   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`` with the secret ``key`` and
    the nonce ``nonce``.

    :param message: bytes
    :param nonce: bytes
    :param key: bytes
    :rtype: bytes
    Invalid keyInvalid nonce    unsigned char[]r   zEncryption failedZraisingN)lencrypto_secretbox_KEYBYTESexc
ValueErrorcrypto_secretbox_NONCEBYTEScrypto_secretbox_ZEROBYTESr   newr   crypto_secretboxr   CryptoErrorbuffercrypto_secretbox_BOXZEROBYTES)messagenoncekeypadded
ciphertextres r   @/usr/lib/python3/dist-packages/nacl/bindings/crypto_secretbox.pyr      s    


r   c                 C   s   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
    Decrypt and returns the encrypted message ``ciphertext`` with the secret
    ``key`` and the nonce ``nonce``.

    :param ciphertext: bytes
    :param nonce: bytes
    :param key: bytes
    :rtype: bytes
    r	   r
   r   r   r   z1Decryption failed. Ciphertext failed verificationr   N)r   r   r   r   r   r   r   r   r   crypto_secretbox_openr   r   r   r   )r   r   r   r   Z	plaintextr   r   r   r    r!   8   s$    


    
r!   N)Z
__future__r   r   r   Znaclr   r   Znacl._sodiumr   r   Znacl.exceptionsr   Zcrypto_secretbox_keybytesr   Zcrypto_secretbox_noncebytesr   Zcrypto_secretbox_zerobytesr   Zcrypto_secretbox_boxzerobytesr   Zcrypto_secretbox_macbytesZcrypto_secretbox_MACBYTESZ!crypto_secretbox_messagebytes_maxZ!crypto_secretbox_MESSAGEBYTES_MAXr   r!   r   r   r   r    <module>   s   