B
    _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Cryptodome.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 )Salsa20CipherzSalsa20 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t	|t
t |t	|t
t || j }|rtdt| 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   X/var/www/html/afkarena_new/venv/lib/python3.7/site-packages/Cryptodome/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 t	|t	|t
t|}|rtd| |dkrt|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|ddW dd}~X Y 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:`Cryptodome.Cipher.Salsa20.Salsa20Cipher` object
    Nr   )r   r   )r   r   r   r   r   new   s    r+   r   )       )N)ZCryptodome.Util.py3compatr   ZCryptodome.Util._raw_apir   r   r   r   r   r   r	   r
   ZCryptodome.Randomr   r   r   r+   r   r   r   r   r   r   <module>   s   (	^
