U q`D@stddlmZmZmZddlmZddlmZddl m Z m Z ddlm Z ddZ dd Zd d Zd d ZddZdS))absolute_importdivisionprint_function) integer_typesN)ffilib)ensurecCstt|ttjdtt|ttjdtt|t|}td|}td|}t ||t|t ||t|t|t|k}t |||dk}|o|S)zA Compare contents of two memory regions in constant time Zraisingzchar []r) r isinstancebytesexc TypeErrormaxlenrnewmemmover sodium_memcmp)Zinp1Zinp2lnZbuf1buf2ZeqLZeqCr:/tmp/pip-target-nv4zd3e_/lib/python/nacl/bindings/utils.pyrs    rcCstt|ttjdtt|ttjd|dkr6tjt|}||}t d|}t dd}t |||t |||||}t|dkdtj dt||dddS)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 runsigned char [] size_t []zPadding failureN)rr r r r r ValueErrorrrrrr sodium_pad CryptoErrorbuffer)s blocksizes_lenZm_lenbufZp_lenrcrrrr.s    rcCsntt|ttjdtt|ttjdt|}tdd}t ||||}|dkr^t d|d|dS)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 rrrzUnpadding failureN) rr r r r rrrrr sodium_unpadr)rrr Zu_lenr"rrrr#Js    r#cCsVtt|ttjdt|}td|}t|||t ||t ||ddS)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 rN) rr r r r rrrrrsodium_incrementr)inprr!rrrr$as   r$cCstt|ttjdtt|ttjdt|}tt||ktjdtd|}td|}t|||t|||t |||t ||ddS)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 rN) rr r r r rrrrr sodium_addr)abrZbuf_aZbuf_brrrr&{s      r&) __future__rrrsixrZnacl.exceptions exceptionsr Z nacl._sodiumrrrrrr#r$r&rrrrs