Bitcoin Core  0.19.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 <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 <vector>
18 
20 
22 class CHash256 {
23 private:
25 public:
26  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
27 
28  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
29  unsigned char buf[CSHA256::OUTPUT_SIZE];
30  sha.Finalize(buf);
31  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
32  }
33 
34  CHash256& Write(const unsigned char *data, size_t len) {
35  sha.Write(data, len);
36  return *this;
37  }
38 
40  sha.Reset();
41  return *this;
42  }
43 };
44 
46 class CHash160 {
47 private:
49 public:
50  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
51 
52  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
53  unsigned char buf[CSHA256::OUTPUT_SIZE];
54  sha.Finalize(buf);
56  }
57 
58  CHash160& Write(const unsigned char *data, size_t len) {
59  sha.Write(data, len);
60  return *this;
61  }
62 
64  sha.Reset();
65  return *this;
66  }
67 };
68 
70 template<typename T1>
71 inline uint256 Hash(const T1 pbegin, const T1 pend)
72 {
73  static const unsigned char pblank[1] = {};
74  uint256 result;
75  CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
76  .Finalize((unsigned char*)&result);
77  return result;
78 }
79 
81 template<typename T1, typename T2>
82 inline uint256 Hash(const T1 p1begin, const T1 p1end,
83  const T2 p2begin, const T2 p2end) {
84  static const unsigned char pblank[1] = {};
85  uint256 result;
86  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
87  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
88  .Finalize((unsigned char*)&result);
89  return result;
90 }
91 
93 template<typename T1>
94 inline uint160 Hash160(const T1 pbegin, const T1 pend)
95 {
96  static unsigned char pblank[1] = {};
97  uint160 result;
98  CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
99  .Finalize((unsigned char*)&result);
100  return result;
101 }
102 
104 inline uint160 Hash160(const std::vector<unsigned char>& vch)
105 {
106  return Hash160(vch.begin(), vch.end());
107 }
108 
110 template<unsigned int N>
112 {
113  return Hash160(vch.begin(), vch.end());
114 }
115 
118 {
119 private:
121 
122  const int nType;
123  const int nVersion;
124 public:
125 
126  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
127 
128  int GetType() const { return nType; }
129  int GetVersion() const { return nVersion; }
130 
131  void write(const char *pch, size_t size) {
132  ctx.Write((const unsigned char*)pch, size);
133  }
134 
135  // invalidates the object
137  uint256 result;
138  ctx.Finalize((unsigned char*)&result);
139  return result;
140  }
141 
145  inline uint64_t GetCheapHash() {
146  unsigned char result[CHash256::OUTPUT_SIZE];
147  ctx.Finalize(result);
148  return ReadLE64(result);
149  }
150 
151  template<typename T>
152  CHashWriter& operator<<(const T& obj) {
153  // Serialize to this stream
154  ::Serialize(*this, obj);
155  return (*this);
156  }
157 };
158 
160 template<typename Source>
162 {
163 private:
164  Source* source;
165 
166 public:
167  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
168 
169  void read(char* pch, size_t nSize)
170  {
171  source->read(pch, nSize);
172  this->write(pch, nSize);
173  }
174 
175  void ignore(size_t nSize)
176  {
177  char data[1024];
178  while (nSize > 0) {
179  size_t now = std::min<size_t>(nSize, 1024);
180  read(data, now);
181  nSize -= now;
182  }
183  }
184 
185  template<typename T>
187  {
188  // Unserialize from this stream
189  ::Unserialize(*this, obj);
190  return (*this);
191  }
192 };
193 
195 template<typename T>
196 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
197 {
198  CHashWriter ss(nType, nVersion);
199  ss << obj;
200  return ss.GetHash();
201 }
202 
203 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
204 
205 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
206 
207 #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:131
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:15
const int nVersion
Definition: hash.h:123
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:186
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:34
uint256 ChainCode
Definition: hash.h:19
CHash160 & Reset()
Definition: hash.h:63
CSHA256 sha
Definition: hash.h:24
A hasher class for Bitcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:22
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:94
void ignore(size_t nSize)
Definition: hash.h:175
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:161
void Serialize(Stream &s, char a)
Definition: serialize.h:203
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:28
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:196
iterator end()
Definition: prevector.h:287
CSHA256 & Reset()
Definition: sha256.cpp:680
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:145
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
CHash160 & Write(const unsigned char *data, size_t len)
Definition: hash.h:58
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:71
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:126
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
CHash256 ctx
Definition: hash.h:120
uint256 GetHash()
Definition: hash.h:136
256-bit opaque blob.
Definition: uint256.h:120
Source * source
Definition: hash.h:164
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
const int nType
Definition: hash.h:122
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:52
CHashVerifier(Source *source_)
Definition: hash.h:167
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
160-bit opaque blob.
Definition: uint256.h:109
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
int GetVersion() const
Definition: hash.h:129
static const size_t OUTPUT_SIZE
Definition: hash.h:26
CHashWriter & operator<<(const T &obj)
Definition: hash.h:152
void Unserialize(Stream &s, char &a)
Definition: serialize.h:221
iterator begin()
Definition: prevector.h:285
void read(char *pch, size_t nSize)
Definition: hash.h:169
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:117
int GetType() const
Definition: hash.h:128
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:46
CSHA256 sha
Definition: hash.h:48
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
A hasher class for SHA-256.
Definition: sha256.h:13
CHash256 & Reset()
Definition: hash.h:39
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12