From 94a6caf204062a5c6b958cf6d866c13913537e27 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Sun, 10 Dec 2023 18:03:52 +0000 Subject: [PATCH] hashes: Add Hash::from_byte_chunks to construct hashes from iterators --- hashes/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index 10c47d75..dc73b38e 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -197,6 +197,19 @@ pub trait Hash: Self::from_engine(engine) } + /// Hashes all the byte slices retrieved from the iterator together. + fn hash_byte_chunks(byte_slices: I) -> Self + where + B: AsRef<[u8]>, + I: IntoIterator, + { + let mut engine = Self::engine(); + for slice in byte_slices { + engine.input(slice.as_ref()); + } + Self::from_engine(engine) + } + /// Flag indicating whether user-visible serializations of this hash /// should be backward. For some reason Satoshi decided this should be /// true for `Sha256dHash`, so here we are.