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._BLAKE2sa int blake2s_init(void **state, const uint8_t *key, size_t key_size, size_t digest_size); int blake2s_destroy(void *state); int blake2s_update(void *state, const uint8_t *buf, size_t len); int blake2s_digest(const void *state, uint8_t digest[32]); int blake2s_copy(const void *src, void *dst); c@sLeZdZdZdZddZddZddZd d Zd d Z d dZ ddZ dS) BLAKE2s_HashaA BLAKE2s 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)rz1.3.6.1.4.1.1722.12.2.2.z$Error %d while instantiating BLAKE2s) digest_size_update_after_digest _digest_donestroidr_raw_blake2s_libZ blake2s_initZ address_ofr r len ValueErrorrgetZblake2s_destroy_stateupdate)selfdatakey digest_bytesupdate_after_digeststateresultr$:/tmp/pip-target-t616c12r/lib/python/Crypto/Hash/BLAKE2s.py__init__Ls$    zBLAKE2s_Hash.__init__cCsH|jr|jstdt|jt|tt |}|rDt d||S)zContinue hashing of a message by consuming the next chunk of data. Args: data (byte string/byte array/memoryview): The next chunk of the message being hashed. z8You can only call 'digest' or 'hexdigest' on this objectz#Error %d while hashing BLAKE2s data) rr TypeErrorrZblake2s_updaterrr r rr)rrr#r$r$r%rfs    zBLAKE2s_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 BLAKE2s digestTN) r rZblake2s_digestrrrrr r)rZbfrr#r$r$r%digestxs  zBLAKE2s_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*BLAKE2s_Hash.hexdigest..)jointupler()rr$r$r% hexdigestszBLAKE2s_Hash.hexdigestcCsDtd}td||d}td||d}||kr@tddS)agVerify that a given **binary** MAC (computed by another party) is valid. Args: mac_tag (byte string/byte array/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. r) digest_bitsrrzMAC check failedN)rnewr(r)rZmac_tagsecretZmac1Zmac2r$r$r%verifys zBLAKE2s_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)r4rr)rZ hex_mac_tagr$r$r% hexverifys zBLAKE2s_Hash.hexverifycKs$d|krd|kr|j|d<tf|S)zQReturn a new instance of a BLAKE2s hash object. See :func:`new`. r r1)rr2)rkwargsr$r$r%r2s zBLAKE2s_Hash.newN) __name__ __module__ __qualname____doc__ block_sizer&rr(r/r4r5r2r$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 (byte string/byte array/memoryview): Optional. The very first chunk of the message to hash. It is equivalent to an early call to :meth:`BLAKE2s_Hash.update`. digest_bytes (integer): Optional. The size of the digest, in bytes (1 to 32). Default is 32. digest_bits (integer): Optional and alternative to ``digest_bytes``. The size of the digest, in bits (8 to 256, in steps of 8). Default is 256. key (byte string): 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:`BLAKE2s_Hash` hash object rNr!Fr r1z*Only one digest parameter must be provided)NNrz!'digest_bytes' not in range 1..32z3'digest_bytes' not in range 8..256, with steps of 8rz"BLAKE2s key cannot exceed 32 byteszUnknown parameters: )popr'rrrr )r6rr!r r1rr$r$r%r2s(          r2N)binasciirZCrypto.Util.py3compatrrZ Crypto.RandomrZCrypto.Util._raw_apirrrr r r r robjectr r2r$r$r$r%s  $