API Reference

multihash.encode(digest, code, length=None)[source]

Encode a hash digest along with the specified function code

Parameters:
  • digest (bytes) – hash digest
  • or str) code ((int) – hash function code
  • length (int) – hash digest length
Returns:

encoded multihash

Return type:

bytes

Raises:
  • TypeError – when the digest is not a bytes object
  • ValueError – when the digest length is not correct
multihash.decode(multihash)[source]

Decode a hash from the given multihash

Parameters:

multihash (bytes) – multihash

Returns:

decoded multihash.Multihash object

Return type:

multihash.Multihash

Raises:
  • TypeError – if multihash is not of type bytes
  • ValueError – if the length of multihash is less than 3 characters
  • ValueError – if the code is invalid
  • ValueError – if the length is invalid
  • ValueError – if the length is not same as the digest
multihash.is_valid(multihash)[source]

Check if the given buffer is a valid multihash

Parameters:multihash (bytes) – input multihash
Returns:if the input is a valid multihash or not
Return type:bool
multihash.is_valid_code(code)[source]

Checks whether a multihash code is valid or not

Parameters:code (int) – input code
Returns:if the code valid or not
Return type:bool
multihash.get_prefix(multihash)[source]

Return the prefix from the multihash

Parameters:multihash (bytes) – input multihash
Returns:multihash prefix
Return type:bytes
Raises:ValueError – when the multihash is invalid
multihash.coerce_code(hash_fn)[source]

Converts a hash function name into its code

If passed a number it will return the number if it’s a valid code

Parameters:

hash_fn – The input hash function can be - str, the name of the hash function - int, the code of the hash function

Returns:

hash function code

Return type:

int

Raises:
  • ValueError – if the hash function is not supported
  • ValueError – if the hash code is not supported
  • ValueError – if the hash type is not a string or an int
multihash.is_app_code(code)[source]

Checks whether a code is part of the app range

Parameters:code (int) – input code
Returns:if code is in the app range or not
Return type:bool
multihash.to_hex_string(multihash)[source]

Convert the given multihash to a hex encoded string

Parameters:hash (bytes) – the multihash to be converted to hex string
Returns:input multihash in str
Return type:str
Raises:TypeError, if the multihash has incorrect type
multihash.from_hex_string(multihash)[source]

Convert the given hex encoded string to a multihash

Parameters:multihash (str) – hex multihash encoded string
Returns:input multihash in bytes
Return type:bytes
Raises:TypeError, if the multihash has incorrect type
multihash.to_b58_string(multihash)[source]

Convert the given multihash to a base58 encoded string

Parameters:multihash (bytes) – multihash to base58 encode
Returns:base58 encoded multihash string
Return type:str
Raises:TypeError, if the multihash has incorrect type
multihash.from_b58_string(multihash)[source]

Convert the given base58 encoded string to a multihash

Parameters:multihash (str) – base58 encoded multihash string
Returns:decoded multihash
Return type:bytes
Raises:TypeError, if the multihash has incorrect type
class multihash.Multihash(code, name, length, digest)