3
X-#                 @   s   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G dd	 d	ejee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	fddZ
dd Zdd Zdd	 Zd
d ZdS )	PublicKeya=  
    The public key counterpart to an Curve25519 :class:`nacl.public.PrivateKey`
    for encrypting messages.

    :param public_key: [:class:`bytes`] Encoded Curve25519 public key
    :param encoder: A class that is able to decode the `public_key`

    :cvar SIZE: The size that the public key is required to be
    c             C   sH   |j || _t| jts"tjdt| j| jkrDtjdj	| jd S )Nz'PublicKey must be created from 32 bytesz-The public key must be exactly {0} bytes long)
decode_public_key
isinstancebytesexc	TypeErrorlenSIZE
ValueErrorformat)self
public_keyencoder r   -/usr/lib/python3/dist-packages/nacl/public.py__init__$   s    
zPublicKey.__init__c             C   s   | j S )N)r   )r   r   r   r   	__bytes__/   s    zPublicKey.__bytes__c             C   s   t t| S )N)hashr   )r   r   r   r   __hash__2   s    zPublicKey.__hash__c             C   s&   t || jsdS tjjt| t|S )NF)r   	__class__naclbindingssodium_memcmpr   )r   otherr   r   r   __eq__5   s    zPublicKey.__eq__c             C   s
   | |k S )Nr   )r   r"   r   r   r   __ne__:   s    zPublicKey.__ne__N)__name__
__module____qualname____doc__r   r    Zcrypto_box_PUBLICKEYBYTESr   r   
RawEncoderr   r   r   r#   r$   r   r   r   r   r
      s   	r
   c               @   sR   e Zd ZdZejjZej	fddZ
dd Zdd Zdd	 Zd
d Zedd ZdS )
PrivateKeya  
    Private key for decrypting messages using the Curve25519 algorithm.

    .. warning:: This **must** be protected and remain secret. Anyone who
        knows the value of your :class:`~nacl.public.PrivateKey` can decrypt
        any message encrypted by the corresponding
        :class:`~nacl.public.PublicKey`

    :param private_key: The private key used to decrypt messages
    :param encoder: The encoder class used to decode the given keys

    :cvar SIZE: The size that the private key is required to be
    c             C   s\   |j |}t|tstjdt|| jkr<tjd| j tj	j
|}|| _t|| _d S )Nz.PrivateKey must be created from a 32 byte seedz,The secret key must be exactly %d bytes long)r   r   r   r   r   r   r   r   r   r    Zcrypto_scalarmult_base_private_keyr
   r   )r   private_keyr   Zraw_public_keyr   r   r   r   O   s    

zPrivateKey.__init__c             C   s   | j S )N)r+   )r   r   r   r   r   `   s    zPrivateKey.__bytes__c             C   s   t t| S )N)r   r   )r   r   r   r   r   c   s    zPrivateKey.__hash__c             C   s&   t || jsdS tjjt| t|S )NF)r   r   r   r    r!   r   )r   r"   r   r   r   r#   f   s    zPrivateKey.__eq__c             C   s
   | |k S )Nr   )r   r"   r   r   r   r$   k   s    zPrivateKey.__ne__c             C   s   | t tjtjdS )z~
        Generates a random :class:`~nacl.public.PrivateKey` object

        :rtype: :class:`~nacl.public.PrivateKey`
        )r   )r	   r*   r   r   r)   )clsr   r   r   generaten   s    zPrivateKey.generateN)r%   r&   r'   r(   r   r    Zcrypto_box_SECRETKEYBYTESr   r   r)   r   r   r   r#   r$   classmethodr.   r   r   r   r   r*   >   s   r*   c               @   sb   e Zd ZdZejjZdd Zdd Z	e
ejfddZdejfd	d
ZdejfddZdd ZdS )Boxa/  
    The Box class boxes and unboxes messages between a pair of keys

    The ciphertexts generated by :class:`~nacl.public.Box` 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 private_key: :class:`~nacl.public.PrivateKey` used to encrypt and
        decrypt messages
    :param public_key: :class:`~nacl.public.PublicKey` used to encrypt and
        decrypt messages

    :cvar NONCE_SIZE: The size that the nonce is required to be.
    c             C   sZ   |rP|rPt |t s t |t r*tjdtjj|jt	j
d|jt	j
d| _nd | _d S )Nz5Box must be created from a PrivateKey and a PublicKey)r   )r   r*   r
   r   r   r   r    Zcrypto_box_beforenmencoder   r)   _shared_key)r   r,   r   r   r   r   r      s    
zBox.__init__c             C   s   | j S )N)r2   )r   r   r   r   r      s    zBox.__bytes__c             C   s   | d d }|j ||_|S )N)r   r2   )r-   Zencodedr   Zboxr   r   r   r      s    
z
Box.decodeNc             C   sn   |dkrt | j}t|| jkr0tjd| j tjj||| j}|j	|}|j	|}t
j|||j	|| S )a  
        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.

        :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`]
        Nz'The nonce must be exactly %s bytes long)r	   
NONCE_SIZEr   r   r   r   r    Zcrypto_box_afternmr2   r1   r   Z_from_parts)r   	plaintextnoncer   
ciphertextZencoded_nonceZencoded_ciphertextr   r   r   encrypt   s    



zBox.encryptc             C   sb   |j |}|dkr.|d| j }|| jd }t|| jkrLtjd| j tj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`]
        Nz'The nonce must be exactly %s bytes long)	r   r3   r   r   r   r   r    Zcrypto_box_open_afternmr2   )r   r6   r5   r   r4   r   r   r   decrypt   s    

zBox.decryptc             C   s   | j S )a  
        Returns the Curve25519 shared secret, that can then be used as a key in
        other symmetric ciphers.

        .. warning:: It is **VITALLY** important that you use a nonce with your
            symmetric cipher. If you fail to do this, you compromise the
            privacy of the messages encrypted. Ensure that the key length of
            your cipher is 32 bytes.
        :rtype: [:class:`bytes`]
        )r2   )r   r   r   r   
shared_key   s    zBox.shared_key)r%   r&   r'   r(   r   r    Zcrypto_box_NONCEBYTESr3   r   r   r/   r   r)   r   r7   r8   r9   r   r   r   r   r0   x   s   	% r0   )Z
__future__r   r   r   Znacl.bindingsr   r   r   r   Z
nacl.utilsr   r   r	   Z	Encodableobjectr
   r*   r0   r   r   r   r   <module>   s   ':