Bitcoin ABC  0.22.13
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
26 public:
27  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
28 
29  void Finalize(uint8_t hash[OUTPUT_SIZE]) {
30  uint8_t buf[CSHA256::OUTPUT_SIZE];
31  sha.Finalize(buf);
32  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
33  }
34 
35  CHash256 &Write(const uint8_t *data, size_t len) {
36  sha.Write(data, len);
37  return *this;
38  }
39 
41  sha.Reset();
42  return *this;
43  }
44 };
45 
47 class CHash160 {
48 private:
50 
51 public:
52  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
53 
54  void Finalize(uint8_t hash[OUTPUT_SIZE]) {
55  uint8_t buf[CSHA256::OUTPUT_SIZE];
56  sha.Finalize(buf);
58  }
59 
60  CHash160 &Write(const uint8_t *data, size_t len) {
61  sha.Write(data, len);
62  return *this;
63  }
64 
66  sha.Reset();
67  return *this;
68  }
69 };
70 
72 template <typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend) {
73  static const uint8_t pblank[1] = {};
74  uint256 result;
75  CHash256()
76  .Write(pbegin == pend ? pblank : (const uint8_t *)&pbegin[0],
77  (pend - pbegin) * sizeof(pbegin[0]))
78  .Finalize((uint8_t *)&result);
79  return result;
80 }
81 
83 template <typename T1, typename T2>
84 inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin,
85  const T2 p2end) {
86  static const uint8_t pblank[1] = {};
87  uint256 result;
88  CHash256()
89  .Write(p1begin == p1end ? pblank : (const uint8_t *)&p1begin[0],
90  (p1end - p1begin) * sizeof(p1begin[0]))
91  .Write(p2begin == p2end ? pblank : (const uint8_t *)&p2begin[0],
92  (p2end - p2begin) * sizeof(p2begin[0]))
93  .Finalize((uint8_t *)&result);
94  return result;
95 }
96 
98 template <typename T1> inline uint160 Hash160(const T1 pbegin, const T1 pend) {
99  static uint8_t pblank[1] = {};
100  uint160 result;
101  CHash160()
102  .Write(pbegin == pend ? pblank : (const uint8_t *)&pbegin[0],
103  (pend - pbegin) * sizeof(pbegin[0]))
104  .Finalize((uint8_t *)&result);
105  return result;
106 }
107 
109 inline uint160 Hash160(const std::vector<uint8_t> &vch) {
110  return Hash160(vch.begin(), vch.end());
111 }
112 
114 template <unsigned int N>
116  return Hash160(vch.begin(), vch.end());
117 }
118 
120 class CHashWriter {
121 private:
123 
124  const int nType;
125  const int nVersion;
126 
127 public:
128  CHashWriter(int nTypeIn, int nVersionIn)
129  : nType(nTypeIn), nVersion(nVersionIn) {}
130 
131  int GetType() const { return nType; }
132  int GetVersion() const { return nVersion; }
133 
134  void write(const char *pch, size_t size) {
135  ctx.Write((const uint8_t *)pch, size);
136  }
137 
138  // invalidates the object
140  uint256 result;
141  ctx.Finalize((uint8_t *)&result);
142  return result;
143  }
144 
148  inline uint64_t GetCheapHash() {
149  uint8_t result[CHash256::OUTPUT_SIZE];
150  ctx.Finalize(result);
151  return ReadLE64(result);
152  }
153 
154  template <typename T> CHashWriter &operator<<(const T &obj) {
155  // Serialize to this stream
156  ::Serialize(*this, obj);
157  return (*this);
158  }
159 };
160 
164 template <typename Source> class CHashVerifier : public CHashWriter {
165 private:
166  Source *source;
167 
168 public:
169  explicit CHashVerifier(Source *source_)
170  : CHashWriter(source_->GetType(), source_->GetVersion()),
171  source(source_) {}
172 
173  void read(char *pch, size_t nSize) {
174  source->read(pch, nSize);
175  this->write(pch, nSize);
176  }
177 
178  void ignore(size_t nSize) {
179  char data[1024];
180  while (nSize > 0) {
181  size_t now = std::min<size_t>(nSize, 1024);
182  read(data, now);
183  nSize -= now;
184  }
185  }
186 
187  template <typename T> CHashVerifier<Source> &operator>>(T &&obj) {
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,
197  int nVersion = PROTOCOL_VERSION) {
198  CHashWriter ss(nType, nVersion);
199  ss << obj;
200  return ss.GetHash();
201 }
202 
203 uint32_t MurmurHash3(uint32_t nHashSeed,
204  const std::vector<uint8_t> &vDataToHash);
205 
206 void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
207  const uint8_t data[32], uint8_t output[64]);
208 
209 #endif // BITCOIN_HASH_H
void write(const char *pch, size_t size)
Definition: hash.h:134
const int nVersion
Definition: hash.h:125
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:187
uint256 ChainCode
Definition: hash.h:19
CHash160 & Write(const uint8_t *data, size_t len)
Definition: hash.h:60
CHash160 & Reset()
Definition: hash.h:65
CSHA256 sha
Definition: hash.h:24
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: hash.h:54
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:98
void ignore(size_t nSize)
Definition: hash.h:178
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:164
void Serialize(Stream &s, char a)
Definition: serialize.h:261
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
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:390
CSHA256 & Reset()
Definition: sha256.cpp:860
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:311
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:148
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
Definition: hash.cpp:72
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:72
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:128
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
CHash256 & Write(const uint8_t *data, size_t len)
Definition: hash.h:35
CHash256 ctx
Definition: hash.h:122
uint256 GetHash()
Definition: hash.h:139
256-bit opaque blob.
Definition: uint256.h:120
Source * source
Definition: hash.h:166
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: hash.h:29
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
const int nType
Definition: hash.h:124
uint32_t MurmurHash3(uint32_t nHashSeed, const std::vector< uint8_t > &vDataToHash)
Definition: hash.cpp:13
CHashVerifier(Source *source_)
Definition: hash.h:169
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
160-bit opaque blob.
Definition: uint256.h:108
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
int GetVersion() const
Definition: hash.h:132
static const size_t OUTPUT_SIZE
Definition: hash.h:27
CHashWriter & operator<<(const T &obj)
Definition: hash.h:154
void Unserialize(Stream &s, char &a)
Definition: serialize.h:313
iterator begin()
Definition: prevector.h:388
void read(char *pch, size_t nSize)
Definition: hash.h:173
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:120
int GetType() const
Definition: hash.h:131
A hasher class for Bitcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:47
CSHA256 sha
Definition: hash.h:49
A hasher class for SHA-256.
Definition: sha256.h:13
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
CHash256 & Reset()
Definition: hash.h:40
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12