1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 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.
15 #include <openssl/ripemd.h>
16 #include <openssl/sha.h>
19 inline uint256 Hash(const T1 pbegin, const T1 pend)
21 static unsigned char pblank[1];
23 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), hash1.begin());
25 SHA256(hash1.begin(), hash1.size(), hash2.begin());
42 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
46 CHashWriter& write(const char *pch, size_t size) {
47 SHA256_Update(&ctx, pch, size);
51 // invalidates the object
54 SHA256_Final(hash1.begin(), &ctx);
56 SHA256(hash1.begin(), hash1.size(), hash2.begin());
61 CHashWriter& operator<<(const T& obj) {
62 // Serialize to this stream
63 ::Serialize(*this, obj, nType, nVersion);
69 template<typename T1, typename T2>
70 inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, const T2 p2end)
72 static unsigned char pblank[1];
76 SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
77 SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
78 SHA256_Final(hash1.begin(), &ctx);
80 SHA256(hash1.begin(), hash1.size(), hash2.begin());
85 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
87 CHashWriter ss(nType, nVersion);
93 inline uint160 Hash160(const T1 pbegin, const T1 pend)
95 static unsigned char pblank[1];
97 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), hash1.begin());
99 RIPEMD160(hash1.begin(), hash1.size(), hash2.begin());
103 inline uint160 Hash160(const std::vector<unsigned char>& vch)
105 return Hash160(vch.begin(), vch.end());