logo
pub trait Hasher: Default + Send + Sync {
    type Size: Size;
    type Digest: Digest<Self::Size>;
    fn digest(input: &[u8]) -> Self::Digest;

    fn size() -> u8 { ... }
}
Expand description

Trait implemented by a hash function implementation.

It specifies its own Digest type, so that the output of the hash function can later be distinguished. This way you can create a MultihashDigest from a Digest.

Every hashing algorithm that is used with Multihash needs to implement those. This trait is very similar to the external digest::Digest trait. There is a small significant difference, which needed the introduction of this Hasher trait instead of re-using the widely used digest::Digest trait.

The external digest::Digest trait has a single return type called Output, which is used for all hashers that implement it. It’s basically a wrapper around the hashed result bytes. For Multihashes we need to distinguish those bytes, as we care about which hash function they were created with (which is the whole point of Multihashes). Therefore the Hasher trait defines an associated type Hasher::Digest for the output of the hasher. This way the implementers can specify their own, hasher specific type (which implements Digest) for their output.

Associated Types

The maximum Digest size for that hasher (it is stack allocated).

The Digest type to distinguish the output of different Hasher implementations.

Required methods

Hashes the given input data and returns its hash digest.

Provided methods

Returns the allocated size of the digest.

Implementors