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§
type Algorithm: HashAlgorithm
Sourcetype State: Sized + Clone
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).
Sourcetype Output: AsRef<[u8]>
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§
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 Methods§
Sourcefn hash(&mut self, algorithm: Self::Algorithm, data: &[u8]) -> Self::Output
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".