Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 <crypto/common.h>
10#include <crypto/ripemd160.h>
11#include <crypto/sha256.h>
12#include <prevector.h>
13#include <serialize.h>
14#include <uint256.h>
15#include <version.h>
16
17#include <vector>
18
20
22class CHash256 {
23private:
25
26public:
27 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
28
29 void Finalize(Span<uint8_t> output) {
30 assert(output.size() == OUTPUT_SIZE);
32 sha.Finalize(buf);
34 }
35
37 sha.Write(input.data(), input.size());
38 return *this;
39 }
40
42 sha.Reset();
43 return *this;
44 }
45};
46
48class CHash160 {
49private:
51
52public:
53 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54
55 void Finalize(Span<uint8_t> output) {
56 assert(output.size() == OUTPUT_SIZE);
58 sha.Finalize(buf);
60 }
61
63 sha.Write(input.data(), input.size());
64 return *this;
65 }
66
68 sha.Reset();
69 return *this;
70 }
71};
72
74template <typename T> inline uint256 Hash(const T &in1) {
75 uint256 result;
77 return result;
78}
79
81template <typename T1, typename T2>
82inline uint256 Hash(const T1 &in1, const T2 &in2) {
83 uint256 result;
84 CHash256()
87 .Finalize(result);
88 return result;
89}
90
92template <typename T1> inline uint160 Hash160(const T1 &in1) {
93 uint160 result;
95 return result;
96}
97
100private:
102
103public:
105 ctx.Write(UCharCast(src.data()), src.size());
106 }
107
114 uint256 result;
115 ctx.Finalize(result.begin());
116 ctx.Reset()
118 .Finalize(result.begin());
119 return result;
120 }
121
128 uint256 result;
129 ctx.Finalize(result.begin());
130 return result;
131 }
132
137 uint256 result = GetHash();
138 return ReadLE64(result.begin());
139 }
140
141 template <typename T> HashWriter &operator<<(const T &obj) {
142 ::Serialize(*this, obj);
143 return *this;
144 }
145};
146
147class CHashWriter : public HashWriter {
148private:
149 const int nType;
150 const int nVersion;
151
152public:
155
156 int GetType() const { return nType; }
157 int GetVersion() const { return nVersion; }
158
159 template <typename T> CHashWriter &operator<<(const T &obj) {
160 // Serialize to this stream
161 ::Serialize(*this, obj);
162 return (*this);
163 }
164};
165
169template <typename Source> class CHashVerifier : public CHashWriter {
170private:
171 Source *source;
172
173public:
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 from this stream
194 ::Unserialize(*this, obj);
195 return (*this);
196 }
197};
198
200template <typename T>
201uint256 SerializeHash(const T &obj, int nType = SER_GETHASH,
202 int nVersion = PROTOCOL_VERSION) {
203 CHashWriter ss(nType, nVersion);
204 ss << obj;
205 return ss.GetHash();
206}
207
209
210void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
211 const uint8_t data[32], uint8_t output[64]);
212
213#endif // BITCOIN_HASH_H
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition hash.h:48
CHash160 & Reset()
Definition hash.h:67
static const size_t OUTPUT_SIZE
Definition hash.h:53
CHash160 & Write(Span< const uint8_t > input)
Definition hash.h:62
CSHA256 sha
Definition hash.h:50
void Finalize(Span< uint8_t > output)
Definition hash.h:55
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition hash.h:22
CHash256 & Write(Span< const uint8_t > input)
Definition hash.h:36
void Finalize(Span< uint8_t > output)
Definition hash.h:29
static const size_t OUTPUT_SIZE
Definition hash.h:27
CHash256 & Reset()
Definition hash.h:41
CSHA256 sha
Definition hash.h:24
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:159
int GetType() const
Definition hash.h:156
const int nType
Definition hash.h:149
CHashWriter(int nTypeIn, int nVersionIn)
Definition hash.h:153
int GetVersion() const
Definition hash.h:157
const int nVersion
Definition hash.h:150
A hasher class for RIPEMD-160.
Definition ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
void Finalize(uint8_t hash[OUTPUT_SIZE])
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:99
CSHA256 ctx
Definition hash.h:101
void write(Span< const std::byte > src)
Definition hash.h:104
HashWriter & operator<<(const T &obj)
Definition hash.h:141
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition hash.h:136
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition hash.h:113
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition hash.h:127
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:92
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:201
uint256 ChainCode
Definition hash.h:19
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition hash.h:74
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
@ 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