Add UpdatedBlockTip signal to CMainSignals and CValidationInterface
[bitcoinplatinum.git] / src / hash.h
blob0771555623975cceaedf9eb4518f6b5e4a5e6833
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
6 #ifndef BITCOIN_HASH_H
7 #define BITCOIN_HASH_H
9 #include "crypto/ripemd160.h"
10 #include "crypto/sha256.h"
11 #include "serialize.h"
12 #include "uint256.h"
13 #include "version.h"
15 #include <vector>
17 typedef uint256 ChainCode;
19 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
20 class CHash256 {
21 private:
22 CSHA256 sha;
23 public:
24 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
26 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
27 unsigned char buf[sha.OUTPUT_SIZE];
28 sha.Finalize(buf);
29 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
32 CHash256& Write(const unsigned char *data, size_t len) {
33 sha.Write(data, len);
34 return *this;
37 CHash256& Reset() {
38 sha.Reset();
39 return *this;
43 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
44 class CHash160 {
45 private:
46 CSHA256 sha;
47 public:
48 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
50 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
51 unsigned char buf[sha.OUTPUT_SIZE];
52 sha.Finalize(buf);
53 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
56 CHash160& Write(const unsigned char *data, size_t len) {
57 sha.Write(data, len);
58 return *this;
61 CHash160& Reset() {
62 sha.Reset();
63 return *this;
67 /** Compute the 256-bit hash of an object. */
68 template<typename T1>
69 inline uint256 Hash(const T1 pbegin, const T1 pend)
71 static const unsigned char pblank[1] = {};
72 uint256 result;
73 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
74 .Finalize((unsigned char*)&result);
75 return result;
78 /** Compute the 256-bit hash of the concatenation of two objects. */
79 template<typename T1, typename T2>
80 inline uint256 Hash(const T1 p1begin, const T1 p1end,
81 const T2 p2begin, const T2 p2end) {
82 static const unsigned char pblank[1] = {};
83 uint256 result;
84 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
85 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
86 .Finalize((unsigned char*)&result);
87 return result;
90 /** Compute the 256-bit hash of the concatenation of three objects. */
91 template<typename T1, typename T2, typename T3>
92 inline uint256 Hash(const T1 p1begin, const T1 p1end,
93 const T2 p2begin, const T2 p2end,
94 const T3 p3begin, const T3 p3end) {
95 static const unsigned char pblank[1] = {};
96 uint256 result;
97 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
98 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
99 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
100 .Finalize((unsigned char*)&result);
101 return result;
104 /** Compute the 160-bit hash an object. */
105 template<typename T1>
106 inline uint160 Hash160(const T1 pbegin, const T1 pend)
108 static unsigned char pblank[1] = {};
109 uint160 result;
110 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
111 .Finalize((unsigned char*)&result);
112 return result;
115 /** Compute the 160-bit hash of a vector. */
116 inline uint160 Hash160(const std::vector<unsigned char>& vch)
118 return Hash160(vch.begin(), vch.end());
121 /** A writer stream (for serialization) that computes a 256-bit hash. */
122 class CHashWriter
124 private:
125 CHash256 ctx;
127 public:
128 int nType;
129 int nVersion;
131 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
133 CHashWriter& write(const char *pch, size_t size) {
134 ctx.Write((const unsigned char*)pch, size);
135 return (*this);
138 // invalidates the object
139 uint256 GetHash() {
140 uint256 result;
141 ctx.Finalize((unsigned char*)&result);
142 return result;
145 template<typename T>
146 CHashWriter& operator<<(const T& obj) {
147 // Serialize to this stream
148 ::Serialize(*this, obj, nType, nVersion);
149 return (*this);
153 /** Compute the 256-bit hash of an object's serialization. */
154 template<typename T>
155 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
157 CHashWriter ss(nType, nVersion);
158 ss << obj;
159 return ss.GetHash();
162 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
164 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
166 #endif // BITCOIN_HASH_H