1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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.
11 #include <openssl/sha.h>
12 #include <openssl/ripemd.h>
16 inline uint256
Hash(const T1 pbegin
, const T1 pend
)
18 static unsigned char pblank
[1];
20 SHA256((pbegin
== pend
? pblank
: (unsigned char*)&pbegin
[0]), (pend
- pbegin
) * sizeof(pbegin
[0]), (unsigned char*)&hash1
);
22 SHA256((unsigned char*)&hash1
, sizeof(hash1
), (unsigned char*)&hash2
);
39 CHashWriter(int nTypeIn
, int nVersionIn
) : nType(nTypeIn
), nVersion(nVersionIn
) {
43 CHashWriter
& write(const char *pch
, size_t size
) {
44 SHA256_Update(&ctx
, pch
, size
);
48 // invalidates the object
51 SHA256_Final((unsigned char*)&hash1
, &ctx
);
53 SHA256((unsigned char*)&hash1
, sizeof(hash1
), (unsigned char*)&hash2
);
58 CHashWriter
& operator<<(const T
& obj
) {
59 // Serialize to this stream
60 ::Serialize(*this, obj
, nType
, nVersion
);
66 template<typename T1
, typename T2
>
67 inline uint256
Hash(const T1 p1begin
, const T1 p1end
,
68 const T2 p2begin
, const T2 p2end
)
70 static unsigned char pblank
[1];
74 SHA256_Update(&ctx
, (p1begin
== p1end
? pblank
: (unsigned char*)&p1begin
[0]), (p1end
- p1begin
) * sizeof(p1begin
[0]));
75 SHA256_Update(&ctx
, (p2begin
== p2end
? pblank
: (unsigned char*)&p2begin
[0]), (p2end
- p2begin
) * sizeof(p2begin
[0]));
76 SHA256_Final((unsigned char*)&hash1
, &ctx
);
78 SHA256((unsigned char*)&hash1
, sizeof(hash1
), (unsigned char*)&hash2
);
82 template<typename T1
, typename T2
, typename T3
>
83 inline uint256
Hash(const T1 p1begin
, const T1 p1end
,
84 const T2 p2begin
, const T2 p2end
,
85 const T3 p3begin
, const T3 p3end
)
87 static unsigned char pblank
[1];
91 SHA256_Update(&ctx
, (p1begin
== p1end
? pblank
: (unsigned char*)&p1begin
[0]), (p1end
- p1begin
) * sizeof(p1begin
[0]));
92 SHA256_Update(&ctx
, (p2begin
== p2end
? pblank
: (unsigned char*)&p2begin
[0]), (p2end
- p2begin
) * sizeof(p2begin
[0]));
93 SHA256_Update(&ctx
, (p3begin
== p3end
? pblank
: (unsigned char*)&p3begin
[0]), (p3end
- p3begin
) * sizeof(p3begin
[0]));
94 SHA256_Final((unsigned char*)&hash1
, &ctx
);
96 SHA256((unsigned char*)&hash1
, sizeof(hash1
), (unsigned char*)&hash2
);
101 uint256
SerializeHash(const T
& obj
, int nType
=SER_GETHASH
, int nVersion
=PROTOCOL_VERSION
)
103 CHashWriter
ss(nType
, nVersion
);
108 inline uint160
Hash160(const std::vector
<unsigned char>& vch
)
111 SHA256(&vch
[0], vch
.size(), (unsigned char*)&hash1
);
113 RIPEMD160((unsigned char*)&hash1
, sizeof(hash1
), (unsigned char*)&hash2
);
117 unsigned int MurmurHash3(unsigned int nHashSeed
, const std::vector
<unsigned char>& vDataToHash
);