Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Each of these now defines two traits and a blanket impl, so `lib.rs` was getting rather bloated. This commit factors each of them into their own modules.
- Loading branch information
Showing
5 changed files
with
281 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! Fixed-size output digest support | ||
|
||
use crate::Reset; | ||
use generic_array::{ArrayLength, GenericArray}; | ||
|
||
/// Trait for returning digest result with the fixed size | ||
pub trait FixedOutput { | ||
/// Output size for fixed output digest | ||
type OutputSize: ArrayLength<u8>; | ||
|
||
/// Write result into provided array and consume the hasher instance. | ||
fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>); | ||
|
||
/// Write result into provided array and reset the hasher instance. | ||
fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>); | ||
|
||
/// Retrieve result and consume the hasher instance. | ||
#[inline] | ||
fn finalize_fixed(self) -> GenericArray<u8, Self::OutputSize> | ||
where | ||
Self: Sized, | ||
{ | ||
let mut out = Default::default(); | ||
self.finalize_into(&mut out); | ||
out | ||
} | ||
|
||
/// Retrieve result and reset the hasher instance. | ||
#[inline] | ||
fn finalize_fixed_reset(&mut self) -> GenericArray<u8, Self::OutputSize> { | ||
let mut out = Default::default(); | ||
self.finalize_into_reset(&mut out); | ||
out | ||
} | ||
} | ||
|
||
/// Trait for fixed-output digest implementations to use to retrieve the | ||
/// hash output. | ||
/// | ||
/// Usage of this trait in user code is discouraged. Instead use the | ||
/// [`FixedOutput::finalize_fixed`] or [`FixedOutput::finalize_fixed_reset`] | ||
/// methods. | ||
/// | ||
/// Types which impl this trait along with [`Reset`] will receive a blanket | ||
/// impl of [`FixedOutput`]. | ||
pub trait FixedOutputDirty { | ||
/// Output size for fixed output digest | ||
type OutputSize: ArrayLength<u8>; | ||
|
||
/// Retrieve result into provided buffer and leave hasher in a dirty state. | ||
/// | ||
/// Implementations should panic if this is called twice without resetting. | ||
fn finalize_into_dirty(&mut self, out: &mut GenericArray<u8, Self::OutputSize>); | ||
} | ||
|
||
impl<D: FixedOutputDirty + Reset> FixedOutput for D { | ||
type OutputSize = D::OutputSize; | ||
|
||
#[inline] | ||
fn finalize_into(mut self, out: &mut GenericArray<u8, Self::OutputSize>) { | ||
self.finalize_into_dirty(out); | ||
} | ||
|
||
#[inline] | ||
fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) { | ||
self.finalize_into_dirty(out); | ||
self.reset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.