Bitcoin Core  26.1.0
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <span.h>
16 #include <uint256.h>
17 #include <version.h>
18 
19 #include <string>
20 #include <vector>
21 
23 
25 class CHash256 {
26 private:
28 public:
29  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
30 
32  assert(output.size() == OUTPUT_SIZE);
33  unsigned char buf[CSHA256::OUTPUT_SIZE];
34  sha.Finalize(buf);
35  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
36  }
37 
39  sha.Write(input.data(), input.size());
40  return *this;
41  }
42 
44  sha.Reset();
45  return *this;
46  }
47 };
48 
50 class CHash160 {
51 private:
53 public:
54  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
55 
57  assert(output.size() == OUTPUT_SIZE);
58  unsigned char 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 
75 template<typename T>
76 inline uint256 Hash(const T& in1)
77 {
78  uint256 result;
79  CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
80  return result;
81 }
82 
84 template<typename T1, typename T2>
85 inline uint256 Hash(const T1& in1, const T2& in2) {
86  uint256 result;
88  return result;
89 }
90 
92 template<typename T1>
93 inline uint160 Hash160(const T1& in1)
94 {
95  uint160 result;
96  CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
97  return result;
98 }
99 
102 {
103 private:
105 
106 public:
108  {
109  ctx.Write(UCharCast(src.data()), src.size());
110  }
111 
117  uint256 result;
118  ctx.Finalize(result.begin());
119  ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
120  return result;
121  }
122 
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>
142  HashWriter& operator<<(const T& obj)
143  {
144  ::Serialize(*this, obj);
145  return *this;
146  }
147 };
148 
149 class CHashWriter : public HashWriter
150 {
151 private:
152  const int nVersion;
153 
154 public:
155  CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}
156 
157  int GetVersion() const { return nVersion; }
158 
159  template<typename T>
160  CHashWriter& operator<<(const T& obj) {
161  ::Serialize(*this, obj);
162  return (*this);
163  }
164 };
165 
167 template <typename Source>
168 class HashVerifier : public HashWriter
169 {
170 private:
171  Source& m_source;
172 
173 public:
175 
177  {
178  m_source.read(dst);
179  this->write(dst);
180  }
181 
182  void ignore(size_t num_bytes)
183  {
184  std::byte data[1024];
185  while (num_bytes > 0) {
186  size_t now = std::min<size_t>(num_bytes, 1024);
187  read({data, now});
188  num_bytes -= now;
189  }
190  }
191 
192  template <typename T>
194  {
195  ::Unserialize(*this, obj);
196  return *this;
197  }
198 };
199 
201 template <typename Source>
203 {
204 private:
205  Source& m_source;
206 
207 public:
209 
211  {
212  m_source.write(src);
213  HashWriter::write(src);
214  }
215 
216  template <typename T>
218  {
219  ::Serialize(*this, obj);
220  return *this;
221  }
222 };
223 
225 [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
226 
227 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
228 
229 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
230 
237 HashWriter TaggedHash(const std::string& tag);
238 
241 {
242  uint160 result;
243  CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
244  return result;
245 }
246 
247 #endif // BITCOIN_HASH_H
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:116
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:694
void read(Span< std::byte > dst)
Definition: hash.h:176
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:75
const int nVersion
Definition: hash.h:152
void Finalize(Span< unsigned char > output)
Definition: hash.h:31
assert(!tx.IsCoinBase())
void Finalize(Span< unsigned char > output)
Definition: hash.h:56
void Unserialize(Stream &, char)=delete
uint256 ChainCode
Definition: hash.h:22
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:17
CHash160 & Reset()
Definition: hash.h:68
CSHA256 sha
Definition: hash.h:27
constexpr std::size_t size() const noexcept
Definition: span.h:186
CSHA256 ctx
Definition: hash.h:104
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:25
HashWriter & operator<<(const T &obj)
Definition: hash.h:142
uint160 RIPEMD160(Span< const unsigned char > data)
Compute the 160-bit RIPEMD-160 hash of an array.
Definition: hash.h:240
static const size_t OUTPUT_SIZE
Definition: hash.h:54
Source & m_source
Definition: hash.h:205
void ignore(size_t num_bytes)
Definition: hash.h:182
constexpr unsigned char * begin()
Definition: uint256.h:68
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:208
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:82
CSHA256 & Reset()
Definition: sha256.cpp:737
const char * source
Definition: rpcconsole.cpp:59
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:127
#define LIFETIMEBOUND
Definition: attributes.h:16
void write(Span< const std::byte > src)
Definition: hash.h:107
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
HashVerifier(Source &source LIFETIMEBOUND)
Definition: hash.h:174
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:720
void Serialize(Stream &, char)=delete
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:89
void write(Span< const std::byte > src)
Definition: hash.h:210
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:93
256-bit opaque blob.
Definition: uint256.h:106
constexpr C * data() const noexcept
Definition: span.h:173
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:217
HashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:193
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:136
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:281
160-bit opaque blob.
Definition: uint256.h:95
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
int GetVersion() const
Definition: hash.h:157
static const size_t OUTPUT_SIZE
Definition: hash.h:29
CHashWriter & operator<<(const T &obj)
Definition: hash.h:160
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:202
unsigned char * UCharCast(char *c)
Definition: span.h:270
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:168
A Span is an object that can refer to a contiguous sequence of objects.
Definition: solver.h:20
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:76
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:50
CSHA256 sha
Definition: hash.h:52
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
Source & m_source
Definition: hash.h:171
A hasher class for SHA-256.
Definition: sha256.h:13
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:63
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:38
CHash256 & Reset()
Definition: hash.h:43
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CHashWriter(int nVersionIn)
Definition: hash.h:155