o
    ‰¡e.  ã                   @   sn   d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
 dd„ Zdd„ Zdd	„ ZG d
d„ deƒZddd„ZdS )é    )Úbchr)ÚVoidPointerÚSmartPointerÚcreate_string_bufferÚget_raw_bufferÚc_size_tÚc_uint8_ptr)Ú_raw_keccak_libc                 C   s|   | dd> k r
| dksJ ‚| dkrdn|   ¡ d d }t|ƒt| d@ ƒ }| dL } | dkr<|t| d@ ƒ }| dL } | dks,|S )z2Left encode function as defined in NIST SP 800-185é   éø  r   é   é   éÿ   )Ú
bit_lengthr   )ÚxÚnumÚenc© r   ú;/usr/lib/python3/dist-packages/Cryptodome/Hash/cSHAKE128.pyÚ_left_encode)   s   þr   c                 C   s,   t | ƒd }|dd> krtdƒ‚t|ƒ|  S )z4Encode string function as defined in NIST SP 800-185r   r
   r   z$String too large to encode in cSHAKE)ÚlenÚ
ValueErrorr   )r   Úbitlenr   r   r   Ú_encode_str;   s   r   c                 C   s,   t |ƒ|  }|t|ƒ|  | }|d|  S )z2Zero pad byte string as defined in NIST SP 800-185ó    )r   r   )r   ÚlengthÚto_padÚnpadr   r   r   Ú_bytepadE   s   r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
cSHAKE_XOFz]A cSHAKE hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c           	      C   s    t ƒ }|rtdƒt|ƒ }t|d| d ƒ}d}nd }d}t | ¡ t|d ƒ|¡}|r2td| ƒ‚t| 	¡ tj
ƒ| _d| _|rE|  |¡ |rN|  |¡ d S d S )Nó    i@  r   é   é   z#Error %d while instantiating cSHAKEF)r   r   r   r	   Úkeccak_initÚ
address_ofr   r   r   ÚgetÚkeccak_destroyÚ_stateÚ_is_squeezingÚupdate)	ÚselfÚdataÚcustomÚcapacityÚstateÚprefix_unpadÚprefixÚpadÚresultr   r   r   Ú__init__W   s0   

þÿÿ
ÿzcSHAKE_XOF.__init__c                 C   sH   | j rtdƒ‚t | j ¡ t|ƒtt|ƒƒ¡}|r"t	d|| j
f ƒ‚| S )z¼Continue hashing of a message by consuming the next chunk of data.

        Args:
            data (byte string/byte array/memoryview): The next chunk of the message being hashed.
        z/You cannot call 'update' after the first 'read'z Error %d while updating %s state)r(   Ú	TypeErrorr	   Úkeccak_absorbr'   r%   r   r   r   r   Úname)r*   r+   r2   r   r   r   r)   r   s   
þÿzcSHAKE_XOF.updatec                 C   sD   d| _ t|ƒ}t | j ¡ |t|ƒ¡}|rtd|| jf ƒ‚t	|ƒS )ah  
        Compute the next piece of XOF output.

        .. note::
            You cannot use :meth:`update` anymore after the first call to
            :meth:`read`.

        Args:
            length (integer): the amount of bytes this method must return

        :return: the next piece of XOF output (of the given length)
        :rtype: byte string
        Tz!Error %d while extracting from %s)
r(   r   r	   Úkeccak_squeezer'   r%   r   r   r6   r   )r*   r   Úbfrr2   r   r   r   Úread„   s   þÿzcSHAKE_XOF.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r)   r9   r   r   r   r   r   Q   s
    r   Nr    c                 C   s   t | |dƒS )az  Return a fresh instance of a cSHAKE128 object.

    Args:
       data (bytes/bytearray/memoryview):
        Optional.
        The very first chunk of the message to hash.
        It is equivalent to an early call to :meth:`update`.
       custom (bytes):
        Optional.
        A customization bytestring (``S`` in SP 800-185).

    :Return: A :class:`cSHAKE_XOF` object
    é   )r   )r+   r,   r   r   r   ÚnewŸ   s   r?   )Nr    )ÚCryptodome.Util.py3compatr   ÚCryptodome.Util._raw_apir   r   r   r   r   r   ÚCryptodome.Hash.keccakr	   r   r   r   Úobjectr   r?   r   r   r   r   Ú<module>   s    
N