Bitcoin ABC 0.32.6
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
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);
31 uint8_t buf[CSHA256::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);
57 uint8_t buf[CSHA256::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;
76 CHash256().Write(MakeUCharSpan(in1)).Finalize(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()
85 .Write(MakeUCharSpan(in1))
86 .Write(MakeUCharSpan(in2))
87 .Finalize(result);
88 return result;
89}
90
92template <typename T1> inline uint160 Hash160(const T1 &in1) {
93 uint160 result;
94 CHash160().Write(MakeUCharSpan(in1)).Finalize(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
136 inline uint64_t GetCheapHash() {
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
150template <typename Source> class HashVerifier : public HashWriter {
151private:
152 Source &m_source;
153
154public:
156
158 m_source.read(dst);
159 this->write(dst);
160 }
161
162 void ignore(size_t num_bytes) {
163 std::byte data[1024];
164 while (num_bytes > 0) {
165 size_t now = std::min<size_t>(num_bytes, 1024);
166 read({data, now});
167 num_bytes -= now;
168 }
169 }
170
171 template <typename T> HashVerifier<Source> &operator>>(T &&obj) {
172 ::Unserialize(*this, obj);
173 return *this;
174 }
175};
176
180template <typename Source> class HashedSourceWriter : public HashWriter {
181private:
182 Source &m_source;
183
184public:
186 : HashWriter{}, m_source{source} {}
187
189 m_source.write(src);
191 }
192
193 template <typename T> HashedSourceWriter &operator<<(const T &obj) {
194 ::Serialize(*this, obj);
195 return *this;
196 }
197};
198
199uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash);
200
201void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
202 const uint8_t data[32], uint8_t output[64]);
203
204#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: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
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
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:150
Source & m_source
Definition: hash.h:152
HashVerifier(Source &source LIFETIMEBOUND)
Definition: hash.h:155
void read(Span< std::byte > dst)
Definition: hash.h:157
void ignore(size_t num_bytes)
Definition: hash.h:162
HashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:171
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
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:180
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:193
Source & m_source
Definition: hash.h:182
void write(Span< const std::byte > src)
Definition: hash.h:188
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:185
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:25
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 ChainCode
Definition: hash.h:19
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
const char * source
Definition: rpcconsole.cpp:56
void Serialize(Stream &, V)=delete
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())