hdac SDK
SDK for hdac blockchain development
hash.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Original code was distributed under the MIT software license.
4 // Copyright (c) 2014-2017 Coin Sciences Ltd
5 // MultiChain code distributed under the GPLv3 license, see COPYING file.
6 
7 #ifndef BITCOIN_HASH_H
8 #define BITCOIN_HASH_H
9 
10 #include <crypto/ripemd160.h>
11 #include <crypto/sha256.h>
12 #include <utils/serialize.h>
13 #include "uint256.h"
14 //#include "version/bcversion.h"
15 
16 #include <vector>
17 
18 typedef uint256 ChainCode;
19 
21 class CHash256 {
22 private:
23  CSHA256 sha;
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[sha.OUTPUT_SIZE];
29  sha.Finalize(buf);
30  sha.Reset().Write(buf, sha.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 
38  CHash256& Reset() {
39  sha.Reset();
40  return *this;
41  }
42 };
43 
45 class CHash160 {
46 private:
47  CSHA256 sha;
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[sha.OUTPUT_SIZE];
53  sha.Finalize(buf);
54  CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
55  }
56 
57  CHash160& Write(const unsigned char *data, size_t len) {
58  sha.Write(data, len);
59  return *this;
60  }
61 
62  CHash160& Reset() {
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 
124 {
125 private:
126  CHash256 ctx;
127 
128 public:
129  int nType;
130  int nVersion;
131 
132  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
133 
134  CHashWriter& write(const char *pch, size_t size) {
135  ctx.Write((const unsigned char*)pch, size);
136  return (*this);
137  }
138 
139  // invalidates the object
140  uint256 GetHash() {
141  uint256 result;
142  ctx.Finalize((unsigned char*)&result);
143  return result;
144  }
145 
146  template<typename T>
147  CHashWriter& operator<<(const T& obj) {
148  // Serialize to this stream
149  ::Serialize(*this, obj, nType, nVersion);
150  return (*this);
151  }
152 };
153 
155 template<typename T>
156 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
157 {
158  CHashWriter ss(nType, nVersion);
159  ss << obj;
160  return ss.GetHash();
161 }
162 
163 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
164 
165 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
166 
167 #endif // BITCOIN_HASH_H
Definition: hash.h:21
Definition: uint256.h:294
Definition: uint256.h:284
Definition: hash.h:123
Definition: hash.h:45
Definition: sha256.h:14
Definition: ripemd160.h:14