Bitcoin ABC 0.32.4
P2P Digital Currency
hash.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_HASH_H
7#define BITCOIN_HASH_H
8
9#include <attributes.h>
10#include <crypto/common.h>
11#include <crypto/ripemd160.h>
12#include <crypto/sha256.h>
13#include <prevector.h>
14#include <serialize.h>
15#include <uint256.h>
16#include <version.h>
17
18#include <vector>
19
21
23class CHash256 {
24private:
26
27public:
28 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29
30 void Finalize(Span<uint8_t> output) {
31 assert(output.size() == OUTPUT_SIZE);
32 uint8_t buf[CSHA256::OUTPUT_SIZE];
33 sha.Finalize(buf);
35 }
36
38 sha.Write(input.data(), input.size());
39 return *this;
40 }
41
43 sha.Reset();
44 return *this;
45 }
46};
47
49class CHash160 {
50private:
52
53public:
54 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
55
56 void Finalize(Span<uint8_t> output) {
57 assert(output.size() == OUTPUT_SIZE);
58 uint8_t buf[CSHA256::OUTPUT_SIZE];
59 sha.Finalize(buf);
61 }
62
64 sha.Write(input.data(), input.size());
65 return *this;
66 }
67
69 sha.Reset();
70 return *this;
71 }
72};
73
75template <typename T> inline uint256 Hash(const T &in1) {
76 uint256 result;
77 CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
78 return result;
79}
80
82template <typename T1, typename T2>
83inline uint256 Hash(const T1 &in1, const T2 &in2) {
84 uint256 result;
85 CHash256()
86 .Write(MakeUCharSpan(in1))
87 .Write(MakeUCharSpan(in2))
88 .Finalize(result);
89 return result;
90}
91
93template <typename T1> inline uint160 Hash160(const T1 &in1) {
94 uint160 result;
95 CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96 return result;
97}
98
101private:
103
104public:
106 ctx.Write(UCharCast(src.data()), src.size());
107 }
108
115 uint256 result;
116 ctx.Finalize(result.begin());
117 ctx.Reset()
119 .Finalize(result.begin());
120 return result;
121 }
122
129 uint256 result;
130 ctx.Finalize(result.begin());
131 return result;
132 }
133
137 inline uint64_t GetCheapHash() {
138 uint256 result = GetHash();
139 return ReadLE64(result.begin());
140 }
141
142 template <typename T> HashWriter &operator<<(const T &obj) {
143 ::Serialize(*this, obj);
144 return *this;
145 }
146};
147
148class CHashWriter : public HashWriter {
149private:
150 const int nType;
151 const int nVersion;
152
153public:
154 CHashWriter(int nTypeIn, int nVersionIn)
155 : nType(nTypeIn), nVersion(nVersionIn) {}
156
157 int GetType() const { return nType; }
158 int GetVersion() const { return nVersion; }
159
160 template <typename T> CHashWriter &operator<<(const T &obj) {
161 ::Serialize(*this, obj);
162 return (*this);
163 }
164};
165
169template <typename Source> class CHashVerifier : public CHashWriter {
170private:
171 Source *source;
172
173public:
174 explicit CHashVerifier(Source *source_)
175 : CHashWriter(source_->GetType(), source_->GetVersion()),
176 source(source_) {}
177
179 source->read(dst);
180 this->write(dst);
181 }
182
183 void ignore(size_t nSize) {
184 std::byte data[1024];
185 while (nSize > 0) {
186 size_t now = std::min<size_t>(nSize, 1024);
187 read({data, now});
188 nSize -= now;
189 }
190 }
191
192 template <typename T> CHashVerifier<Source> &operator>>(T &&obj) {
193 ::Unserialize(*this, obj);
194 return (*this);
195 }
196};
197
201template <typename Source> class HashedSourceWriter : public CHashWriter {
202private:
203 Source &m_source;
204
205public:
208 }
209
211 m_source.write(src);
213 }
214
215 template <typename T> HashedSourceWriter &operator<<(const T &obj) {
216 ::Serialize(*this, obj);
217 return *this;
218 }
219};
220
222template <typename T>
223uint256 SerializeHash(const T &obj, int nType = SER_GETHASH,
224 int nVersion = PROTOCOL_VERSION) {
225 CHashWriter ss(nType, nVersion);
226 ss << obj;
227 return ss.GetHash();
228}
229
230uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash);
231
232void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
233 const uint8_t data[32], uint8_t output[64]);
234
235#endif // BITCOIN_HASH_H
#define LIFETIMEBOUND
Definition: attributes.h:16
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
CHash160 & Reset()
Definition: hash.h:68
static const size_t OUTPUT_SIZE
Definition: hash.h:54
CHash160 & Write(Span< const uint8_t > input)
Definition: hash.h:63
CSHA256 sha
Definition: hash.h:51
void Finalize(Span< uint8_t > output)
Definition: hash.h:56
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:23
CHash256 & Write(Span< const uint8_t > input)
Definition: hash.h:37
void Finalize(Span< uint8_t > output)
Definition: hash.h:30
static const size_t OUTPUT_SIZE
Definition: hash.h:28
CHash256 & Reset()
Definition: hash.h:42
CSHA256 sha
Definition: hash.h:25
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:169
Source * source
Definition: hash.h:171
CHashVerifier(Source *source_)
Definition: hash.h:174
void ignore(size_t nSize)
Definition: hash.h:183
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:192
void read(Span< std::byte > dst)
Definition: hash.h:178
CHashWriter & operator<<(const T &obj)
Definition: hash.h:160
int GetType() const
Definition: hash.h:157
const int nType
Definition: hash.h:150
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:154
int GetVersion() const
Definition: hash.h:158
const int nVersion
Definition: hash.h:151
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:288
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:313
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Reset()
Definition: sha256.cpp:860
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
CSHA256 ctx
Definition: hash.h:102
void write(Span< const std::byte > src)
Definition: hash.h:105
HashWriter & operator<<(const T &obj)
Definition: hash.h:142
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:137
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:114
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:128
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:201
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:215
Source & m_source
Definition: hash.h:203
void write(Span< const std::byte > src)
Definition: hash.h:210
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:206
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
constexpr std::size_t size() const noexcept
Definition: span.h:210
constexpr C * data() const noexcept
Definition: span.h:199
uint8_t * begin()
Definition: uint256.h:85
160-bit opaque blob.
Definition: uint256.h:117
256-bit opaque blob.
Definition: uint256.h:129
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:93
uint32_t MurmurHash3(uint32_t nHashSeed, Span< const uint8_t > vDataToHash)
Definition: hash.cpp:14
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
Definition: hash.cpp:72
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:223
uint256 ChainCode
Definition: hash.h:20
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
const char * source
Definition: rpcconsole.cpp:56
void Serialize(Stream &, V)=delete
@ SER_GETHASH
Definition: serialize.h:156
void Unserialize(Stream &, V)=delete
uint8_t * UCharCast(char *c)
Definition: span.h:310
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
Definition: span.h:350
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11