diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-01-03 12:56:06 +0100 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-01-03 12:54:45 +0100 |
commit | fa961141f7fc515fbb6fcb9d8417108034b256ce (patch) | |
tree | 2c6d2ad71a491dd32dd837e763948ed036770dc7 /src/hash.h | |
parent | d8bdee0fc889def7c5f5076da13db4fce0a3728a (diff) |
Add HashVerifier
It is similar to CHashVerifier, but HashVerifier does not need a
serialize type and version
Diffstat (limited to 'src/hash.h')
-rw-r--r-- | src/hash.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/hash.h b/src/hash.h index b18a031268..b2ef29fcdb 100644 --- a/src/hash.h +++ b/src/hash.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_HASH_H #define BITCOIN_HASH_H +#include <attributes.h> #include <crypto/common.h> #include <crypto/ripemd160.h> #include <crypto/sha256.h> @@ -165,6 +166,39 @@ public: }; /** Reads data from an underlying stream, while hashing the read data. */ +template <typename Source> +class HashVerifier : public HashWriter +{ +private: + Source& m_source; + +public: + explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {} + + void read(Span<std::byte> dst) + { + m_source.read(dst); + this->write(dst); + } + + void ignore(size_t num_bytes) + { + std::byte data[1024]; + while (num_bytes > 0) { + size_t now = std::min<size_t>(num_bytes, 1024); + read({data, now}); + num_bytes -= now; + } + } + + template <typename T> + HashVerifier<Source>& operator>>(T&& obj) + { + ::Unserialize(*this, obj); + return *this; + } +}; + template<typename Source> class CHashVerifier : public CHashWriter { |