U
    g                     @   sd   d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ ddlmZ G dd deZdd	 Zd
S )    )VoidPointerSmartPointercreate_string_bufferget_raw_bufferc_size_tc_uint8_ptrc_ubyte)long_to_bytes)bchr   )_raw_keccak_libc                   @   s:   e Zd ZdZdd Zdd Zdd Zdd	d
Zdd ZdS )
TurboSHAKEzaA TurboSHAKE hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c                 C   sf   t  }t| t|td}|r0td| t| tj	| _
d| _|| _|| _|rb| | d S )N   z'Error %d while instantiating TurboSHAKEF)r   r   Zkeccak_initZ
address_ofr   r   
ValueErrorr   getZkeccak_destroy_state_is_squeezing	_capacity_domainupdate)selfcapacitydomain_separationdatastateresult r   A/tmp/pip-unpacked-wheel-_q8s9isk/Cryptodome/Hash/TurboSHAKE128.py__init__   s    
zTurboSHAKE.__init__c                 C   sB   | j rtdt| j t|tt|}|r>t	d| | S )zContinue 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 TurboSHAKE state)
r   	TypeErrorr   Zkeccak_absorbr   r   r   r   lenr   )r   r   r   r   r   r   r   %   s    
zTurboSHAKE.updatec                 C   sF   d| _ t|}t| j |t|t| j}|r>t	d| 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 TurboSHAKE)r   r   r   Zkeccak_squeezer   r   r   r   r   r   r   )r   lengthZbfrr   r   r   r   read7   s    zTurboSHAKE.readNc                 C   s   t | | j| j|S )N)typer   r   )r   r   r   r   r   newR   s    zTurboSHAKE.newc                 C   s*   t | j }|r td| d| _d S )Nz)Error %d while resetting TurboSHAKE stateF)r   Zkeccak_resetr   r   r   r   )r   r   r   r   r   _resetU   s    zTurboSHAKE._reset)N)	__name__
__module____qualname____doc__r   r   r"   r$   r%   r   r   r   r   r      s   
r   c                  K   sF   |  dd}d|  kr dks.n td| |  d}td||dS )	a  Create a new TurboSHAKE128 object.

    Args:
       domain (integer):
         Optional - A domain separation byte, between 0x01 and 0x7F.
         The default value is 0x1F.
       data (bytes/bytearray/memoryview):
        Optional - The very first chunk of the message to hash.
        It is equivalent to an early call to :meth:`update`.

    :Return: A :class:`TurboSHAKE` object
    domain   r      z&Incorrect domain separation value (%d)r       )r   )r   r   r   )kwargsr   r   r   r   r   r$   ]   s    
r$   N)ZCryptodome.Util._raw_apir   r   r   r   r   r   r   ZCryptodome.Util.numberr	   ZCryptodome.Util.py3compatr
   Zkeccakr   objectr   r$   r   r   r   r   <module>   s
   $Q