Bitcoin ABC 0.30.7
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 to this stream
162 ::Serialize(*this, obj);
163 return (*this);
164 }
165};
166
170template <typename Source> class CHashVerifier : public CHashWriter {
171private:
172 Source *source;
173
174public:
175 explicit CHashVerifier(Source *source_)
176 : CHashWriter(source_->GetType(), source_->GetVersion()),
177 source(source_) {}
178
180 source->read(dst);
181 this->write(dst);
182 }
183
184 void ignore(size_t nSize) {
185 std::byte data[1024];
186 while (nSize > 0) {
187 size_t now = std::min<size_t>(nSize, 1024);
188 read({data, now});
189 nSize -= now;
190 }
191 }
192
193 template <typename T> CHashVerifier<Source> &operator>>(T &&obj) {
194 // Unserialize from this stream
195 ::Unserialize(*this, obj);
196 return (*this);
197 }
198};
199
203template <typename Source> class HashedSourceWriter : public CHashWriter {
204private:
205 Source &m_source;
206
207public:
210 }
211
213 m_source.write(src);
215 }
216
217 template <typename T> HashedSourceWriter &operator<<(const T &obj) {
218 ::Serialize(*this, obj);
219 return *this;
220 }
221};
222
224template <typename T>
225uint256 SerializeHash(const T &obj, int nType = SER_GETHASH,
226 int nVersion = PROTOCOL_VERSION) {
227 CHashWriter ss(nType, nVersion);
228 ss << obj;
229 return ss.GetHash();
230}
231
232uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash);
233
234void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
235 const uint8_t data[32], uint8_t output[64]);
236
237#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:170
Source * source
Definition: hash.h:172
CHashVerifier(Source *source_)
Definition: hash.h:175
void ignore(size_t nSize)
Definition: hash.h:184
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:193
void read(Span< std::byte > dst)
Definition: hash.h:179
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:203
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:217
Source & m_source
Definition: hash.h:205
void write(Span< const std::byte > src)
Definition: hash.h:212
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:208
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:209
constexpr C * data() const noexcept
Definition: span.h:198
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:225
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:53
@ SER_GETHASH
Definition: serialize.h:154
void Unserialize(Stream &, char)=delete
void Serialize(Stream &, char)=delete
uint8_t * UCharCast(char *c)
Definition: span.h:309
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:337
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11