Bitcoin Core  24.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-2020 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 <string>
19 #include <vector>
20 
22 
24 class CHash256 {
25 private:
27 public:
28  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29 
31  assert(output.size() == OUTPUT_SIZE);
32  unsigned char buf[CSHA256::OUTPUT_SIZE];
33  sha.Finalize(buf);
34  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
35  }
36 
38  sha.Write(input.data(), input.size());
39  return *this;
40  }
41 
43  sha.Reset();
44  return *this;
45  }
46 };
47 
49 class CHash160 {
50 private:
52 public:
53  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54 
56  assert(output.size() == OUTPUT_SIZE);
57  unsigned char 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 
74 template<typename T>
75 inline uint256 Hash(const T& in1)
76 {
77  uint256 result;
78  CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79  return result;
80 }
81 
83 template<typename T1, typename T2>
84 inline uint256 Hash(const T1& in1, const T2& in2) {
85  uint256 result;
87  return result;
88 }
89 
91 template<typename T1>
92 inline uint160 Hash160(const T1& in1)
93 {
94  uint160 result;
95  CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96  return result;
97 }
98 
101 {
102 private:
104 
105 public:
107  {
108  ctx.Write(UCharCast(src.data()), src.size());
109  }
110 
116  uint256 result;
117  ctx.Finalize(result.begin());
118  ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
119  return result;
120  }
121 
127  uint256 result;
128  ctx.Finalize(result.begin());
129  return result;
130  }
131 
135  inline uint64_t GetCheapHash() {
136  uint256 result = GetHash();
137  return ReadLE64(result.begin());
138  }
139 
140  template <typename T>
141  HashWriter& operator<<(const T& obj)
142  {
143  ::Serialize(*this, obj);
144  return *this;
145  }
146 };
147 
148 class CHashWriter : public HashWriter
149 {
150 private:
151  const int nType;
152  const int nVersion;
153 
154 public:
155  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
156 
157  int GetType() const { return nType; }
158  int GetVersion() const { return nVersion; }
159 
160  template<typename T>
161  CHashWriter& operator<<(const T& obj) {
162  // Serialize to this stream
163  ::Serialize(*this, obj);
164  return (*this);
165  }
166 };
167 
169 template<typename Source>
171 {
172 private:
173  Source* source;
174 
175 public:
176  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
177 
179  {
180  source->read(dst);
181  this->write(dst);
182  }
183 
184  void ignore(size_t nSize)
185  {
186  std::byte data[1024];
187  while (nSize > 0) {
188  size_t now = std::min<size_t>(nSize, 1024);
189  read({data, now});
190  nSize -= now;
191  }
192  }
193 
194  template<typename T>
196  {
197  // Unserialize from this stream
198  ::Unserialize(*this, obj);
199  return (*this);
200  }
201 };
202 
204 template <typename Source>
206 {
207 private:
208  Source& m_source;
209 
210 public:
211  explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : CHashWriter{source.GetType(), source.GetVersion()}, m_source{source} {}
212 
214  {
215  m_source.write(src);
216  CHashWriter::write(src);
217  }
218 
219  template <typename T>
221  {
222  ::Serialize(*this, obj);
223  return *this;
224  }
225 };
226 
228 template<typename T>
229 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
230 {
231  CHashWriter ss(nType, nVersion);
232  ss << obj;
233  return ss.GetHash();
234 }
235 
237 [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
238 
239 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
240 
241 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
242 
249 HashWriter TaggedHash(const std::string& tag);
250 
251 #endif // BITCOIN_HASH_H
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:115
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:681
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:30
assert(!tx.IsCoinBase())
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:195
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
void Unserialize(Stream &, char)=delete
uint256 ChainCode
Definition: hash.h:21
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:17
CHash160 & Reset()
Definition: hash.h:67
CSHA256 sha
Definition: hash.h:26
constexpr std::size_t size() const noexcept
Definition: span.h:186
CSHA256 ctx
Definition: hash.h:103
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:24
HashWriter & operator<<(const T &obj)
Definition: hash.h:141
static const size_t OUTPUT_SIZE
Definition: hash.h:53
unsigned char * begin()
Definition: uint256.h:61
void ignore(size_t nSize)
Definition: hash.h:184
void read(Span< std::byte > dst)
Definition: hash.h:178
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:170
Source & m_source
Definition: hash.h:208
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object&#39;s serialization.
Definition: hash.h:229
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:211
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:82
CSHA256 & Reset()
Definition: sha256.cpp:724
const char * source
Definition: rpcconsole.cpp:59
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:126
#define LIFETIMEBOUND
Definition: attributes.h:16
void write(Span< const std::byte > src)
Definition: hash.h:106
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:707
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
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:155
void write(Span< const std::byte > src)
Definition: hash.h:213
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:92
256-bit opaque blob.
Definition: uint256.h:119
Source * source
Definition: hash.h:173
constexpr C * data() const noexcept
Definition: span.h:173
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:220
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
const int nType
Definition: hash.h:151
CHashVerifier(Source *source_)
Definition: hash.h:176
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:135
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
160-bit opaque blob.
Definition: uint256.h:108
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
int GetVersion() const
Definition: hash.h:158
static const size_t OUTPUT_SIZE
Definition: hash.h:28
CHashWriter & operator<<(const T &obj)
Definition: hash.h:161
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:205
unsigned char * UCharCast(char *c)
Definition: span.h:275
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
int GetType() const
Definition: hash.h:157
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
CSHA256 sha
Definition: hash.h:51
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
A hasher class for SHA-256.
Definition: sha256.h:13
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:62
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
CHash256 & Reset()
Definition: hash.h:42
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12