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 "serialize.h"
17 typedef uint256 ChainCode
;
19 /** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
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
];
29 sha
.Reset().Write(buf
, sha
.OUTPUT_SIZE
).Finalize(hash
);
32 CHash256
& Write(const unsigned char *data
, size_t len
) {
43 /** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
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
];
53 CRIPEMD160().Write(buf
, sha
.OUTPUT_SIZE
).Finalize(hash
);
56 CHash160
& Write(const unsigned char *data
, size_t len
) {
67 /** Compute the 256-bit hash of an object. */
69 inline uint256
Hash(const T1 pbegin
, const T1 pend
)
71 static const unsigned char pblank
[1] = {};
73 CHash256().Write(pbegin
== pend
? pblank
: (const unsigned char*)&pbegin
[0], (pend
- pbegin
) * sizeof(pbegin
[0]))
74 .Finalize((unsigned char*)&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] = {};
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
);
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] = {};
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
);
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] = {};
110 CHash160().Write(pbegin
== pend
? pblank
: (const unsigned char*)&pbegin
[0], (pend
- pbegin
) * sizeof(pbegin
[0]))
111 .Finalize((unsigned char*)&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. */
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
);
138 // invalidates the object
141 ctx
.Finalize((unsigned char*)&result
);
146 CHashWriter
& operator<<(const T
& obj
) {
147 // Serialize to this stream
148 ::Serialize(*this, obj
, nType
, nVersion
);
153 /** Compute the 256-bit hash of an object's serialization. */
155 uint256
SerializeHash(const T
& obj
, int nType
=SER_GETHASH
, int nVersion
=PROTOCOL_VERSION
)
157 CHashWriter
ss(nType
, nVersion
);
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