U
    ÎZjÊ  ã                   @   s„   d dl mZ d dlmZmZ d dl mZ eeedœdd„Z	ee
edœdd	„Zee
edœd
d„Zeedœdd„Zeeedœdd„ZdS )é    N)ÚffiÚlib)Úensure)Úinp1Úinp2Úreturnc                 C   s    t t| tƒtjd� t t|tƒtjd� tt| ƒt|ƒƒ}t d|¡}t d|¡}t 	|| t| ƒ¡ t 	||t|ƒ¡ t| ƒt|ƒk}t
 |||¡dk}|ož|S )zA
    Compare contents of two memory regions in constant time
    ©Zraisingzchar []r   )r   Ú
isinstanceÚbytesÚexcÚ	TypeErrorÚmaxÚlenr   ÚnewÚmemmover   Úsodium_memcmp)r   r   ÚlnZbuf1Zbuf2ZeqLZeqC© r   úV/var/www/html/TRUCKING_PROJECT/venv/lib/python3.8/site-packages/nacl/bindings/utils.pyr      s    r   )ÚsÚ	blocksizer   c                 C   sª   t t| tƒtjd� t t|tƒtjd� |dkr6tj‚t| ƒ}|| }t 	d|¡}t 	dd¡}t 
|| |¡ t |||||¡}t |dkdtjd� t ||d ¡dd… S )zþ
    Pad the input bytearray ``s`` to a multiple of ``blocksize``
    using the ISO/IEC 7816-4 algorithm

    :param s: input bytes string
    :type s: bytes
    :param blocksize:
    :type blocksize: int
    :return: padded string
    :rtype: bytes
    r   r   úunsigned char []ú	size_t []é   zPadding failureN)r   r	   r
   r   r   ÚintÚ
ValueErrorr   r   r   r   r   Ú
sodium_padÚCryptoErrorÚbuffer)r   r   Ús_lenZm_lenÚbufZp_lenÚrcr   r   r   r   )   s    r   c                 C   sn   t t| tƒtjd� t t|tƒtjd� t| ƒ}t dd¡}t	 
|| ||¡}|dkr^t d¡‚| d|d … S )zÚ
    Remove ISO/IEC 7816-4 padding from the input byte array ``s``

    :param s: input bytes string
    :type s: bytes
    :param blocksize:
    :type blocksize: int
    :return: unpadded string
    :rtype: bytes
    r   r   r   r   zUnpadding failureN)r   r	   r
   r   r   r   r   r   r   r   Úsodium_unpadr   )r   r   r   Zu_lenr!   r   r   r   r"   C   s    
r"   )Úinpr   c                 C   sV   t t| tƒtjd� t| ƒ}t d|¡}t || |¡ t	 
||¡ t ||¡dd… S )ag  
    Increment the value of a byte-sequence interpreted
    as the little-endian representation of a unsigned big integer.

    :param inp: input bytes buffer
    :type inp: bytes
    :return: a byte-sequence representing, as a little-endian
             unsigned big integer, the value ``to_int(inp)``
             incremented by one.
    :rtype: bytes

    r   r   N)r   r	   r
   r   r   r   r   r   r   r   Úsodium_incrementr   )r#   r   r    r   r   r   r$   X   s    r$   )ÚaÚbr   c                 C   sœ   t t| tƒtjd� t t|tƒtjd� t| ƒ}t t|ƒ|ktjd� t d|¡}t d|¡}t || |¡ t |||¡ t	 
|||¡ t ||¡dd… S )a  
    Given a couple of *same-sized* byte sequences, interpreted as the
    little-endian representation of two unsigned integers, compute
    the modular addition of the represented values, in constant time for
    a given common length of the byte sequences.

    :param a: input bytes buffer
    :type a: bytes
    :param b: input bytes buffer
    :type b: bytes
    :return: a byte-sequence representing, as a little-endian big integer,
             the integer value of ``(to_int(a) + to_int(b)) mod 2^(8*len(a))``
    :rtype: bytes
    r   r   N)r   r	   r
   r   r   r   r   r   r   r   Ú
sodium_addr   )r%   r&   r   Zbuf_aZbuf_br   r   r   r'   q   s    r'   )Znacl.exceptionsÚ
exceptionsr   Znacl._sodiumr   r   r   r
   Úboolr   r   r   r"   r$   r'   r   r   r   r   Ú<module>   s   