diff --git a/digest/src/lib.rs b/digest/src/lib.rs index 79bef801d..1d005d15f 100644 --- a/digest/src/lib.rs +++ b/digest/src/lib.rs @@ -79,8 +79,59 @@ pub trait FixedOutput { /// Output size for fixed output digest type OutputSize: ArrayLength; - /// Retrieve result and consume hasher instance. - fn finalize_fixed(self) -> GenericArray; + /// Write result into provided array and consume the hasher instance. + fn finalize_into(self, out: &mut GenericArray); + + /// Write result into provided array and reset the hasher instance. + fn finalize_into_reset(&mut self, out: &mut GenericArray); + + /// Retrieve result and consume the hasher instance. + #[inline] + fn finalize_fixed(self) -> GenericArray + 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 { + 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. +pub trait FixedOutputDirty { + /// Output size for fixed output digest + type OutputSize: ArrayLength; + + /// Retrieve result into provided buffer and leave hasher in a dirty state. + /// Usage of this method in user code is discouraged, prefer `finalize_fixed` + /// or `finalize_fixed_reset`. + /// + /// Implementations should panic if this is called twice without resetting. + fn finalize_into_dirty(&mut self, out: &mut GenericArray); +} + +impl FixedOutput for D { + type OutputSize = D::OutputSize; + + #[inline] + fn finalize_into(mut self, out: &mut GenericArray) { + self.finalize_into_dirty(out); + } + + #[inline] + fn finalize_into_reset(&mut self, out: &mut GenericArray) { + self.finalize_into_dirty(out); + self.reset(); + } } /// Trait for returning digest result with the variable size