Skip to content

Commit 14159d0

Browse files
Add hash customization traits
1 parent 962daba commit 14159d0

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

digest/src/core_api.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ pub trait VariableOutputCore: UpdateCore + OutputSizeUser + BufferKindUser + Siz
9393
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>);
9494
}
9595

96+
/// Core trait for hash functions with customization string.
97+
pub trait CustomOutputCore {
98+
/// Initialize hasher with customization string.
99+
fn new(customization: &[u8]) -> Self;
100+
}
101+
96102
/// Type which used for defining truncation side in the [`VariableOutputCore`]
97103
/// trait.
98104
#[derive(Copy, Clone, Debug)]

digest/src/core_api/wrapper.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use super::{
33
Reset, UpdateCore, XofReaderCoreWrapper,
44
};
55
use crate::{
6-
ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update,
6+
CustomInit, ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset,
7+
HashMarker, Update,
78
};
9+
use crate::core_api::CustomOutputCore;
810
use block_buffer::BlockBuffer;
911
use core::fmt;
1012
use crypto_common::{BlockSizeUser, InvalidLength, Key, KeyInit, KeySizeUser, Output};
@@ -184,6 +186,16 @@ where
184186
}
185187
}
186188

189+
impl<T> CustomInit for CoreWrapper<T>
190+
where
191+
T: BufferKindUser + CustomOutputCore
192+
{
193+
#[inline]
194+
fn new_custom(customization: &[u8]) -> Self {
195+
Self::from_core(T::new(customization))
196+
}
197+
}
198+
187199
#[cfg(feature = "oid")]
188200
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
189201
impl<T> AssociatedOid for CoreWrapper<T>

digest/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ pub trait VariableOutputReset: VariableOutput + Reset {
273273
}
274274
}
275275

276+
/// Trait for hash functions with customization string for domain separation.
277+
pub trait CustomInit {
278+
/// Create new hasher instance with the given customization string.
279+
fn new_custom(customization: &[u8]) -> Self;
280+
}
281+
276282
/// The error type used in variable hash traits.
277283
#[derive(Clone, Copy, Debug, Default)]
278284
pub struct InvalidOutputSize;

0 commit comments

Comments
 (0)