Skip to content

Commit 42d0a04

Browse files
committed
add finalize_into methods to DynDigest
1 parent ff33848 commit 42d0a04

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

digest/src/core_api.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<D: FixedOutputCore + Reset> FixedOutput for CoreWrapper<D, D::BlockSize> {
9595

9696
#[inline]
9797
fn finalize_into(mut self, out: &mut GenericArray<u8, Self::OutputSize>) {
98-
let Self { core, buffer} = &mut self;
98+
let Self { core, buffer } = &mut self;
9999
core.finalize_fixed_core(buffer, out);
100100
}
101101

@@ -120,17 +120,23 @@ impl<D: ExtendableOutputCore + Reset> ExtendableOutput for CoreWrapper<D, D::Blo
120120

121121
#[inline]
122122
fn finalize_xof(mut self) -> Self::Reader {
123-
let Self { core, buffer} = &mut self;
123+
let Self { core, buffer } = &mut self;
124124
let reader_core = core.finalize_xof_core(buffer);
125-
CoreWrapper { core: reader_core, buffer: Default::default() }
125+
CoreWrapper {
126+
core: reader_core,
127+
buffer: Default::default(),
128+
}
126129
}
127130

128131
#[inline]
129132
fn finalize_xof_reset(&mut self) -> Self::Reader {
130133
let Self { core, buffer } = self;
131134
let reader_core = core.finalize_xof_core(buffer);
132135
self.reset();
133-
CoreWrapper { core: reader_core, buffer: Default::default() }
136+
CoreWrapper {
137+
core: reader_core,
138+
buffer: Default::default(),
139+
}
134140
}
135141
}
136142

@@ -156,4 +162,3 @@ impl<R: XofReaderCore> std::io::Read for CoreWrapper<R, R::BlockSize> {
156162
Ok(buf.len())
157163
}
158164
}
159-

digest/src/dyn_digest.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#![cfg(feature = "alloc")]
21
use alloc::boxed::Box;
2+
use core::fmt;
33

44
use super::{FixedOutput, Reset, Update};
5-
use generic_array::typenum::Unsigned;
5+
use generic_array::{typenum::Unsigned, GenericArray};
66

77
/// The `DynDigest` trait is a modification of `Digest` trait suitable
88
/// for trait objects.
@@ -19,6 +19,16 @@ pub trait DynDigest {
1919
/// Retrieve result and consume boxed hasher instance
2020
fn finalize(self: Box<Self>) -> Box<[u8]>;
2121

22+
/// Write result into provided array and consume the hasher instance.
23+
///
24+
/// Returns error if buffer length is not equal to `output_size`.
25+
fn finalize_into(self, buf: &mut [u8]) -> Result<(), InvalidBufferLength>;
26+
27+
/// Write result into provided array and reset the hasher instance.
28+
///
29+
/// Returns error if buffer length is not equal to `output_size`.
30+
fn finalize_into_reset(&mut self, out: &mut [u8]) -> Result<(), InvalidBufferLength>;
31+
2232
/// Reset hasher instance to its initial state.
2333
fn reset(&mut self);
2434

@@ -42,6 +52,24 @@ impl<D: Update + FixedOutput + Reset + Clone + 'static> DynDigest for D {
4252
self.finalize_fixed().to_vec().into_boxed_slice()
4353
}
4454

55+
fn finalize_into(self, buf: &mut [u8]) -> Result<(), InvalidBufferLength> {
56+
if buf.len() == self.output_size() {
57+
self.finalize_into(GenericArray::from_mut_slice(buf));
58+
Ok(())
59+
} else {
60+
Err(InvalidBufferLength)
61+
}
62+
}
63+
64+
fn finalize_into_reset(&mut self, buf: &mut [u8]) -> Result<(), InvalidBufferLength> {
65+
if buf.len() == self.output_size() {
66+
self.finalize_into_reset(GenericArray::from_mut_slice(buf));
67+
Ok(())
68+
} else {
69+
Err(InvalidBufferLength)
70+
}
71+
}
72+
4573
fn reset(&mut self) {
4674
Reset::reset(self);
4775
}
@@ -60,3 +88,16 @@ impl Clone for Box<dyn DynDigest> {
6088
self.box_clone()
6189
}
6290
}
91+
92+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
93+
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq)]
94+
pub struct InvalidBufferLength;
95+
96+
impl fmt::Display for InvalidBufferLength {
97+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98+
f.write_str("invalid buffer length")
99+
}
100+
}
101+
102+
#[cfg(feature = "std")]
103+
impl std::error::Error for InvalidBufferLength {}

digest/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,16 @@ pub mod dev;
5050
#[cfg(feature = "core-api")]
5151
mod core_api;
5252
mod digest;
53+
#[cfg(feature = "alloc")]
5354
mod dyn_digest;
5455

55-
#[cfg(feature = "core-api")]
56-
pub use block_buffer;
5756
#[cfg(feature = "core-api")]
5857
pub use crate::core_api::{CoreWrapper, ExtendableOutputCore, FixedOutputCore, UpdateCore};
5958
pub use crate::digest::{Digest, Output};
59+
#[cfg(feature = "core-api")]
60+
pub use block_buffer;
6061
#[cfg(feature = "alloc")]
61-
pub use dyn_digest::DynDigest;
62+
pub use dyn_digest::{DynDigest, InvalidBufferLength};
6263
pub use generic_array::{self, typenum::consts, ArrayLength, GenericArray};
6364

6465
/// Trait for updating hasher state with input data.

0 commit comments

Comments
 (0)