U +Mb$@srddlmZddlmZmZddlmZddlmZm Z m Z m Z m Z m Z mZeddZGdddeZd d Zd S) ) unhexlify)bordtobytes)get_random_bytes)load_pycryptodome_raw_lib VoidPointer SmartPointercreate_string_bufferget_raw_bufferc_size_t c_uint8_ptrzCrypto.Hash._BLAKE2ba int blake2b_init(void **state, const uint8_t *key, size_t key_size, size_t digest_size); int blake2b_destroy(void *state); int blake2b_update(void *state, const uint8_t *buf, size_t len); int blake2b_digest(const void *state, uint8_t digest[64]); int blake2b_copy(const void *src, void *dst); c@sLeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ dS) BLAKE2b_HashaA BLAKE2b hash object. Do not instantiate directly. Use the :func:`new` function. :ivar oid: ASN.1 Object ID :vartype oid: string :ivar block_size: the size in bytes of the internal message block, input to the compression function :vartype block_size: integer :ivar digest_size: the size in bytes of the resulting hash :vartype digest_size: integer @cCs||_||_d|_|dkr,|s,dt||_t}t|t |t t |t |}|rft d|t |tj|_|r||dS)NF) 0rz1.3.6.1.4.1.1722.12.2.1.z$Error %d while instantiating BLAKE2b) digest_size_update_after_digest _digest_donestroidr_raw_blake2b_libZ blake2b_initZ address_ofr r len ValueErrorrgetZblake2b_destroy_stateupdate)selfdatakey digest_bytesupdate_after_digeststateresultr$:/tmp/pip-target-t616c12r/lib/python/Crypto/Hash/BLAKE2b.py__init__Ls$    zBLAKE2b_Hash.__init__cCsH|jr|jstdt|jt|tt |}|rDt d||S)zContinue hashing of a message by consuming the next chunk of data. Args: data (bytes/bytearray/memoryview): The next chunk of the message being hashed. z8You can only call 'digest' or 'hexdigest' on this objectz#Error %d while hashing BLAKE2b data) rr TypeErrorrZblake2b_updaterrr r rr)rrr#r$r$r%rfs    zBLAKE2b_Hash.updatecCsBtd}t|j|}|r*td|d|_t|d|jS)zReturn the **binary** (non-printable) digest of the message that has been hashed so far. :return: The hash digest, computed over the data processed so far. Binary form. :rtype: byte string rz&Error %d while creating BLAKE2b digestTN) r rZblake2b_digestrrrrr r)rZbfrr#r$r$r%digestxs  zBLAKE2b_Hash.digestcCsdddt|DS)zReturn the **printable** digest of the message that has been hashed so far. :return: The hash digest, computed over the data processed so far. Hexadecimal encoded. :rtype: string cSsg|]}dt|qS)z%02x)r).0xr$r$r% sz*BLAKE2b_Hash.hexdigest..)jointupler()rr$r$r% hexdigestszBLAKE2b_Hash.hexdigestcCsDtd}td||d}td||d}||kr@tddS)a`Verify that a given **binary** MAC (computed by another party) is valid. Args: mac_tag (bytes/bytearray/memoryview): the expected MAC of the message. Raises: ValueError: if the MAC does not match. It means that the message has been tampered with or that the MAC key is incorrect. ) digest_bitsrrzMAC check failedN)rnewr(r)rZmac_tagsecretZmac1Zmac2r$r$r%verifys zBLAKE2b_Hash.verifycCs|tt|dS)anVerify that a given **printable** MAC (computed by another party) is valid. Args: hex_mac_tag (string): the expected MAC of the message, as a hexadecimal string. Raises: ValueError: if the MAC does not match. It means that the message has been tampered with or that the MAC key is incorrect. N)r5rr)rZ hex_mac_tagr$r$r% hexverifys zBLAKE2b_Hash.hexverifycKs$d|krd|kr|j|d<tf|S)zQReturn a new instance of a BLAKE2b hash object. See :func:`new`. r r2)rr3)rkwargsr$r$r%r3s zBLAKE2b_Hash.newN) __name__ __module__ __qualname____doc__ block_sizer&rr(r/r5r6r3r$r$r$r%r :s r cKs|dd}|dd}|dd}|dd}d||fkrDtdd||fkrTd }|dk r|d |krpd ksntd n0d |krd krnn|d rtd|d }|dd}t|d krtd|rtdt|t||||S)aCreate a new hash object. Args: data (bytes/bytearray/memoryview): Optional. The very first chunk of the message to hash. It is equivalent to an early call to :meth:`BLAKE2b_Hash.update`. digest_bytes (integer): Optional. The size of the digest, in bytes (1 to 64). Default is 64. digest_bits (integer): Optional and alternative to ``digest_bytes``. The size of the digest, in bits (8 to 512, in steps of 8). Default is 512. key (bytes/bytearray/memoryview): Optional. The key to use to compute the MAC (1 to 64 bytes). If not specified, no key will be used. update_after_digest (boolean): Optional. By default, a hash object cannot be updated anymore after the digest is computed. When this flag is ``True``, such check is no longer enforced. Returns: A :class:`BLAKE2b_Hash` hash object rNr!Fr r2z*Only one digest parameter must be provided)NNrz!'digest_bytes' not in range 1..64iz3'digest_bytes' not in range 8..512, with steps of 8rz"BLAKE2s key cannot exceed 64 byteszUnknown parameters: )popr'rrrr )r7rr!r r2rr$r$r%r3s(          r3N)binasciirZCrypto.Util.py3compatrrZ Crypto.RandomrZCrypto.Util._raw_apirrrr r r r robjectr r3r$r$r$r%s  $