a b@sddlmZddlmZmZddlmZeeedddZ ee eddd Z ee edd d Z eed d dZ eeedddZdS)N)ffilib)ensure)inp1inp2returncCstt|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)rrlnZbuf1Zbuf2ZeqLZeqCr:/tmp/pip-target-98j97qn4/lib/python/nacl/bindings/utils.pyrs  r)s blocksizercCstt|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 rrunsigned char [] size_t []zPadding failureN)rr r r r int ValueErrorrrrrr sodium_pad CryptoErrorbuffer)rrs_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 rrrrzUnpadding failureN) rr r r r rrrrr sodium_unpadr)rrrZu_lenr!rrrr"Cs   r")inprcCsVtt|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 rrN) rr r r r rrrrrsodium_incrementr)r#rr rrrr$Xs   r$)abrcCstt|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 rrN) rr r r r rrrrr sodium_addr)r%r&rZbuf_aZbuf_brrrr'qs  r')Znacl.exceptions exceptionsr Z nacl._sodiumrrrr boolrrrr"r$r'rrrrs