Update osx build instructions to ensure users link to the correct version of OpenSSL
[bitcoinplatinum.git] / src / hash.h
blobeaa1780c044c110b03a2a22abff0b2e81b750800
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.
5 #ifndef BITCOIN_HASH_H
6 #define BITCOIN_HASH_H
8 #include "uint256.h"
9 #include "serialize.h"
11 #include <openssl/sha.h>
12 #include <openssl/ripemd.h>
13 #include <vector>
15 template<typename T1>
16 inline uint256 Hash(const T1 pbegin, const T1 pend)
18 static unsigned char pblank[1];
19 uint256 hash1;
20 SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
21 uint256 hash2;
22 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
23 return hash2;
26 class CHashWriter
28 private:
29 SHA256_CTX ctx;
31 public:
32 int nType;
33 int nVersion;
35 void Init() {
36 SHA256_Init(&ctx);
39 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
40 Init();
43 CHashWriter& write(const char *pch, size_t size) {
44 SHA256_Update(&ctx, pch, size);
45 return (*this);
48 // invalidates the object
49 uint256 GetHash() {
50 uint256 hash1;
51 SHA256_Final((unsigned char*)&hash1, &ctx);
52 uint256 hash2;
53 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
54 return hash2;
57 template<typename T>
58 CHashWriter& operator<<(const T& obj) {
59 // Serialize to this stream
60 ::Serialize(*this, obj, nType, nVersion);
61 return (*this);
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];
71 uint256 hash1;
72 SHA256_CTX ctx;
73 SHA256_Init(&ctx);
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);
77 uint256 hash2;
78 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
79 return 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];
88 uint256 hash1;
89 SHA256_CTX ctx;
90 SHA256_Init(&ctx);
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);
95 uint256 hash2;
96 SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
97 return hash2;
100 template<typename T>
101 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
103 CHashWriter ss(nType, nVersion);
104 ss << obj;
105 return ss.GetHash();
108 inline uint160 Hash160(const std::vector<unsigned char>& vch)
110 uint256 hash1;
111 SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
112 uint160 hash2;
113 RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
114 return hash2;
117 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
119 #endif