Fabcoin Core  0.16.2
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_HASH_H
7 #define FABCOIN_HASH_H
8 
9 #include <crypto/ripemd160.h>
10 #include <crypto/sha256.h>
11 #include <prevector.h>
12 #include <serialize.h>
13 #include <uint256.h>
14 #include <version.h>
15 
16 #include <vector>
17 
19 
21 class CHash256 {
22 private:
24 public:
25  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
26 
27  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
28  unsigned char buf[CSHA256::OUTPUT_SIZE];
29  sha.Finalize(buf);
30  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(hash);
31  }
32 
33  CHash256& Write(const unsigned char *data, size_t len) {
34  sha.Write(data, len);
35  return *this;
36  }
37 
39  sha.Reset();
40  return *this;
41  }
42 };
43 
45 class CHash160 {
46 private:
48 public:
49  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
50 
51  void Finalize(unsigned char hash[OUTPUT_SIZE]) {
52  unsigned char buf[CSHA256::OUTPUT_SIZE];
53  sha.Finalize(buf);
55  }
56 
57  CHash160& Write(const unsigned char *data, size_t len) {
58  sha.Write(data, len);
59  return *this;
60  }
61 
63  sha.Reset();
64  return *this;
65  }
66 };
67 
69 template<typename T1>
70 inline uint256 Hash(const T1 pbegin, const T1 pend)
71 {
72  static const unsigned char pblank[1] = {};
73  uint256 result;
74  CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
75  .Finalize((unsigned char*)&result);
76  return result;
77 }
78 
80 template<typename T1, typename T2>
81 inline uint256 Hash(const T1 p1begin, const T1 p1end,
82  const T2 p2begin, const T2 p2end) {
83  static const unsigned char pblank[1] = {};
84  uint256 result;
85  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
86  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
87  .Finalize((unsigned char*)&result);
88  return result;
89 }
90 
92 template<typename T1, typename T2, typename T3>
93 inline uint256 Hash(const T1 p1begin, const T1 p1end,
94  const T2 p2begin, const T2 p2end,
95  const T3 p3begin, const T3 p3end) {
96  static const unsigned char pblank[1] = {};
97  uint256 result;
98  CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
99  .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
100  .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
101  .Finalize((unsigned char*)&result);
102  return result;
103 }
104 
106 template<typename T1>
107 inline uint160 Hash160(const T1 pbegin, const T1 pend)
108 {
109  static unsigned char pblank[1] = {};
110  uint160 result;
111  CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
112  .Finalize((unsigned char*)&result);
113  return result;
114 }
115 
117 inline uint160 Hash160(const std::vector<unsigned char>& vch)
118 {
119  return Hash160(vch.begin(), vch.end());
120 }
121 
123 template<unsigned int N>
125 {
126  return Hash160(vch.begin(), vch.end());
127 }
128 
131 {
132 private:
134 
135  const int nType;
136  const int nVersion;
137 public:
138 
139  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
140 
141  int GetType() const { return nType; }
142  int GetVersion() const { return nVersion; }
143 
144  void write(const char *pch, size_t size) {
145  ctx.Write((const unsigned char*)pch, size);
146  }
147 
148  // invalidates the object
150  uint256 result;
151  ctx.Finalize((unsigned char*)&result);
152  return result;
153  }
154 
155  template<typename T>
156  CHashWriter& operator<<(const T& obj) {
157  // Serialize to this stream
158  ::Serialize(*this, obj);
159  return (*this);
160  }
161 };
162 
164 template<typename Source>
166 {
167 private:
169 
170 public:
171  CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
172 
173  void read(char* pch, size_t nSize)
174  {
175  source->read(pch, nSize);
176  this->write(pch, nSize);
177  }
178 
179  void ignore(size_t nSize)
180  {
181  char data[1024];
182  while (nSize > 0) {
183  size_t now = std::min<size_t>(nSize, 1024);
184  read(data, now);
185  nSize -= now;
186  }
187  }
188 
189  template<typename T>
191  {
192  // Unserialize from this stream
193  ::Unserialize(*this, obj);
194  return (*this);
195  }
196 };
197 
199 template<typename T>
200 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
201 {
202  CHashWriter ss(nType, nVersion);
203  ss << obj;
204  return ss.GetHash();
205 }
206 
207 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
208 
209 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
210 
213 {
214 private:
215  uint64_t v[4];
216  uint64_t tmp;
217  int count;
218 
219 public:
221  CSipHasher(uint64_t k0, uint64_t k1);
226  CSipHasher& Write(uint64_t data);
228  CSipHasher& Write(const unsigned char* data, size_t size);
230  uint64_t Finalize() const;
231 };
232 
243 uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
244 uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);
245 
246 #endif // FABCOIN_HASH_H
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:201
#define T1
Definition: integer.cpp:2170
void write(const char *pch, size_t size)
Definition: hash.h:144
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:72
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:16
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:1239
const int nVersion
Definition: hash.h:136
#define T2
Definition: integer.cpp:2171
#define T(i, x)
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:33
uint256 ChainCode
Definition: hash.h:18
CHash160 & Reset()
Definition: hash.h:62
CSHA256 sha
Definition: hash.h:23
A hasher class for Fabcoin&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:21
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:107
bool read(const std::string &s, Value &value)
void ignore(size_t nSize)
Definition: hash.h:179
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:165
uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val)
Optimized SipHash-2-4 implementation for uint256.
Definition: hash.cpp:169
void Serialize(Stream &s, char a)
Definition: serialize.h:198
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:27
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:200
iterator end()
Definition: prevector.h:292
CSHA256 & Reset()
Definition: sha256.cpp:244
int count
Definition: hash.h:217
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:227
void write(const Value &value, std::ostream &os)
CHash160 & Write(const unsigned char *data, size_t len)
Definition: hash.h:57
#define k0
Definition: ripemd.cpp:18
CHashVerifier< Source > & operator>>(T &obj)
Definition: hash.h:190
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:70
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:139
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:36
uint64_t tmp
Definition: hash.h:216
int GetType() const
Definition: hash.h:141
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
CHash256 ctx
Definition: hash.h:133
uint256 GetHash()
Definition: hash.h:149
256-bit opaque blob.
Definition: uint256.h:132
Source * source
Definition: hash.h:168
uint8_t const size_t const size
Definition: sha3.h:20
int GetVersion() const
Definition: hash.h:142
const int nType
Definition: hash.h:135
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:51
CHashVerifier(Source *source_)
Definition: hash.h:171
#define k1
Definition: ripemd.cpp:19
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, uint32_t extra)
Definition: hash.cpp:209
160-bit opaque blob.
Definition: uint256.h:120
SipHash-2-4.
Definition: hash.h:212
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
static const size_t OUTPUT_SIZE
Definition: hash.h:25
CHashWriter & operator<<(const T &obj)
Definition: hash.h:156
void Unserialize(Stream &s, char &a)
Definition: serialize.h:210
iterator begin()
Definition: prevector.h:290
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:130
A hasher class for Fabcoin&#39;s 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:45
CSHA256 sha
Definition: hash.h:47
#define T3
Definition: integer.cpp:2172
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
A hasher class for SHA-256.
Definition: sha256.h:13
uint8_t const * data
Definition: sha3.h:19
CHash256 & Reset()
Definition: hash.h:38
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12