Skip to content

Commit

Permalink
Add Update::chain and Digest::new_with_prefix (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Dec 14, 2021
1 parent f7cd3ab commit ffd8fe7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
10 changes: 10 additions & 0 deletions digest/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.10.1 (2021-12-14)
### Added
- `Update::chain` and `Digest::new_with_prefix` methods. ([#846])

### Fixed
- Doc cfg attribute for CtOutput and MacError. ([#842])

[#842]: https://github.com/RustCrypto/traits/pull/842
[#846]: https://github.com/RustCrypto/traits/pull/846

## 0.10.0 (2021-12-07)
### Changed
- Dirty traits are removed and instead block-level traits are introduced.
Expand Down
15 changes: 14 additions & 1 deletion digest/src/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ pub trait HashMarker {}
/// This trait wraps [`Update`], [`FixedOutput`], [`Default`], and
/// [`HashMarker`] traits and provides additional convenience methods.
pub trait Digest: OutputSizeUser {
/// Create new hasher instance
/// Create new hasher instance.
fn new() -> Self;

/// Create new hasher instance which has processed the provided data.
fn new_with_prefix(data: impl AsRef<[u8]>) -> Self;

/// Process data, updating the internal state.
fn update(&mut self, data: impl AsRef<[u8]>);

Expand Down Expand Up @@ -57,6 +60,16 @@ impl<D: FixedOutput + Default + Update + HashMarker> Digest for D {
Self::default()
}

#[inline]
fn new_with_prefix(data: impl AsRef<[u8]>) -> Self
where
Self: Default + Sized,
{
let mut h = Self::default();
h.update(data.as_ref());
h
}

#[inline]
fn update(&mut self, data: impl AsRef<[u8]>) {
Update::update(self, data.as_ref());
Expand Down
9 changes: 9 additions & 0 deletions digest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ use core::fmt;
pub trait Update {
/// Update state using the provided data.
fn update(&mut self, data: &[u8]);

/// Digest input data in a chained manner.
fn chain(mut self, data: impl AsRef<[u8]>) -> Self
where
Self: Sized,
{
self.update(data.as_ref());
self
}
}

/// Trait for hash functions with fixed-size output.
Expand Down

0 comments on commit ffd8fe7

Please sign in to comment.