Master Core  v0.0.9 - 49a5c0d97abf09ef2911ddfe8d9551df59f9efd3-dirty
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
hash.h
Go to the documentation of this file.
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 
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
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;
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
SHA512_CTX ctxOuter
Definition: hash.h:132
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
Definition: hash.cpp:8
int nVersion
Definition: hash.h:36
SHA512_CTX ctxInner
Definition: hash.h:131
void Serialize(Stream &s, char a, int, int=0)
Definition: serialize.h:119
CHashWriter & write(const char *pch, size_t size)
Definition: hash.h:46
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:112
int HMAC_SHA512_Init(HMAC_SHA512_CTX *pctx, const void *pkey, size_t len)
Definition: hash.cpp:60
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Definition: hash.h:104
SHA256_CTX ctx
Definition: hash.h:32
int HMAC_SHA512_Update(HMAC_SHA512_CTX *pctx, const void *pdata, size_t len)
Definition: hash.cpp:88
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:19
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:42
uint256 GetHash()
Definition: hash.h:52
256-bit unsigned integer
Definition: uint256.h:531
static const int PROTOCOL_VERSION
Definition: version.h:29
160-bit unsigned integer
Definition: uint256.h:419
CHashWriter & operator<<(const T &obj)
Definition: hash.h:61
void Init()
Definition: hash.h:38
int HMAC_SHA512_Final(unsigned char *pmd, HMAC_SHA512_CTX *pctx)
Definition: hash.cpp:93
int nType
Definition: hash.h:35