Merge rust-bitcoin/rust-bitcoin#2272: hashes: Add Hash::from_bytes_iter to construct hashes from iterators

94a6caf204 hashes: Add Hash::from_byte_chunks to construct hashes from iterators (Steven Roose)

Pull request description:

  Is there any interest in a method like this? I'm not necessarily a fan myself, was just in a situation where I was doing this a lot and it would save the me the "engine, from_engine dance" each time.

ACKs for top commit:
  Kixunil:
    ACK 94a6caf204
  apoelstra:
    ACK 94a6caf204

Tree-SHA512: 96e74366dbf64d50fb6642024d18eeeee5b78154fa152c845a1073c904599af69328a141f0a7e2bd1a7b43a091c886da57dfe6e87bab5a5e01b0f167019caae0
This commit is contained in:
Andrew Poelstra 2023-12-11 19:44:36 +00:00
commit ada8c53ce2
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 13 additions and 0 deletions

View File

@ -197,6 +197,19 @@ pub trait Hash:
Self::from_engine(engine) Self::from_engine(engine)
} }
/// Hashes all the byte slices retrieved from the iterator together.
fn hash_byte_chunks<B, I>(byte_slices: I) -> Self
where
B: AsRef<[u8]>,
I: IntoIterator<Item = B>,
{
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 /// Flag indicating whether user-visible serializations of this hash
/// should be backward. For some reason Satoshi decided this should be /// should be backward. For some reason Satoshi decided this should be
/// true for `Sha256dHash`, so here we are. /// true for `Sha256dHash`, so here we are.