hashes: Add drain_to_hash and encode_to_hash
What changed, and why it matters
This commit adds two new convenience functions to the rust-bitcoin hashes library. They are purely additive helper functions that wrap existing, safe hashing operations into a one-step call. There is no indication of any security bug, fix, or behavior change.
No security action needed. Treat as a normal API addition and review for API ergonomics if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit introduces encode_to_hash<T, H> and drain_to_hash<T, H> in hashes/src/lib.rs. Both functions create a default HashEngine, feed data into it via the pre-existing encode_to_engine/drain_to_engine helpers, and return engine.finalize(). The code is a thin, safe wrapper with no unsafe blocks, no cryptographic changes, and no modifications to existing APIs.
Changed components
hashes/src/lib.rsInspect captured patch +22 / −0
diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs
index 24bcbe41..5bc7dc08 100644
--- a/hashes/src/lib.rs
+++ b/hashes/src/lib.rs
@@ -223,6 +223,28 @@ where
}
}
+/// Encodes an object into a hash value.
+pub fn encode_to_hash<T, H>(obj: &T) -> H::Hash
+where
+ T: encoding::Encode + ?Sized,
+ H: HashEngine + Default,
+{
+ let mut engine = H::default();
+ encode_to_engine(obj, &mut engine);
+ engine.finalize()
+}
+
+/// Drains the output of an [`encoding::Encoder`] into a hash value.
+pub fn drain_to_hash<T, H>(encoder: &mut T) -> H::Hash
+where
+ T: encoding::Encoder + ?Sized,
+ H: HashEngine + Default,
+{
+ let mut engine = H::default();
+ drain_to_engine(encoder, &mut engine);
+ engine.finalize()
+}
+
/// Trait which applies to hashes of all types.
pub trait Hash:
Copy + Clone + PartialEq + Eq + PartialOrd + Ord + hash::Hash + convert::AsRef<[u8]>
Why this scored 15/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.