U
    ֫[D                     @   st   d dl mZmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ dd Zdd	 Zd
d Zdd Zdd ZdS )    )absolute_importdivisionprint_function)integer_typesN)ffilib)ensurec                 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)Zinp1Zinp2lnZbuf1Zbuf2ZeqLZeqC r   5/usr/lib/python3/dist-packages/nacl/bindings/utils.pyr      s    

r   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   r   
ValueErrorr   r   r   r   r   
sodium_padCryptoErrorbuffer)s	blocksize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"   J   s    


r"   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   )Zinpr   r    r   r   r   r#   a   s    
r#   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   )abr   Zbuf_aZbuf_br   r   r   r$   {   s     

r$   )Z
__future__r   r   r   Zsixr   Znacl.exceptions
exceptionsr   Znacl._sodiumr   r   r   r   r   r"   r#   r$   r   r   r   r   <module>   s   