Пример #1
0
 def from_pubkey(cls, key: PublicKey) -> "ID":
     serialized_key = key.serialize()
     algo = multihash.Func.sha2_256
     if ENABLE_INLINING and len(serialized_key) <= MAX_INLINE_KEY_LENGTH:
         algo = IDENTITY_MULTIHASH_CODE
     mh_digest = multihash.digest(serialized_key, algo)
     return cls(mh_digest.encode())
Пример #2
0
def ipfs_hash(content):
    """ Returns the SHA2-256 multihash of an object. Non-bytes objects are
    converted to strings and then encoded using utf-8 before being hashed.
    """
    if not isinstance(content, bytes):
        content = str(content).encode()
    mh = multihash.digest(content, multihash.Func.sha2_256)
    return base58.b58encode(mh.encode()).decode()
Пример #3
0
def test_id_from_public_key():
    bits_list = [1024, 1280, 1536, 1536, 2048]
    key = RSA.generate(random.choice(bits_list))
    key_bin = key.exportKey("DER")
    algo = multihash.Func.sha2_256
    mh_digest = multihash.digest(key_bin, algo)
    expected = ID(mh_digest.encode())
    actual = id_from_public_key(key)

    assert actual == expected
Пример #4
0
    def create(cls,
               data,
               links=None,
               hash_algorithm='sha2-256',
               serializer=json.dumps):
        links = [l for l in links
                 if isinstance(l, Link)] if links is not None else []
        serialized = ensure_bytes(serializer({'data': data, 'links': links}))
        mh = multihash.digest(serialized, hash_algorithm).encode('base58')

        return Node(data, links, serialized, mh)
Пример #5
0
def id_from_public_key(key):
    # export into binary format
    key_bin = key.exportKey("DER")

    algo = multihash.Func.sha2_256
    # TODO: seems identity is not yet supported in pymultihash
    # if len(b) <= MAX_INLINE_KEY_LENGTH:
    #     algo multihash.func.identity

    mh_digest = multihash.digest(key_bin, algo)
    return ID(mh_digest.encode())
Пример #6
0
def test_id_from_public_key():
    key_pair = create_new_key_pair()
    public_key = key_pair.public_key

    key_bin = public_key.serialize()
    algo = multihash.Func.sha2_256
    mh_digest = multihash.digest(key_bin, algo)
    expected = ID(mh_digest.encode())

    actual = ID.from_pubkey(public_key)

    assert actual == expected
Пример #7
0
def multihash(data, fn_name='sha2_256'):
    """A utility function to make multihashing more convenient

    Args:
        data (bytes str): Any Python dict that is an output of the
            `marshal` function
        fn_name (str): Any of the following string values: 'sha1',
            'sha2_256', 'sha2_512', 'sha3_512', 'sha3', 'sha3_384',
            'sha3_256', 'sha3_224', 'shake_128', 'shake_256', 'blake2b',
            'blake2s'

    Returns:
        A base58 encoded digest of a hash (encoded in ascii)

    """
    return digest(data, fn_name).encode('base58').decode('ascii')
Пример #8
0
def multihash(data, fn_name='sha2_256'):
    """A utility function to make multihashing more convenient

    Args:
        data (bytes str): Any Python dict that is an output of the
            `marshal` function
        fn_name (str): Any of the following string values: 'sha1',
            'sha2_256', 'sha2_512', 'sha3_512', 'sha3', 'sha3_384',
            'sha3_256', 'sha3_224', 'shake_128', 'shake_256', 'blake2b',
            'blake2s'

    Returns:
        A base58 encoded digest of a hash (encoded in ascii)

    """
    return digest(data, fn_name).encode('base58').decode('ascii')
Пример #9
0
    def compute_peerid(public_key: str) -> str:
        """
        Compute the peer id from a public key.

        In particular, compute the base58 representation of
        libp2p PeerID from Bitcoin EC encoded Secp256k1 public key.

        :param public_key: the public key.
        :return: the peer id.
        """
        key_protobuf = PublicKey(
            key_type=KeyType.Secp256k1,
            data=_hex_to_bytes(public_key)  # type: ignore
        )
        key_serialized = key_protobuf.SerializeToString()
        algo = multihash.Func.sha2_256
        if ENABLE_INLINING and len(key_serialized) <= MAX_INLINE_KEY_LENGTH:
            algo = IDENTITY_MULTIHASH_CODE
        key_mh = multihash.digest(key_serialized, algo)
        return base58.b58encode(key_mh.encode()).decode()
Пример #10
0
async def test_dht_client_put_value(p2pds):
    peer_id_0, _ = await p2pds[0].control.identify()
    await connect_safe(p2pds[0], p2pds[1])

    # test case: valid key
    pk0 = await p2pds[0].dht.get_public_key(peer_id_0)
    # make the `key` from pk0
    algo = multihash.Func.sha2_256
    value = pk0.Data
    mh_digest = multihash.digest(value, algo)
    mh_digest_bytes = mh_digest.encode()
    key = b"/pk/" + mh_digest_bytes
    await p2pds[0].dht.put_value(key, value)
    # test case: get_value
    await p2pds[1].dht.get_value(key) == value

    # test case: invalid key
    key_invalid = b"/123/456"
    with pytest.raises(ControlFailure):
        await p2pds[0].dht.put_value(key_invalid, key_invalid)
Пример #11
0
def multihash_sha256(data_in_bytes):
    return multihash.digest(data_in_bytes,
                            "sha2_256").encode("hex").decode('utf-8')
Пример #12
0
def peer_id_from_pubkey(pubkey: datatypes.PublicKey) -> ID:
    algo = multihash.Func.sha2_256
    mh_digest = multihash.digest(pubkey.to_bytes(), algo)
    return ID(mh_digest.encode())
Пример #13
0
def get_multihash(data):
    return multihash.digest(data, 'sha2_256').digest.hex()
Пример #14
0
def test_hash():
    return multihash.digest(b'hello world', 'sha2-256').encode()
Пример #15
0
def _mk_multihash_sha256(data: bytes) -> bytes:
    return multihash.digest(data, "sha2-256")
Пример #16
0
def multihash(data):
    # NOTE: This is just a utility function to make the usage of this
    #       library more convenient.
    # NOTE: Not sure why all IPLD implementations use sha2-256...
    return digest(data, 'sha2_256').encode('base58').decode('utf-8')
Пример #17
0
def multihash(data):
    # NOTE: This is just a utility function to make the usage of this
    #       library more convenient.
    # NOTE: Not sure why all IPLD implementations use sha2-256...
    return digest(data, 'sha2_256').encode('base58').decode('utf-8')