Пример #1
0
    def compute_hash(self):
        """
        Generates a hash of the transaction
        :return:
        """
        tx_string = json.dumps(self.__dict__, sort_keys=True)

        return blake2b(tx_string.encode()).hexdigest()
Пример #2
0
def make_identifier(public_bytes: bytes) -> str:
    """
    Generate a new decentralised identifier from public key as bytes
    :param public_bytes: public key as bytes
    :return: decentralised identifier
    """
    bl2 = blake2b(digest_size=20)
    bl2.update(public_bytes)
    pk_digest = bl2.digest()

    cl2 = blake2b(digest_size=20)
    cl2.update(pk_digest)
    checksum = bytearray.fromhex(cl2.hexdigest())[:4]

    return IDENTIFIER_PREFIX + base58.b58encode(
        bytes([IDENTIFIER_METHOD, IDENTIFIER_VERSION, IDENTIFIER_PAD]) +
        pk_digest + checksum).decode('ascii')
Пример #3
0
 def __init__(self, data, owner, group=None):
     """
     :param data:
     :param owner: the public key of the sender
     """
     self.data = data
     self.signer = blake2b(owner.encode()).hexdigest()
     self.timestamp = time.time()
     self.group = group
     self.object_id = time.time()  #Todo to be change to something suitable
     self.hash = self.compute_hash()
Пример #4
0
 def calculate_name_hash(cls, name):
     if isinstance(name, str):
         name = name.encode('ascii')
     # see:
     # https://github.com/aeternity/protocol/blob/master/AENS.md#hashing
     # and also:
     # https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#namehash-algorithm
     labels = name.split(b'.')
     hash_func = lambda data: blake2b(data, digest_size=32).digest()
     hashed = b'\x00' * 32
     while labels:
         hashed = hash_func(hashed + hash_func(labels[0]))
         labels = labels[1:]
     b58hash = base58.b58encode_check(hashed)
     return f'nm${b58hash}'
Пример #5
0
 def test_scrapper(self):
     h = blake2b()
     h.update(u"G3215532Q".encode('utf8'))
     hash_fin = h.hexdigest()
     self.assertEqual(hash_fin, h.hexdigest())
Пример #6
0
def test():
    for _ in range(9999):
        for alg in _hashlib.openssl_md_meth_names:
            _hashlib.hmac_digest(fstr(), fstr(), alg)

            try:
                _hashlib.pbkdf2_hmac(alg, fstr(), fstr(), fint())
                _hashlib.pbkdf2_hmac(alg, fstr(), fstr(), fint(), fint())
            except ValueError:
                pass

            try:
                _hashlib.scrypt(password=fstr(),
                                salt=fstr(),
                                n=fint(),
                                r=fint(),
                                p=fint())
            except (ValueError, TypeError, AttributeError):
                pass

            try:
                do_hash_tests(_hashlib.new(alg))
                do_hash_tests(_hashlib.new(alg, fstr()))
            except ValueError:
                pass

        hashes = [
            _hashlib.openssl_md5, _hashlib.openssl_sha1,
            _hashlib.openssl_sha224, _hashlib.openssl_sha256,
            _hashlib.openssl_sha384, _hashlib.openssl_sha512, _md5.md5,
            _sha1.sha1, _sha256.sha224, _sha256.sha256, _sha512.sha384,
            _sha512.sha512, _sha3.sha3_224, _sha3.sha3_384, _sha3.sha3_512,
            _sha3.shake_128, _sha3.shake_256
        ]

        for h in hashes:
            do_hash_tests(h())
            do_hash_tests(h(fstr()))

        try:
            do_hash_tests(
                _blake2.blake2b(fstr(),
                                digest_size=fint(),
                                key=fstr(),
                                salt=fstr(),
                                person=fstr(),
                                fanout=fint(),
                                depth=fint(),
                                leaf_size=fint(),
                                node_offset=fint(),
                                node_depth=fint(),
                                inner_size=fint(),
                                last_node=fint()))
            do_hash_tests(
                _blake2.blake2s(fstr(),
                                digest_size=fint(),
                                key=fstr(),
                                salt=fstr(),
                                person=fstr(),
                                fanout=fint(),
                                depth=fint(),
                                leaf_size=fint(),
                                node_offset=fint(),
                                node_depth=fint(),
                                inner_size=fint(),
                                last_node=fint()))
        except (ValueError, OverflowError):
            pass
Пример #7
0
def get_secret(api_key: str, nonce: str, secret_key: str):
    h = blake2b(digest_size=64)
    h.update(api_key.encode('utf-8'))
    h.update(nonce.encode('utf-8'))
    h.update(secret_key.encode('utf-8'))
    return h.hexdigest()