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 "prevector.h"
13 #include "serialize.h"
21 typedef uint256 ChainCode;
23 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
28 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
30 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
31 unsigned char buf[sha.OUTPUT_SIZE];
33 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
36 CHash256& Write(const unsigned char *data, size_t len) {
47 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
52 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
55 unsigned char buf[sha.OUTPUT_SIZE];
57 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
60 CHash160& Write(const unsigned char *data, size_t len) {
71 /** Compute the 256-bit hash of an object. */
73 inline uint256 Hash(const T1 pbegin, const T1 pend)
75 static const unsigned char pblank[1] = {};
77 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
78 .Finalize((unsigned char*)&result);
82 /** Compute the 256-bit hash of the concatenation of two objects. */
83 template<typename T1, typename T2>
84 inline uint256 Hash(const T1 p1begin, const T1 p1end,
85 const T2 p2begin, const T2 p2end) {
86 static const unsigned char pblank[1] = {};
88 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
89 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
90 .Finalize((unsigned char*)&result);
94 /** Compute the 256-bit hash of the concatenation of three objects. */
95 template<typename T1, typename T2, typename T3>
96 inline uint256 Hash(const T1 p1begin, const T1 p1end,
97 const T2 p2begin, const T2 p2end,
98 const T3 p3begin, const T3 p3end) {
99 static const unsigned char pblank[1] = {};
101 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
102 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
103 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
104 .Finalize((unsigned char*)&result);
108 /** Compute the 160-bit hash an object. */
109 template<typename T1>
110 inline uint160 Hash160(const T1 pbegin, const T1 pend)
112 static unsigned char pblank[1] = {};
114 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
115 .Finalize((unsigned char*)&result);
119 /** Compute the 160-bit hash of a vector. */
120 inline uint160 Hash160(const std::vector<unsigned char>& vch)
122 return Hash160(vch.begin(), vch.end());
125 /** Compute the 160-bit hash of a vector. */
126 template<unsigned int N>
127 inline uint160 Hash160(const prevector<N, unsigned char>& vch)
129 return Hash160(vch.begin(), vch.end());
132 /** A writer stream (for serialization) that computes a 256-bit hash. */
142 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
144 int GetType() const { return nType; }
145 int GetVersion() const { return nVersion; }
147 void write(const char *pch, size_t size) {
148 ctx.Write((const unsigned char*)pch, size);
151 // invalidates the object
154 ctx.Finalize((unsigned char*)&result);
159 CHashWriter& operator<<(const T& obj) {
160 // Serialize to this stream
161 ::Serialize(*this, obj);
167 /** A writer stream (for serialization) that computes a 256-bit BLAKE2b hash. */
171 crypto_generichash_blake2b_state state;
177 CBLAKE2bWriter(int nTypeIn, int nVersionIn, const unsigned char* personal) : nType(nTypeIn), nVersion(nVersionIn) {
178 assert(crypto_generichash_blake2b_init_salt_personal(
186 int GetType() const { return nType; }
187 int GetVersion() const { return nVersion; }
189 CBLAKE2bWriter& write(const char *pch, size_t size) {
190 crypto_generichash_blake2b_update(&state, (const unsigned char*)pch, size);
194 // invalidates the object
197 crypto_generichash_blake2b_final(&state, (unsigned char*)&result, 32);
202 CBLAKE2bWriter& operator<<(const T& obj) {
203 // Serialize to this stream
204 ::Serialize(*this, obj);
209 /** A writer stream (for serialization) that computes a 256-bit Verus hash. */
210 class CVerusHashWriter
219 CVerusHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() { }
220 void Reset() { state.Reset(); }
222 CVerusHashWriter& write(const char *pch, size_t size) {
223 state.Write((const unsigned char*)pch, size);
227 // invalidates the object for further writing
230 state.Finalize((unsigned char*)&result);
234 int64_t *xI64p() { return state.ExtraI64Ptr(); }
235 CVerusHash &GetState() { return state; }
238 CVerusHashWriter& operator<<(const T& obj) {
239 // Serialize to this stream
240 ::Serialize(*this, obj);
245 /** A writer stream (for serialization) that computes a 256-bit Verus hash with key initialized to Haraka standard. */
246 class CVerusHashV2Writer
255 CVerusHashV2Writer(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn), state() {}
256 void Reset() { state.Reset(); }
258 CVerusHashV2Writer& write(const char *pch, size_t size) {
259 state.Write((const unsigned char*)pch, size);
263 // invalidates the object for further writing
266 state.Finalize((unsigned char*)&result);
270 int64_t *xI64p() { return state.ExtraI64Ptr(); }
271 CVerusHashV2 &GetState() { return state; }
274 CVerusHashV2Writer& operator<<(const T& obj) {
275 // Serialize to this stream
276 ::Serialize(*this, obj);
281 /** Compute the 256-bit hash of an object's serialization. */
283 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
285 CHashWriter ss(nType, nVersion);
290 /** Compute the 256-bit Verus hash of an object's serialization. */
292 uint256 SerializeVerusHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
294 CVerusHashWriter ss(nType, nVersion);
299 /** Compute the 256-bit Verus hash of an object's serialization. */
301 uint256 SerializeVerusHashV2(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
303 CVerusHashV2Writer ss(nType, nVersion);
308 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
310 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
312 #endif // BITCOIN_HASH_H