hashes: Add with_input method to HashEngine
What changed, and why it matters
This commit adds a new convenience method called with_input to the hash engine API. It is purely a quality-of-life change that lets callers chain hash-input calls together. There is no security-relevant change: the existing input method still does the actual work, and no behavior, validation, or cryptographic logic is modified.
No security action required. Treat as a normal API enhancement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces a consuming, self-returning wrapper with_input(mut self, data: &[u8]) -> Self on the HashEngine trait. It simply calls the existing input(&mut self, ...) method and returns self, enabling method chaining. A unit test verifies that chained inputs produce the same digest as feeding the concatenated bytes through hash. No existing code paths are altered.
Changed components
hashes/src/lib.rsHashEngine trait APIInspect captured patch +16 / −0
diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs
index 2412be24..24bcbe41 100644
--- a/hashes/src/lib.rs
+++ b/hashes/src/lib.rs
@@ -175,6 +175,13 @@ pub trait HashEngine: Clone {
/// Adds data to the hash engine.
fn input(&mut self, data: &[u8]);
+ /// Adds data to the hash engine and returns the engine.
+ #[must_use]
+ fn with_input(mut self, data: &[u8]) -> Self {
+ self.input(data);
+ self
+ }
+
/// Returns the number of bytes already input into the engine.
fn n_bytes_hashed(&self) -> u64;
@@ -345,4 +352,13 @@ mod tests {
let roundtrip = hex.parse::<TestNewtype>().expect("failed to parse hex");
assert_eq!(roundtrip, orig);
}
+
+ #[test]
+ fn engine_with_input_chains() {
+ use crate::{sha256, HashEngine as _};
+
+ let chained =
+ sha256::HashEngine::default().with_input(b"abc").with_input(b"def").finalize();
+ assert_eq!(chained, sha256::Hash::hash(b"abcdef"));
+ }
}
Why this scored 20/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.