Bitcoin Core  0.20.99
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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  const int nType;
106  const int nVersion;
107 public:
108 
109  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
110 
111  int GetType() const { return nType; }
112  int GetVersion() const { return nVersion; }
113 
114  void write(const char *pch, size_t size) {
115  ctx.Write((const unsigned char*)pch, size);
116  }
117 
123  uint256 result;
124  ctx.Finalize(result.begin());
125  ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
126  return result;
127  }
128 
134  uint256 result;
135  ctx.Finalize(result.begin());
136  return result;
137  }
138 
142  inline uint64_t GetCheapHash() {
143  uint256 result = GetHash();
144  return ReadLE64(result.begin());
145  }
146 
147  template<typename T>
148  CHashWriter& operator<<(const T& obj) {
149  // Serialize to this stream
150  ::Serialize(*this, obj);
151  return (*this);
152  }
153 };
154 
156 template<typename Source>
158 {
159 private:
160  Source* source;
161 
162 public:
163  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
164 
165  void read(char* pch, size_t nSize)
166  {
167  source->read(pch, nSize);
168  this->write(pch, nSize);
169  }
170 
171  void ignore(size_t nSize)
172  {
173  char data[1024];
174  while (nSize > 0) {
175  size_t now = std::min<size_t>(nSize, 1024);
176  read(data, now);
177  nSize -= now;
178  }
179  }
180 
181  template<typename T>
183  {
184  // Unserialize from this stream
185  ::Unserialize(*this, obj);
186  return (*this);
187  }
188 };
189 
191 template<typename T>
192 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
193 {
194  CHashWriter ss(nType, nVersion);
195  ss << obj;
196  return ss.GetHash();
197 }
198 
200 NODISCARD uint256 SHA256Uint256(const uint256& input);
201 
202 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
203 
204 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
205 
212 CHashWriter TaggedHash(const std::string& tag);
213 
214 #endif // BITCOIN_HASH_H
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
void write(const char *pch, size_t size)
Definition: hash.h:114
CSHA256 ctx
Definition: hash.h:103
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:72
const std::chrono::seconds now
const int nVersion
Definition: hash.h:106
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:182
NODISCARD uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:82
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
uint256 ChainCode
Definition: hash.h:21
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:16
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:133
CHash160 & Reset()
Definition: hash.h:67
CSHA256 sha
Definition: hash.h:26
constexpr std::size_t size() const noexcept
Definition: span.h:153
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:24
unsigned char * begin()
Definition: uint256.h:58
void ignore(size_t nSize)
Definition: hash.h:171
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:157
void Serialize(Stream &s, char a)
Definition: serialize.h:227
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:192
CSHA256 & Reset()
Definition: sha256.cpp:680
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
Definition: span.h:220
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:142
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
#define NODISCARD
Definition: attributes.h:18
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:109
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:122
256-bit opaque blob.
Definition: uint256.h:124
Source * source
Definition: hash.h:160
constexpr C * data() const noexcept
Definition: span.h:140
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
const int nType
Definition: hash.h:105
CHashVerifier(Source *source_)
Definition: hash.h:163
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
160-bit opaque blob.
Definition: uint256.h:113
CHashWriter TaggedHash(const std::string &tag)
Return a CHashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:89
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
int GetVersion() const
Definition: hash.h:112
static const size_t OUTPUT_SIZE
Definition: hash.h:28
CHashWriter & operator<<(const T &obj)
Definition: hash.h:148
void Unserialize(Stream &s, char &a)
Definition: serialize.h:245
void read(char *pch, size_t nSize)
Definition: hash.h:165
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:82
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
int GetType() const
Definition: hash.h:111
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