o
    Þý°já  ã                   @   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 )
Ú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vrtdt |ƒ ƒ‚t |ƒdkrtdt |ƒ ƒ‚tdd|ƒ| _tƒ | _t t	|ƒt
t |ƒƒt	|ƒt
t |ƒƒ| j ¡ ¡}|rEt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_libÚSalsa20_stream_initr	   r   Ú
address_ofr   ÚgetÚSalsa20_stream_destroyÚ
block_size)ÚselfÚkeyr   Úresult© r   úŒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/Cryptodome/Cipher/Salsa20.pyÚ__init__6   s,   ÿ

û
ÿzSalsa20Cipher.__init__Nc                 C   s”   |du rt t|ƒƒ}n|}t|ƒstdƒ‚t|ƒt|ƒkr%tdt|ƒ ƒ‚t | j ¡ t	|ƒt	|ƒt
t|ƒƒ¡}|r@td| ƒ‚|du rHt|ƒ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   ÚSalsa20_stream_encryptr   r   r	   r   r   )r   Ú	plaintextÚoutputÚ
ciphertextr   r   r   r   ÚencryptS   s(   ÿ
üzSalsa20Cipher.encryptc              
   C   s>   z| j ||d�W S  ty } z
tt|ƒ dd¡ƒ‚d}~ww )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)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r+   r   r   r   r   r   .   s
    
&r   Nc                 C   s   |du 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   
r1   r   )é   é    r,   )ÚCryptodome.Util.py3compatr   ÚCryptodome.Util._raw_apir   r   r   r   r   r   r	   r
   ÚCryptodome.Randomr   r   r   r1   r   r   r   r   r   r   Ú<module>   s   (ÿ
^