3 L]D@srddlmZmZmZddlmZddljZddl m Z m Z ddlm Z ddZ dd Zd d Zd d ZddZdS))absolute_importdivisionprint_function) integer_typesN)ffilib)ensurecCstt|ttjdtt|ttjdtt|t|}tjd|}tjd|}tj ||t|tj ||t|t|t|k}t j |||dk}|o|S)zA Compare contents of two memory regions in constant time )raisingzchar []r) r isinstancebytesexc TypeErrormaxlenrnewmemmover sodium_memcmp)Zinp1Zinp2lnZbuf1buf2ZeqLZeqCr7/tmp/pip-install-wfra5znf/pynacl/nacl/bindings/utils.pyrs      rcCstt|ttjdtt|ttjd|dkr6tjt|}||}tj d|}tj dd}tj |||t j |||||}t|dkdtj dtj||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 rzunsigned char []z size_t []zPadding failureN)rr r r r r ValueErrorrrrrr sodium_pad CryptoErrorbuffer)s blocksizes_lenZm_lenbufZp_lenrcrrrr.s      rcCsntt|ttjdtt|ttjdt|}tjdd}t j ||||}|dkr^tj 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 z size_t []rrzUnpadding failureN) rr r r r rrrrr sodium_unpadr)rrrZu_lenr rrrr!Js      r!cCsVtt|ttjdt|}tjd|}tj|||t j ||tj ||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 zunsigned char []N) rr r r r rrrrrsodium_incrementr)inprrrrrr"as    r"cCstt|ttjdtt|ttjdt|}tt||ktjdtjd|}tjd|}tj|||tj|||t j |||tj ||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 zunsigned char []N) rr r r r rrrrr sodium_addr)abrZbuf_aZbuf_brrrr${s        r$) __future__rrrsixrZnacl.exceptions exceptionsr Z nacl._sodiumrrrrrr!r"r$rrrrs