]> Git Repo - VerusCoin.git/blob - src/hash.h
Merge pull request #3076 from lano1106/uint256_util
[VerusCoin.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 #ifndef BITCOIN_HASH_H
6 #define BITCOIN_HASH_H
7
8 #include "uint256.h"
9 #include "serialize.h"
10
11 #include <openssl/sha.h>
12 #include <openssl/ripemd.h>
13 #include <vector>
14
15 template<typename T1>
16 inline uint256 Hash(const T1 pbegin, const T1 pend)
17 {
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;
24 }
25
26 class CHashWriter
27 {
28 private:
29     SHA256_CTX ctx;
30
31 public:
32     int nType;
33     int nVersion;
34
35     void Init() {
36         SHA256_Init(&ctx);
37     }
38
39     CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
40         Init();
41     }
42
43     CHashWriter& write(const char *pch, size_t size) {
44         SHA256_Update(&ctx, pch, size);
45         return (*this);
46     }
47
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;
55     }
56
57     template<typename T>
58     CHashWriter& operator<<(const T& obj) {
59         // Serialize to this stream
60         ::Serialize(*this, obj, nType, nVersion);
61         return (*this);
62     }
63 };
64
65
66 template<typename T1, typename T2>
67 inline uint256 Hash(const T1 p1begin, const T1 p1end,
68                     const T2 p2begin, const T2 p2end)
69 {
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;
80 }
81
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)
86 {
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;
98 }
99
100 template<typename T>
101 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
102 {
103     CHashWriter ss(nType, nVersion);
104     ss << obj;
105     return ss.GetHash();
106 }
107
108 template<typename T1>
109 inline uint160 Hash160(const T1 pbegin, const T1 pend)
110 {
111     static unsigned char pblank[1];
112     uint256 hash1;
113     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
114     uint160 hash2;
115     RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
116     return hash2;
117 }
118
119 inline uint160 Hash160(const std::vector<unsigned char>& vch)
120 {
121     return Hash160(vch.begin(), vch.end());
122 }
123
124 unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
125
126 typedef struct
127 {
128     SHA512_CTX ctxInner;
129     SHA512_CTX ctxOuter;
130 } HMAC_SHA512_CTX;
131
132 int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len);
133 int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len);
134 int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx);
135
136 #endif
This page took 0.031333 seconds and 4 git commands to generate.