Skip to content

Commit

Permalink
Add hash personalization traits
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainpelissier committed Jul 17, 2023
1 parent 962daba commit 0e16ed1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 17 additions & 1 deletion digest/src/core_api/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use super::{
Reset, UpdateCore, XofReaderCoreWrapper,
};
use crate::{
ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker, Update,
ExtendableOutput, ExtendableOutputReset, FixedOutput, FixedOutputReset, HashMarker,
PersonalizationInit, Update,
};
use block_buffer::BlockBuffer;
use core::fmt;
Expand Down Expand Up @@ -184,6 +185,21 @@ where
}
}

impl<T> PersonalizationInit for CoreWrapper<T>
where
T: BufferKindUser + PersonalizationInit,
{
type PersonalizationExtArg = T::PersonalizationExtArg;
#[inline]
fn new(personalization: &[u8]) -> Self {
Self::from_core(T::new(personalization))
}

fn new_ext(personalization_ext: &Self::PersonalizationExtArg) -> Self {
Self::from_core(T::new_ext(personalization_ext))
}
}

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

/// Trait for hash functions with personalization string for domain separation.
pub trait PersonalizationInit: Sized {
// It would be nice to define a default value equal to `[u8]`, but unfortunately
// associated type defaults are currently unstable

/// Extended personalization
type PersonalizationExtArg;

/// Create new hasher instance with the given personalization string.
fn new(personalization: &[u8]) -> Self;

/// Create new hasher instance with the given extended personalization.
fn new_ext(personalization_ext: &Self::PersonalizationExtArg) -> Self;
}

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

0 comments on commit 0e16ed1

Please sign in to comment.