Skip to content

Commit

Permalink
Add hash customization traits
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpelissier committed Jul 17, 2023
1 parent 962daba commit a9a41e3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions digest/src/core_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ pub trait VariableOutputCore: UpdateCore + OutputSizeUser + BufferKindUser + Siz
fn finalize_variable_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>);
}

/// Core trait for hash functions with customization string.
pub trait CustomOutputCore {
/// Initialize hasher with customization string.
fn new(customization: &[u8]) -> Self;
}

/// Type which used for defining truncation side in the [`VariableOutputCore`]
/// trait.
#[derive(Copy, Clone, Debug)]
Expand Down
14 changes: 13 additions & 1 deletion digest/src/core_api/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use super::{
AlgorithmName, Buffer, BufferKindUser, ExtendableOutputCore, FixedOutputCore, OutputSizeUser,
Reset, UpdateCore, XofReaderCoreWrapper,
};
use crate::core_api::CustomOutputCore;
use crate::{
ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update,
CustomInit, ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker,
Update,
};
use block_buffer::BlockBuffer;
use core::fmt;
Expand Down Expand Up @@ -184,6 +186,16 @@ where
}
}

impl<T> CustomInit for CoreWrapper<T>
where
T: BufferKindUser + CustomOutputCore,
{
#[inline]
fn new_custom(customization: &[u8]) -> Self {
Self::from_core(T::new(customization))
}
}

#[cfg(feature = "oid")]
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
impl<T> AssociatedOid for CoreWrapper<T>
Expand Down
6 changes: 6 additions & 0 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ pub trait VariableOutputReset: VariableOutput + Reset {
}
}

/// Trait for hash functions with customization string for domain separation.
pub trait CustomInit {
/// Create new hasher instance with the given customization string.
fn new_custom(customization: &[u8]) -> Self;
}

/// The error type used in variable hash traits.
#[derive(Clone, Copy, Debug, Default)]
pub struct InvalidOutputSize;
Expand Down

0 comments on commit a9a41e3

Please sign in to comment.