Bump version to 0.5.8
[novacoin.git] / src / hash.h
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.
5
6 #ifndef BITCOIN_HASH_H
7 #define BITCOIN_HASH_H
8
9 #include "serialize.h"
10 #include "uint256.h"
11 #include "version.h"
12
13 #include <vector>
14
15 #include <openssl/ripemd.h>
16 #include <openssl/sha.h>
17
18 template<typename T1>
19 inline uint256 Hash(const T1 pbegin, const T1 pend)
20 {
21     static unsigned char pblank[1];
22     uint256 hash1;
23     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
24     uint256 hash2;
25     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
26     return hash2;
27 }
28
29 class CHashWriter
30 {
31 private:
32     SHA256_CTX ctx;
33
34 public:
35     int nType;
36     int nVersion;
37
38     void Init() {
39         SHA256_Init(&ctx);
40     }
41
42     CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
43         Init();
44     }
45
46     CHashWriter& write(const char *pch, size_t size) {
47         SHA256_Update(&ctx, pch, size);
48         return (*this);
49     }
50
51     // invalidates the object
52     uint256 GetHash() {
53         uint256 hash1;
54         SHA256_Final((unsigned char*)&hash1, &ctx);
55         uint256 hash2;
56         SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
57         return hash2;
58     }
59
60     template<typename T>
61     CHashWriter& operator<<(const T& obj) {
62         // Serialize to this stream
63         ::Serialize(*this, obj, nType, nVersion);
64         return (*this);
65     }
66 };
67
68
69 template<typename T1, typename T2>
70 inline uint256 Hash(const T1 p1begin, const T1 p1end,
71                     const T2 p2begin, const T2 p2end)
72 {
73     static unsigned char pblank[1];
74     uint256 hash1;
75     SHA256_CTX ctx;
76     SHA256_Init(&ctx);
77     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
78     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
79     SHA256_Final((unsigned char*)&hash1, &ctx);
80     uint256 hash2;
81     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
82     return hash2;
83 }
84
85 template<typename T1, typename T2, typename T3>
86 inline uint256 Hash(const T1 p1begin, const T1 p1end,
87                     const T2 p2begin, const T2 p2end,
88                     const T3 p3begin, const T3 p3end)
89 {
90     static unsigned char pblank[1];
91     uint256 hash1;
92     SHA256_CTX ctx;
93     SHA256_Init(&ctx);
94     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
95     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
96     SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
97     SHA256_Final((unsigned char*)&hash1, &ctx);
98     uint256 hash2;
99     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
100     return hash2;
101 }
102
103 template<typename T>
104 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
105 {
106     CHashWriter ss(nType, nVersion);
107     ss << obj;
108     return ss.GetHash();
109 }
110
111 template<typename T1>
112 inline uint160 Hash160(const T1 pbegin, const T1 pend)
113 {
114     static unsigned char pblank[1];
115     uint256 hash1;
116     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
117     uint160 hash2;
118     RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
119     return hash2;
120 }
121
122 inline uint160 Hash160(const std::vector<unsigned char>& vch)
123 {
124     return Hash160(vch.begin(), vch.end());
125 }
126
127 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
128
129 typedef struct
130 {
131     SHA512_CTX ctxInner;
132     SHA512_CTX ctxOuter;
133 } HMAC_SHA512_CTX;
134
135 int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len);
136 int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len);
137 int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx);
138
139 #endif