Skip to content

Commit fa96114

Browse files
author
MarcoFalke
committed
Add HashVerifier
It is similar to CHashVerifier, but HashVerifier does not need a serialize type and version
1 parent d8bdee0 commit fa96114

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/hash.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_HASH_H
77
#define BITCOIN_HASH_H
88

9+
#include <attributes.h>
910
#include <crypto/common.h>
1011
#include <crypto/ripemd160.h>
1112
#include <crypto/sha256.h>
@@ -165,6 +166,39 @@ class CHashWriter : public HashWriter
165166
};
166167

167168
/** Reads data from an underlying stream, while hashing the read data. */
169+
template <typename Source>
170+
class HashVerifier : public HashWriter
171+
{
172+
private:
173+
Source& m_source;
174+
175+
public:
176+
explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
177+
178+
void read(Span<std::byte> dst)
179+
{
180+
m_source.read(dst);
181+
this->write(dst);
182+
}
183+
184+
void ignore(size_t num_bytes)
185+
{
186+
std::byte data[1024];
187+
while (num_bytes > 0) {
188+
size_t now = std::min<size_t>(num_bytes, 1024);
189+
read({data, now});
190+
num_bytes -= now;
191+
}
192+
}
193+
194+
template <typename T>
195+
HashVerifier<Source>& operator>>(T&& obj)
196+
{
197+
::Unserialize(*this, obj);
198+
return *this;
199+
}
200+
};
201+
168202
template<typename Source>
169203
class CHashVerifier : public CHashWriter
170204
{

0 commit comments

Comments
 (0)