Skip to content

Commit 086cf38

Browse files
authored
digest: add TryCustomizedInit trait (#2395)
Some algorithms (e.g. Ascon-CXOF128 and `bash-prg-hash`) place restrictions on customization strings, so with the current version of `digest` we either have to panic on invalid strings or use inherent methods. This PR amends this by introducing a fallible variant of `CustomizedInit` with the blanket impl to act as a bridge between the traits.
1 parent 9488e7e commit 086cf38

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

digest/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## 0.11.3 (UNRELEASED)
99
### Added
1010
- `dev::initialized_mac_test` function ([#2367])
11+
- `TryCustomizedInit` trait ([#2395])
1112

1213
[#2367]: https://github.com/RustCrypto/traits/pull/2367
14+
[#2395]: https://github.com/RustCrypto/traits/pull/2395
1315

1416
## 0.11.2 (2026-03-13)
1517
### Changed

digest/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,27 @@ pub trait CustomizedInit: Sized {
205205
fn new_customized(customization: &[u8]) -> Self;
206206
}
207207

208+
/// Trait for hash functions with customization string for domain separation which place
209+
/// restrictions on customization strings.
210+
pub trait TryCustomizedInit: Sized {
211+
/// Error returned for invalid customization strings.
212+
type Error;
213+
214+
/// Create new hasher instance with the given customization string.
215+
///
216+
/// # Errors
217+
/// If the provided customization string is not valid for the hash function.
218+
fn try_new_customized(customization: &[u8]) -> Result<Self, Self::Error>;
219+
}
220+
221+
impl<T: CustomizedInit> TryCustomizedInit for T {
222+
type Error = core::convert::Infallible;
223+
224+
fn try_new_customized(customization: &[u8]) -> Result<Self, Self::Error> {
225+
Ok(Self::new_customized(customization))
226+
}
227+
}
228+
208229
/// Types with a certain collision resistance.
209230
pub trait CollisionResistance {
210231
/// Collision resistance in bytes.

0 commit comments

Comments
 (0)