1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "crypto/sha2.h"
10 #include "crypto/ripemd160.h"
11 #include "serialize.h"
17 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
22 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
24 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
25 unsigned char buf[sha.OUTPUT_SIZE];
27 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
30 CHash256& Write(const unsigned char *data, size_t len) {
41 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
46 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
48 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
49 unsigned char buf[sha.OUTPUT_SIZE];
51 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
54 CHash160& Write(const unsigned char *data, size_t len) {
65 /** Compute the 256-bit hash of an object. */
67 inline uint256 Hash(const T1 pbegin, const T1 pend)
69 static const unsigned char pblank[1] = {};
71 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
72 .Finalize((unsigned char*)&result);
76 /** Compute the 256-bit hash of the concatenation of two objects. */
77 template<typename T1, typename T2>
78 inline uint256 Hash(const T1 p1begin, const T1 p1end,
79 const T2 p2begin, const T2 p2end) {
80 static const unsigned char pblank[1] = {};
82 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
83 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
84 .Finalize((unsigned char*)&result);
88 /** Compute the 256-bit hash of the concatenation of three objects. */
89 template<typename T1, typename T2, typename T3>
90 inline uint256 Hash(const T1 p1begin, const T1 p1end,
91 const T2 p2begin, const T2 p2end,
92 const T3 p3begin, const T3 p3end) {
93 static const unsigned char pblank[1] = {};
95 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
96 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
97 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
98 .Finalize((unsigned char*)&result);
102 /** Compute the 160-bit hash an object. */
103 template<typename T1>
104 inline uint160 Hash160(const T1 pbegin, const T1 pend)
106 static unsigned char pblank[1] = {};
108 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
109 .Finalize((unsigned char*)&result);
113 /** Compute the 160-bit hash of a vector. */
114 inline uint160 Hash160(const std::vector<unsigned char>& vch)
116 return Hash160(vch.begin(), vch.end());
119 /** A writer stream (for serialization) that computes a 256-bit hash. */
129 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
131 CHashWriter& write(const char *pch, size_t size) {
132 ctx.Write((const unsigned char*)pch, size);
136 // invalidates the object
139 ctx.Finalize((unsigned char*)&result);
144 CHashWriter& operator<<(const T& obj) {
145 // Serialize to this stream
146 ::Serialize(*this, obj, nType, nVersion);
151 /** Compute the 256-bit hash of an object's serialization. */
153 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
155 CHashWriter ss(nType, nVersion);
160 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
162 #endif // BITCOIN_HASH_H