3
¬ �eÍ  ã               @   sn   d dl mZ d dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ eddƒZG dd„ dƒZdd	d
„ZdZdZdS )é    )Ú_copy_bytes)Úload_pycryptodome_raw_libÚcreate_string_bufferÚget_raw_bufferÚVoidPointerÚSmartPointerÚc_size_tÚc_uint8_ptrÚis_writeable_buffer)Úget_random_byteszCrypto.Cipher._Salsa20aþ  
                    int Salsa20_stream_init(uint8_t *key, size_t keylen,
                                            uint8_t *nonce, size_t nonce_len,
                                            void **pSalsaState);
                    int Salsa20_stream_destroy(void *salsaState);
                    int Salsa20_stream_encrypt(void *salsaState,
                                               const uint8_t in[],
                                               uint8_t out[], size_t len);
                    c               @   s,   e Zd ZdZdd„ Zd	dd„Zd
dd„ZdS )ÚSalsa20CipherzŸSalsa20 cipher object. Do not create it directly. Use :py:func:`new`
    instead.

    :var nonce: The nonce with length 8
    :vartype nonce: byte string
    c             C   s²   t |ƒtkrtdt |ƒ ƒ‚t |ƒdkr8tdt |ƒ ƒ‚tdd|ƒ| _tƒ | _tjt	|ƒt
t |ƒƒt	|ƒt
t |ƒƒ| jjƒ ƒ}|rŠtdƒ‚t| jjƒ tjƒ| _d| _t |ƒ| _dS )zQInitialize a Salsa20 cipher object

        See also `new()` at the module level.z+Incorrect key length for Salsa20 (%d bytes)é   z-Incorrect nonce length for Salsa20 (%d bytes)Nz'Error %d instantiating a Salsa20 cipheré   )ÚlenÚkey_sizeÚ
ValueErrorr   Únoncer   Ú_stateÚ_raw_salsa20_libZSalsa20_stream_initr	   r   Z
address_ofr   ÚgetZSalsa20_stream_destroyÚ
block_size)ÚselfÚkeyr   Úresult© r   ú\/var/www/html/wholesaler-api-python-env/lib/python3.6/site-packages/Crypto/Cipher/Salsa20.pyÚ__init__6   s&    



zSalsa20Cipher.__init__Nc             C   s˜   |dkrt t|ƒƒ}n4|}t|ƒs*tdƒ‚t|ƒt|ƒkrJtdt|ƒ ƒ‚tj| jjƒ t	|ƒt	|ƒt
t|ƒƒƒ}|r€td| ƒ‚|dkr�t|ƒS dS dS )a«  Encrypt a piece of data.

        Args:
          plaintext(bytes/bytearray/memoryview): The data to encrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the ciphertext
            is written to. If ``None``, the ciphertext is returned.
        Returns:
          If ``output`` is ``None``, the ciphertext is returned as ``bytes``.
          Otherwise, ``None``.
        Nz4output must be a bytearray or a writeable memoryviewz9output must have the same length as the input  (%d bytes)z&Error %d while encrypting with Salsa20)r   r   r
   Ú	TypeErrorr   r   ZSalsa20_stream_encryptr   r   r	   r   r   )r   Z	plaintextÚoutputÚ
ciphertextr   r   r   r   ÚencryptS   s$    zSalsa20Cipher.encryptc             C   sJ   y| j ||d�S  tk
rD } ztt|ƒjddƒƒ‚W Y dd}~X nX dS )a±  Decrypt a piece of data.
        
        Args:
          ciphertext(bytes/bytearray/memoryview): The data to decrypt, of any size.
        Keyword Args:
          output(bytes/bytearray/memoryview): The location where the plaintext
            is written to. If ``None``, the plaintext is returned.
        Returns:
          If ``output`` is ``None``, the plaintext is returned as ``bytes``.
          Otherwise, ``None``.
        )r   ÚencÚdecN)r    r   ÚstrÚreplace)r   r   r   Úer   r   r   Údecrypty   s    zSalsa20Cipher.decrypt)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r&   r   r   r   r   r   .   s   
&r   Nc             C   s   |dkrt dƒ}t| |ƒS )a%  Create a new Salsa20 cipher

    :keyword key: The secret key to use. It must be 16 or 32 bytes long.
    :type key: bytes/bytearray/memoryview

    :keyword nonce:
        A value that must never be reused for any other encryption
        done with this key. It must be 8 bytes long.

        If not provided, a random byte string will be generated (you can read
        it back via the ``nonce`` attribute of the returned object).
    :type nonce: bytes/bytearray/memoryview

    :Return: a :class:`Crypto.Cipher.Salsa20.Salsa20Cipher` object
    Nr   )r   r   )r   r   r   r   r   ÚnewŒ   s    r+   r   é   é    )N)r,   r-   )ZCrypto.Util.py3compatr   ZCrypto.Util._raw_apir   r   r   r   r   r   r	   r
   ZCrypto.Randomr   r   r   r+   r   r   r   r   r   r   Ú<module>   s   (	^
