Bitcoin Core  24.99.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 <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 <string>
18 #include <vector>
19 
21 
23 class CHash256 {
24 private:
26 public:
27  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
28 
30  assert(output.size() == OUTPUT_SIZE);
31  unsigned char buf[CSHA256::OUTPUT_SIZE];
32  sha.Finalize(buf);
33  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
34  }
35 
37  sha.Write(input.data(), input.size());
38  return *this;
39  }
40 
42  sha.Reset();
43  return *this;
44  }
45 };
46 
48 class CHash160 {
49 private:
51 public:
52  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
53 
55  assert(output.size() == OUTPUT_SIZE);
56  unsigned char buf[CSHA256::OUTPUT_SIZE];
57  sha.Finalize(buf);
59  }
60 
62  sha.Write(input.data(), input.size());
63  return *this;
64  }
65 
67  sha.Reset();
68  return *this;
69  }
70 };
71 
73 template<typename T>
74 inline uint256 Hash(const T& in1)
75 {
76  uint256 result;
77  CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
78  return result;
79 }
80 
82 template<typename T1, typename T2>
83 inline uint256 Hash(const T1& in1, const T2& in2) {
84  uint256 result;
86  return result;
87 }
88 
90 template<typename T1>
91 inline uint160 Hash160(const T1& in1)
92 {
93  uint160 result;
94  CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
95  return result;
96 }
97 
100 {
101 private:
103 
104 public:
106  {
107  ctx.Write(UCharCast(src.data()), src.size());
108  }
109 
115  uint256 result;
116  ctx.Finalize(result.begin());
117  ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
118  return result;
119  }
120 
126  uint256 result;
127  ctx.Finalize(result.begin());
128  return result;
129  }
130 
134  inline uint64_t GetCheapHash() {
135  uint256 result = GetHash();
136  return ReadLE64(result.begin());
137  }
138 
139  template <typename T>
140  HashWriter& operator<<(const T& obj)
141  {
142  ::Serialize(*this, obj);
143  return *this;
144  }
145 };
146 
147 class CHashWriter : public HashWriter
148 {
149 private:
150  const int nType;
151  const int nVersion;
152 
153 public:
154  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
155 
156  int GetType() const { return nType; }
157  int GetVersion() const { return nVersion; }
158 
159  template<typename T>
160  CHashWriter& operator<<(const T& obj) {
161  // Serialize to this stream
162  ::Serialize(*this, obj);
163  return (*this);
164  }
165 };
166 
168 template<typename Source>
170 {
171 private:
172  Source* source;
173 
174 public:
175  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
176 
178  {
179  source->read(dst);
180  this->write(dst);
181  }
182 
183  void ignore(size_t nSize)
184  {
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>
195  {
196  // Unserialize from this stream
197  ::Unserialize(*this, obj);
198  return (*this);
199  }
200 };
201 
203 template<typename T>
204 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
205 {
206  CHashWriter ss(nType, nVersion);
207  ss << obj;
208  return ss.GetHash();
209 }
210 
212 [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
213 
214 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
215 
216 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
217 
224 HashWriter TaggedHash(const std::string& tag);
225 
226 #endif // BITCOIN_HASH_H
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:48
static const size_t OUTPUT_SIZE
Definition: hash.h:52
void Finalize(Span< unsigned char > output)
Definition: hash.h:54
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:61
CSHA256 sha
Definition: hash.h:50
CHash160 & Reset()
Definition: hash.h:66
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:23
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:36
void Finalize(Span< unsigned char > 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: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
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:194
void ignore(size_t nSize)
Definition: hash.h:183
void read(Span< std::byte > dst)
Definition: hash.h:177
int GetType() const
Definition: hash.h:156
const int nType
Definition: hash.h:150
CHashWriter & operator<<(const T &obj)
Definition: hash.h:160
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:154
int GetVersion() const
Definition: hash.h:157
const int nVersion
Definition: hash.h:151
A hasher class for RIPEMD-160.
Definition: ripemd160.h:13
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
A hasher class for SHA-256.
Definition: sha256.h:14
CSHA256 & Reset()
Definition: sha256.cpp:724
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:707
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:681
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:140
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:134
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:125
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
constexpr std::size_t size() const noexcept
Definition: span.h:186
constexpr C * data() const noexcept
Definition: span.h:173
unsigned char * begin()
Definition: uint256.h:61
160-bit opaque blob.
Definition: uint256.h:108
256-bit opaque blob.
Definition: uint256.h:119
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:91
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:75
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:17
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:89
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:204
uint256 ChainCode
Definition: hash.h:20
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:82
@ SER_GETHASH
Definition: serialize.h:133
void Unserialize(Stream &, char)=delete
void Serialize(Stream &, char)=delete
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
unsigned char * UCharCast(char *c)
Definition: span.h:275
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12