U
    ­Ö«[6  ã                   @   sb   d dl mZmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
mZmZ G dd„ dejeeƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionN)Úencoding)Ú
exceptions)ÚEncryptedMessageÚStringFixerÚrandomc                   @   sf   e Zd ZdZejjZejjZ	ejj
ZejjZejfdd„Zdd„ Zdejfdd„Zdejfd	d
„ZdS )Ú	SecretBoxa5  
    The SecretBox class encrypts and decrypts messages using the given secret
    key.

    The ciphertexts generated by :class:`~nacl.secret.Secretbox` include a 16
    byte authenticator which is checked as part of the decryption. An invalid
    authenticator will cause the decrypt function to raise an exception. The
    authenticator is not a signature. Once you've decrypted the message you've
    demonstrated the ability to create arbitrary valid message, so messages you
    send are repudiable. For non-repudiable messages, sign them after
    encryption.

    :param key: The secret key used to encrypt and decrypt messages
    :param encoder: The encoder class used to decode the given key

    :cvar KEY_SIZE: The size that the key is required to be.
    :cvar NONCE_SIZE: The size that the nonce is required to be.
    :cvar MACBYTES: The size of the authentication MAC tag in bytes.
    :cvar MESSAGEBYTES_MAX: The maximum size of a message which can be
                            safely encrypted with a single key/nonce
                            pair.
    c                 C   sF   |  |¡}t|tƒst d¡‚t|ƒ| jkr<t d| j ¡‚|| _d S )Nz'SecretBox must be created from 32 bytesz%The key must be exactly %s bytes long)	ÚdecodeÚ
isinstanceÚbytesÚexcÚ	TypeErrorÚlenÚKEY_SIZEÚ
ValueErrorÚ_key)ÚselfÚkeyÚencoder© r   ú-/usr/lib/python3/dist-packages/nacl/secret.pyÚ__init__4   s    


ÿÿzSecretBox.__init__c                 C   s   | j S )N)r   )r   r   r   r   Ú	__bytes__A   s    zSecretBox.__bytes__Nc                 C   sn   |dkrt | jƒ}t|ƒ| jkr0t d| j ¡‚tj ||| j¡}| 	|¡}| 	|¡}t
 ||| 	|| ¡¡S )aL  
        Encrypts the plaintext message using the given `nonce` (or generates
        one randomly if omitted) and returns the ciphertext encoded with the
        encoder.

        .. warning:: It is **VITALLY** important that the nonce is a nonce,
            i.e. it is a number used only once for any given key. If you fail
            to do this, you compromise the privacy of the messages encrypted.
            Give your nonces a different prefix, or have one side use an odd
            counter and one an even counter. Just make sure they are different.

        :param plaintext: [:class:`bytes`] The plaintext message to encrypt
        :param nonce: [:class:`bytes`] The nonce to use in the encryption
        :param encoder: The encoder to use to encode the ciphertext
        :rtype: [:class:`nacl.utils.EncryptedMessage`]
        Nú'The nonce must be exactly %s bytes long)r	   Ú
NONCE_SIZEr   r   r   ÚnaclÚbindingsZcrypto_secretboxr   Úencoder   Z_from_parts)r   Ú	plaintextÚnoncer   Ú
ciphertextZencoded_nonceZencoded_ciphertextr   r   r   ÚencryptD   s"    
ÿ ÿ

ýzSecretBox.encryptc                 C   sb   |  |¡}|dkr.|d| j… }|| jd… }t|ƒ| jkrLt d| j ¡‚tj ||| j¡}|S )aá  
        Decrypts the ciphertext using the `nonce` (explicitly, when passed as a
        parameter or implicitly, when omitted, as part of the ciphertext) and
        returns the plaintext message.

        :param ciphertext: [:class:`bytes`] The encrypted message to decrypt
        :param nonce: [:class:`bytes`] The nonce used when encrypting the
            ciphertext
        :param encoder: The encoder used to decode the ciphertext.
        :rtype: [:class:`bytes`]
        Nr   )	r   r   r   r   r   r   r   Zcrypto_secretbox_openr   )r   r"   r!   r   r    r   r   r   Údecrypti   s    
ÿ ÿzSecretBox.decrypt)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Zcrypto_secretbox_KEYBYTESr   Zcrypto_secretbox_NONCEBYTESr   Zcrypto_secretbox_MACBYTESZMACBYTESZ!crypto_secretbox_MESSAGEBYTES_MAXZMESSAGEBYTES_MAXr   Z
RawEncoderr   r   r#   r$   r   r   r   r   r
      s   %r
   )Z
__future__r   r   r   Znacl.bindingsr   r   r   r   Z
nacl.utilsr   r   r	   Z	EncodableÚobjectr
   r   r   r   r   Ú<module>   s
   