Update timestamps.h
[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]), hash1.begin());
24     uint256 hash2;
25     SHA256(hash1.begin(), hash1.size(), hash2.begin());
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(hash1.begin(), &ctx);
55         uint256 hash2;
56         SHA256(hash1.begin(), hash1.size(), hash2.begin());
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, const T2 p2begin, const T2 p2end)
71 {
72     static unsigned char pblank[1];
73     uint256 hash1;
74     SHA256_CTX ctx;
75     SHA256_Init(&ctx);
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);
79     uint256 hash2;
80     SHA256(hash1.begin(), hash1.size(), hash2.begin());
81     return hash2;
82 }
83
84 template<typename T>
85 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
86 {
87     CHashWriter ss(nType, nVersion);
88     ss << obj;
89     return ss.GetHash();
90 }
91
92 template<typename T1>
93 inline uint160 Hash160(const T1 pbegin, const T1 pend)
94 {
95     static unsigned char pblank[1];
96     uint256 hash1;
97     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), hash1.begin());
98     uint160 hash2;
99     RIPEMD160(hash1.begin(), hash1.size(), hash2.begin());
100     return hash2;
101 }
102
103 inline uint160 Hash160(const std::vector<unsigned char>& vch)
104 {
105     return Hash160(vch.begin(), vch.end());
106 }
107
108 #endif