]> Git Repo - VerusCoin.git/blob - src/hash.h
Merge pull request #2099 from gavinandresen/blkfile_upgrade
[VerusCoin.git] / src / hash.h
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
7
8 #include "uint256.h"
9 #include "serialize.h"
10
11 #include <openssl/sha.h>
12 #include <openssl/ripemd.h>
13
14 template<typename T1>
15 inline uint256 Hash(const T1 pbegin, const T1 pend)
16 {
17     static unsigned char pblank[1];
18     uint256 hash1;
19     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
20     uint256 hash2;
21     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
22     return hash2;
23 }
24
25 class CHashWriter
26 {
27 private:
28     SHA256_CTX ctx;
29
30 public:
31     int nType;
32     int nVersion;
33
34     void Init() {
35         SHA256_Init(&ctx);
36     }
37
38     CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {
39         Init();
40     }
41
42     CHashWriter& write(const char *pch, size_t size) {
43         SHA256_Update(&ctx, pch, size);
44         return (*this);
45     }
46
47     // invalidates the object
48     uint256 GetHash() {
49         uint256 hash1;
50         SHA256_Final((unsigned char*)&hash1, &ctx);
51         uint256 hash2;
52         SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
53         return hash2;
54     }
55
56     template<typename T>
57     CHashWriter& operator<<(const T& obj) {
58         // Serialize to this stream
59         ::Serialize(*this, obj, nType, nVersion);
60         return (*this);
61     }
62 };
63
64
65 template<typename T1, typename T2>
66 inline uint256 Hash(const T1 p1begin, const T1 p1end,
67                     const T2 p2begin, const T2 p2end)
68 {
69     static unsigned char pblank[1];
70     uint256 hash1;
71     SHA256_CTX ctx;
72     SHA256_Init(&ctx);
73     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
74     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
75     SHA256_Final((unsigned char*)&hash1, &ctx);
76     uint256 hash2;
77     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
78     return hash2;
79 }
80
81 template<typename T1, typename T2, typename T3>
82 inline uint256 Hash(const T1 p1begin, const T1 p1end,
83                     const T2 p2begin, const T2 p2end,
84                     const T3 p3begin, const T3 p3end)
85 {
86     static unsigned char pblank[1];
87     uint256 hash1;
88     SHA256_CTX ctx;
89     SHA256_Init(&ctx);
90     SHA256_Update(&ctx, (p1begin == p1end ? pblank : (unsigned char*)&p1begin[0]), (p1end - p1begin) * sizeof(p1begin[0]));
91     SHA256_Update(&ctx, (p2begin == p2end ? pblank : (unsigned char*)&p2begin[0]), (p2end - p2begin) * sizeof(p2begin[0]));
92     SHA256_Update(&ctx, (p3begin == p3end ? pblank : (unsigned char*)&p3begin[0]), (p3end - p3begin) * sizeof(p3begin[0]));
93     SHA256_Final((unsigned char*)&hash1, &ctx);
94     uint256 hash2;
95     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
96     return hash2;
97 }
98
99 template<typename T>
100 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
101 {
102     CHashWriter ss(nType, nVersion);
103     ss << obj;
104     return ss.GetHash();
105 }
106
107 inline uint160 Hash160(const std::vector<unsigned char>& vch)
108 {
109     uint256 hash1;
110     SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
111     uint160 hash2;
112     RIPEMD160((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
113     return hash2;
114 }
115
116 #endif
This page took 0.030291 seconds and 4 git commands to generate.