]> Git Repo - VerusCoin.git/blame - src/hash.h
Add support for spending keys to the encrypted wallet.
[VerusCoin.git] / src / hash.h
CommitLineData
0fb9073e 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2013 The Bitcoin Core developers
b4347f60 3// Distributed under the MIT software license, see the accompanying
0fb9073e 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 5
0fb9073e
PW
6#ifndef BITCOIN_HASH_H
7#define BITCOIN_HASH_H
8
a5bc9c09 9#include "crypto/ripemd160.h"
36fa4a78 10#include "crypto/sha256.h"
0fb9073e 11#include "serialize.h"
51ed9ec9
BD
12#include "uint256.h"
13#include "version.h"
0fb9073e 14
7ab026f4 15#include <vector>
0fb9073e 16
a5748996
CF
17typedef uint256 ChainCode;
18
7b4737c8
PW
19/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
20class CHash256 {
21private:
22 CSHA256 sha;
23public:
a0495bb6
PW
24 static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
25
26 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
27 unsigned char buf[sha.OUTPUT_SIZE];
7b4737c8 28 sha.Finalize(buf);
a0495bb6 29 sha.Reset().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
7b4737c8
PW
30 }
31
32 CHash256& Write(const unsigned char *data, size_t len) {
33 sha.Write(data, len);
34 return *this;
35 }
36
37 CHash256& Reset() {
38 sha.Reset();
39 return *this;
40 }
41};
42
43/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
44class CHash160 {
45private:
46 CSHA256 sha;
47public:
a0495bb6
PW
48 static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
49
50 void Finalize(unsigned char hash[OUTPUT_SIZE]) {
51 unsigned char buf[sha.OUTPUT_SIZE];
7b4737c8 52 sha.Finalize(buf);
a0495bb6 53 CRIPEMD160().Write(buf, sha.OUTPUT_SIZE).Finalize(hash);
7b4737c8
PW
54 }
55
56 CHash160& Write(const unsigned char *data, size_t len) {
57 sha.Write(data, len);
58 return *this;
59 }
60
61 CHash160& Reset() {
62 sha.Reset();
63 return *this;
64 }
65};
66
67/** Compute the 256-bit hash of an object. */
0fb9073e
PW
68template<typename T1>
69inline uint256 Hash(const T1 pbegin, const T1 pend)
70{
7b4737c8
PW
71 static const unsigned char pblank[1] = {};
72 uint256 result;
73 CHash256().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
74 .Finalize((unsigned char*)&result);
75 return result;
76}
77
78/** Compute the 256-bit hash of the concatenation of two objects. */
79template<typename T1, typename T2>
80inline uint256 Hash(const T1 p1begin, const T1 p1end,
81 const T2 p2begin, const T2 p2end) {
82 static const unsigned char pblank[1] = {};
83 uint256 result;
84 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
85 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
86 .Finalize((unsigned char*)&result);
87 return result;
88}
89
90/** Compute the 256-bit hash of the concatenation of three objects. */
91template<typename T1, typename T2, typename T3>
92inline uint256 Hash(const T1 p1begin, const T1 p1end,
93 const T2 p2begin, const T2 p2end,
94 const T3 p3begin, const T3 p3end) {
95 static const unsigned char pblank[1] = {};
96 uint256 result;
97 CHash256().Write(p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0]))
98 .Write(p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0]))
99 .Write(p3begin == p3end ? pblank : (const unsigned char*)&p3begin[0], (p3end - p3begin) * sizeof(p3begin[0]))
100 .Finalize((unsigned char*)&result);
101 return result;
102}
103
104/** Compute the 160-bit hash an object. */
105template<typename T1>
106inline uint160 Hash160(const T1 pbegin, const T1 pend)
107{
108 static unsigned char pblank[1] = {};
109 uint160 result;
110 CHash160().Write(pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0]))
111 .Finalize((unsigned char*)&result);
112 return result;
113}
114
115/** Compute the 160-bit hash of a vector. */
116inline uint160 Hash160(const std::vector<unsigned char>& vch)
117{
118 return Hash160(vch.begin(), vch.end());
0fb9073e
PW
119}
120
7b4737c8 121/** A writer stream (for serialization) that computes a 256-bit hash. */
0fb9073e
PW
122class CHashWriter
123{
124private:
7b4737c8 125 CHash256 ctx;
0fb9073e
PW
126
127public:
128 int nType;
129 int nVersion;
130
7b4737c8 131 CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
0fb9073e
PW
132
133 CHashWriter& write(const char *pch, size_t size) {
7b4737c8 134 ctx.Write((const unsigned char*)pch, size);
0fb9073e
PW
135 return (*this);
136 }
137
138 // invalidates the object
139 uint256 GetHash() {
7b4737c8
PW
140 uint256 result;
141 ctx.Finalize((unsigned char*)&result);
142 return result;
0fb9073e
PW
143 }
144
145 template<typename T>
146 CHashWriter& operator<<(const T& obj) {
147 // Serialize to this stream
148 ::Serialize(*this, obj, nType, nVersion);
149 return (*this);
150 }
151};
152
7b4737c8 153/** Compute the 256-bit hash of an object's serialization. */
0fb9073e
PW
154template<typename T>
155uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
156{
157 CHashWriter ss(nType, nVersion);
158 ss << obj;
159 return ss.GetHash();
160}
161
7ab026f4
MC
162unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector<unsigned char>& vDataToHash);
163
a5748996 164void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
b4347f60 165
093303a8 166#endif // BITCOIN_HASH_H
This page took 0.150541 seconds and 4 git commands to generate.