Skip to main content

HashProvider

Trait HashProvider 

Source
pub trait HashProvider {
    type Algorithm: HashAlgorithm;
    type State: Sized + Clone;
    type Output: AsRef<[u8]>;

    // Required methods
    fn init(&mut self, algorithm: Self::Algorithm) -> Self::State;
    fn update(&mut self, instance: &mut Self::State, data: &[u8]);
    fn finalize(&mut self, instance: Self::State) -> Self::Output;

    // Provided method
    fn hash(&mut self, algorithm: Self::Algorithm, data: &[u8]) -> Self::Output { ... }
}
Expand description

Hashing of byte streams.

Required Associated Types§

Source

type Algorithm: HashAlgorithm

Source

type State: Sized + Clone

State in which is carried between rounds of feeding data.

As construction is not fallible, this can not be a handle into a limited pool. (Cf. architecture requirements: “Incomplete operations should not block the system”).

If hardware exists that can only hash efficiently in an internal state, this needs to be an encapsulation of that state, as construction is not fallible. As this is likely a costly process, such implementations are encouraged to implement Self::hash in an optimized way. (Also, if such a hardware actually exists, please open an issue about it).

Source

type Output: AsRef<[u8]>

Output of a hashing operation.

This needs to be sufficiently large to contain any selected hash’s output. When collecting multiple hash results of the same algorithm in limited space (i.e., in situations when it makes sense to store 8 SHA-512 outputs or 16 SHA-256 outputs), it can make sense to copy data out rather than storing the HashResult type to free the space. (See also project FAQ on output sizes).

Required Methods§

Source

fn init(&mut self, algorithm: Self::Algorithm) -> Self::State

Source

fn update(&mut self, instance: &mut Self::State, data: &[u8])

Source

fn finalize(&mut self, instance: Self::State) -> Self::Output

Provided Methods§

Source

fn hash(&mut self, algorithm: Self::Algorithm, data: &[u8]) -> Self::Output

Hash contiguous in-memory data in a single pass.

This method is provided, but implementations are encouraged to provide optimized versions if an actual speed-up can be gained; conversely, users are encouraged to use this if data is already present in this form.

Optimized versions are expected to be rare, though, so don’t go out of your way using it: Only buffer the full data, or create special cases for when there actually is just one item in an iterator, without testing and possibly consulting with the back-end authors first.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§