1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 "prevector.h"
12 #include "serialize.h"
18 typedef uint256 ChainCode
;
20 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
25 static const size_t OUTPUT_SIZE
= CSHA256::OUTPUT_SIZE
;
27 void Finalize(unsigned char hash
[OUTPUT_SIZE
]) {
28 unsigned char buf
[CSHA256::OUTPUT_SIZE
];
30 sha
.Reset().Write(buf
, CSHA256::OUTPUT_SIZE
).Finalize(hash
);
33 CHash256
& Write(const unsigned char *data
, size_t len
) {
44 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
49 static const size_t OUTPUT_SIZE
= CRIPEMD160::OUTPUT_SIZE
;
51 void Finalize(unsigned char hash
[OUTPUT_SIZE
]) {
52 unsigned char buf
[CSHA256::OUTPUT_SIZE
];
54 CRIPEMD160().Write(buf
, CSHA256::OUTPUT_SIZE
).Finalize(hash
);
57 CHash160
& Write(const unsigned char *data
, size_t len
) {
68 /** Compute the 256-bit hash of an object. */
70 inline uint256
Hash(const T1 pbegin
, const T1 pend
)
72 static const unsigned char pblank
[1] = {};
74 CHash256().Write(pbegin
== pend
? pblank
: (const unsigned char*)&pbegin
[0], (pend
- pbegin
) * sizeof(pbegin
[0]))
75 .Finalize((unsigned char*)&result
);
79 /** Compute the 256-bit hash of the concatenation of two objects. */
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] = {};
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
);
91 /** Compute the 256-bit hash of the concatenation of three objects. */
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] = {};
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
);
105 /** Compute the 160-bit hash an object. */
106 template<typename T1
>
107 inline uint160
Hash160(const T1 pbegin
, const T1 pend
)
109 static unsigned char pblank
[1] = {};
111 CHash160().Write(pbegin
== pend
? pblank
: (const unsigned char*)&pbegin
[0], (pend
- pbegin
) * sizeof(pbegin
[0]))
112 .Finalize((unsigned char*)&result
);
116 /** Compute the 160-bit hash of a vector. */
117 inline uint160
Hash160(const std::vector
<unsigned char>& vch
)
119 return Hash160(vch
.begin(), vch
.end());
122 /** Compute the 160-bit hash of a vector. */
123 template<unsigned int N
>
124 inline uint160
Hash160(const prevector
<N
, unsigned char>& vch
)
126 return Hash160(vch
.begin(), vch
.end());
129 /** A writer stream (for serialization) that computes a 256-bit hash. */
139 CHashWriter(int nTypeIn
, int nVersionIn
) : nType(nTypeIn
), nVersion(nVersionIn
) {}
141 int GetType() const { return nType
; }
142 int GetVersion() const { return nVersion
; }
144 void write(const char *pch
, size_t size
) {
145 ctx
.Write((const unsigned char*)pch
, size
);
148 // invalidates the object
151 ctx
.Finalize((unsigned char*)&result
);
156 CHashWriter
& operator<<(const T
& obj
) {
157 // Serialize to this stream
158 ::Serialize(*this, obj
);
163 /** Reads data from an underlying stream, while hashing the read data. */
164 template<typename Source
>
165 class CHashVerifier
: public CHashWriter
171 CHashVerifier(Source
* source_
) : CHashWriter(source_
->GetType(), source_
->GetVersion()), source(source_
) {}
173 void read(char* pch
, size_t nSize
)
175 source
->read(pch
, nSize
);
176 this->write(pch
, nSize
);
179 void ignore(size_t nSize
)
183 size_t now
= std::min
<size_t>(nSize
, 1024);
190 CHashVerifier
<Source
>& operator>>(T
& obj
)
192 // Unserialize from this stream
193 ::Unserialize(*this, obj
);
198 /** Compute the 256-bit hash of an object's serialization. */
200 uint256
SerializeHash(const T
& obj
, int nType
=SER_GETHASH
, int nVersion
=PROTOCOL_VERSION
)
202 CHashWriter
ss(nType
, nVersion
);
207 unsigned int MurmurHash3(unsigned int nHashSeed
, const std::vector
<unsigned char>& vDataToHash
);
209 void BIP32Hash(const ChainCode
&chainCode
, unsigned int nChild
, unsigned char header
, const unsigned char data
[32], unsigned char output
[64]);
220 /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */
221 CSipHasher(uint64_t k0
, uint64_t k1
);
222 /** Hash a 64-bit integer worth of data
223 * It is treated as if this was the little-endian interpretation of 8 bytes.
224 * This function can only be used when a multiple of 8 bytes have been written so far.
226 CSipHasher
& Write(uint64_t data
);
227 /** Hash arbitrary bytes. */
228 CSipHasher
& Write(const unsigned char* data
, size_t size
);
229 /** Compute the 64-bit SipHash-2-4 of the data written so far. The object remains untouched. */
230 uint64_t Finalize() const;
233 /** Optimized SipHash-2-4 implementation for uint256.
235 * It is identical to:
237 * .Write(val.GetUint64(0))
238 * .Write(val.GetUint64(1))
239 * .Write(val.GetUint64(2))
240 * .Write(val.GetUint64(3))
243 uint64_t SipHashUint256(uint64_t k0
, uint64_t k1
, const uint256
& val
);
244 uint64_t SipHashUint256Extra(uint64_t k0
, uint64_t k1
, const uint256
& val
, uint32_t extra
);
246 #endif // BITCOIN_HASH_H