U
    g
                     @   sZ   d dl mZ d dlmZ ddlmZ dd ZdZdZdZ	d	Z
G d
d deZdddZdS )    )long_to_bytes)bchr   )TurboSHAKE128c                 C   s$   | dkrdS t | }|tt| S )Nr       )r   r   len)xS r
   B/tmp/pip-unpacked-wheel-_q8s9isk/Cryptodome/Hash/KangarooTwelve.py_length_encode$   s    r            c                   @   s2   e Zd ZdZdd Zdd Zdd Zdd
dZdS )K12_XOFzeA KangarooTwelve hash object.
    Do not instantiate directly.
    Use the :func:`new` function.
    c                 C   sb   |d krd}|t t| | _t| _d | _tjdd| _d| _	d | _
d| _d| _|r^| | d S )N    r   domainr   )r   r   _custom	SHORT_MSG_state_paddingr   new_hash1_length1_hash2_length2_ctrupdateselfdatacustomr
   r
   r   __init__9   s    zK12_XOF.__init__c           
      C   s  | j tkrtd| j tkrX| jt| }|t| j dkrR|| _| j| | S t	| _ | j t	krt
|}| jdk szttt|d| j }| j|d|  |  j|7  _| jdk r| S | jdkstd}| j| |  jd7  _tjdd| _d| _d	| _t| _ | ||d S | j tks,td}t|}t
|}||k rt|d | j |}| j|||  |  j|| 7  _|}| jdkr@| jd
}	| j|	 |  jd
7  _| j  d| _|  jd	7  _q@| S )a
  Hash the next piece of data.

        .. note::
            For better performance, submit chunks with a length multiple of 8192 bytes.

        Args:
            data (byte string/byte array/memoryview): The next chunk of the
              message to hash.
        z/You cannot call 'update' after the first 'read'i    Ns                r   r   r       )r   	SQUEEZING	TypeErrorr   r   r   r   r   r   LONG_MSG_S0
memoryviewAssertionErrorminr   r   r   r   r   LONG_MSG_SXread_reset)
r    r!   Znext_lengthZdata_memZdtcdividerindexZlen_dataZ	new_indexcv_ir
   r
   r   r   Q   sV    




zK12_XOF.updatec                 C   s   d}| j tkr(| j| j d| _t| _ | j tkrP| | j d}| j tksPt	| j tkr|sj| | j | j
dkr| jd}| j| |  jd7  _| j  d| _
|  jd7  _t| jd d }| j| d| _t| _ | j| j_| j|S )	ad  
        Produce more bytes of the digest.

        .. 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
        F   Tr   r&   r   s      )r   r   r   r   r   r   r'   r)   r-   r+   r   r   r.   r   r/   r   r   Z_domain)r    lengthZcustom_was_consumedr2   trailerr
   r
   r   r.      s2    





zK12_XOF.readNr   c                 C   s   t | ||S )N)typer   r
   r
   r   r      s    zK12_XOF.new)Nr   )__name__
__module____qualname____doc__r#   r   r.   r   r
   r
   r
   r   r   3   s
   I1r   Nc                 C   s
   t | |S )ag  Return a fresh instance of a KangarooTwelve 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 byte string.

    :Return: A :class:`K12_XOF` object
    )r   )r!   r"   r
   r
   r   r      s    r   )NN)ZCryptodome.Util.numberr   ZCryptodome.Util.py3compatr    r   r   r   r)   r-   r'   objectr   r   r
   r
   r
   r   <module>   s   	 