multiformats.multihash.raw
Implementation of raw hash functions used by multihash multicodecs.
Hash functions are implemented using the following modules:
hashlib, for the
sha
/shake
hash functions and theblake2
hash functions.blake3, for the
blake3
hash function.pyskein, for the
skein
hash functions.mmh3, for the
murmur3
hash functions.pycryptodomex, for the
ripemd-160
hash function, thekangarootwelve
hash function, thekeccak
hash functions and thesha2-512-224
/sha2-512-256
hash functions.
All modules other than hashlib are optional dependencies.
The get
function attempts to dynamically import any optional dependencies required by desired multihash
implementation, raising ImportError
if the dependency is not installed.
Core functionality is provided by the exists
and get
functions,
which can be used to check whether an implementatino with given name is known, and if so to get the corresponding pair
of hash function and max digest size:
>>> multihash.hashfun.exists("sha2-256")
True
>>> multihash.hashfun.get("sha2-256")
(<function _hashlib_sha.<locals>.hashfun at 0x0000013F4A3C6160>, 32)
The hash functions take a single bytes
input (the data) and return a bytes
output (the hash digest).
The max digest sizes (if not None
) are used to sense-check hash digests passed to wrap
and/or obtained from unwrap
: telling whether a digest has been generated by a hash function
is deemed to be computationally unfeasible in general,
but hash digests of length greater than the max digest size can always be discounted as invalid.
Hashfun
MultihashImpl
exists
get
- get(name)[source]
Given a multihash multicodec name, returns its implementation as a pair of a hash function and a max digest size (possibly
None
).>>> multihash.hashfun.get("sha2-256") (<function _hashlib_sha.<locals>.hashfun at 0x0000013F4A3C6160>, 32)
register
- register(name, hashfun, digest_size, *, overwrite=False)[source]
Registers a hash function and hash digest size implementing the multihash multicodec with given name, which must already exist.
Example usage (from the source code of this module):
register("sha1", _hashlib_sha(1), 20) # max digest size is 20 bytes, i.e. 160 bits register(f"sha2-256", _hashlib_sha(2, 256), 256//8)
- Parameters:
- Raises:
ValueError – if
overwrite
isFalse
and an implementation the same name already exists- Return type: