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.
9 #include "crypto/ripemd160.h"
10 #include "crypto/sha256.h"
11 #include "crypto/verus_hash.h"
12 #include "serialize.h"
20 typedef uint256 ChainCode;
22 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
27 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
30 unsigned char buf[sha.OUTPUT_SIZE];
32 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
35 CHash256& Write(const unsigned char *data, size_t len) {
46 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
51 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
53 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
54 unsigned char buf[sha.OUTPUT_SIZE];
56 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
59 CHash160& Write(const unsigned char *data, size_t len) {
70 /** Compute the 256-bit hash of an object. */
72 inline uint256 Hash(const T1 pbegin, const T1 pend)
74 static const unsigned char pblank[1] = {};
76 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
77 .Finalize((unsigned char*)&result);
81 /** Compute the 256-bit hash of the concatenation of two objects. */
82 template<typename T1, typename T2>
83 inline uint256 Hash(const T1 p1begin, const T1 p1end,
84 const T2 p2begin, const T2 p2end) {
85 static const unsigned char pblank[1] = {};
87 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
88 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
89 .Finalize((unsigned char*)&result);
93 /** Compute the 256-bit hash of the concatenation of three objects. */
94 template<typename T1, typename T2, typename T3>
95 inline uint256 Hash(const T1 p1begin, const T1 p1end,
96 const T2 p2begin, const T2 p2end,
97 const T3 p3begin, const T3 p3end) {
98 static const unsigned char pblank[1] = {};
100 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
101 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
102 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
103 .Finalize((unsigned char*)&result);
107 /** Compute the 160-bit hash an object. */
108 template<typename T1>
109 inline uint160 Hash160(const T1 pbegin, const T1 pend)
111 static unsigned char pblank[1] = {};
113 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
114 .Finalize((unsigned char*)&result);
118 /** Compute the 160-bit hash of a vector. */
119 inline uint160 Hash160(const std::vector<unsigned char>& vch)
121 return Hash160(vch.begin(), vch.end());
124 /** A writer stream (for serialization) that computes a 256-bit hash. */
134 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
136 CHashWriter& write(const char *pch, size_t size) {
137 ctx.Write((const unsigned char*)pch, size);
141 // invalidates the object
144 ctx.Finalize((unsigned char*)&result);
149 CHashWriter& operator<<(const T& obj) {
150 // Serialize to this stream
151 ::Serialize(*this, obj, nType, nVersion);
157 /** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */
161 crypto_generichash_blake2b_state state;
167 CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) {
168 assert(crypto_generichash_blake2b_init_salt_personal(
176 CBLAKE2bWriter& write(const char *pch, size_t size) {
177 crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size);
181 // invalidates the object
184 crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32);
189 CBLAKE2bWriter& operator<<(const T& obj) {
190 // Serialize to this stream
191 ::Serialize(*this, obj, nType, nVersion);
196 /** A writer stream (for serialization) that computes a 256-bit Verus hash. */
197 class CVerusHashWriter
206 CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() {}
208 CVerusHashWriter& write(const char *pch, size_t size) {
209 state.Write((const unsigned char*)pch, size);
213 // invalidates the object for further writing
216 state.Finalize((unsigned char*)&result);
221 CVerusHashWriter& operator<<(const T& obj) {
222 // Serialize to this stream
223 ::Serialize(*this, obj, nType, nVersion);
228 /** An optimized and dangerous writer stream (for serialization) that computes a 256-bit Verus hash without the normal
229 * safety checks. Do not try to write more than 1488 bytes to this hash writer. */
230 class CVerusMiningHashWriter
234 unsigned char charBuf[1488];
238 memset(charBuf, 0, sizeof(charBuf));
246 CVerusMiningHashWriter(int nTypeIn, int nVersionIn, int pos = 0) : buf()
250 nVersion = nVersionIn;
253 CVerusMiningHashWriter& write(const char *pch, size_t size) {
254 if ((nPos + size) <= sizeof(buf.charBuf))
256 memcpy(&(buf.charBuf[nPos]), pch, size);
262 // does not invalidate the object for modification and further hashing
265 CVerusHash::Hash((unsigned char*)&result, buf.charBuf, nPos);
270 CVerusMiningHashWriter& operator<<(const T& obj) {
271 // Serialize to this stream
272 ::Serialize(*this, obj, nType, nVersion);
277 /** Compute the 256-bit hash of an object's serialization. */
279 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
281 CHashWriter ss(nType, nVersion);
286 /** Compute the 256-bit Verus hash of an object's serialization. */
288 uint256 SerializeVerusHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
290 CVerusHashWriter ss(nType, nVersion);
295 /** Compute the 256-bit Verus hash of an object's serialization. */
297 uint256 SerializeVerusMiningHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
299 CVerusMiningHashWriter ss = CVerusMiningHashWriter(nType, nVersion);
304 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
306 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
308 #endif // BITCOIN_HASH_H