Bitcoin ABC  0.24.10
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT 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 <crypto/common.h>
10 #include <crypto/ripemd160.h>
11 #include <crypto/sha256.h>
12 #include <prevector.h>
13 #include <serialize.h>
14 #include <uint256.h>
15 #include <version.h>
16 
17 #include <vector>
18 
20 
22 class CHash256 {
23 private:
25 
26 public:
27  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
28 
29  void Finalize(Span<uint8_t> output) {
30  assert(output.size() == OUTPUT_SIZE);
31  uint8_t buf[CSHA256::OUTPUT_SIZE];
32  sha.Finalize(buf);
33  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
34  }
35 
37  sha.Write(input.data(), input.size());
38  return *this;
39  }
40 
42  sha.Reset();
43  return *this;
44  }
45 };
46 
48 class CHash160 {
49 private:
51 
52 public:
53  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54 
55  void Finalize(Span<uint8_t> output) {
56  assert(output.size() == OUTPUT_SIZE);
57  uint8_t buf[CSHA256::OUTPUT_SIZE];
58  sha.Finalize(buf);
60  }
61 
63  sha.Write(input.data(), input.size());
64  return *this;
65  }
66 
68  sha.Reset();
69  return *this;
70  }
71 };
72 
74 template <typename T> inline uint256 Hash(const T &in1) {
75  uint256 result;
76  CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
77  return result;
78 }
79 
81 template <typename T1, typename T2>
82 inline uint256 Hash(const T1 &in1, const T2 &in2) {
83  uint256 result;
84  CHash256()
85  .Write(MakeUCharSpan(in1))
86  .Write(MakeUCharSpan(in2))
87  .Finalize(result);
88  return result;
89 }
90 
92 template <typename T1> inline uint160 Hash160(const T1 &in1) {
93  uint160 result;
94  CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
95  return result;
96 }
97 
99 class CHashWriter {
100 private:
102 
103  const int nType;
104  const int nVersion;
105 
106 public:
107  CHashWriter(int nTypeIn, int nVersionIn)
108  : nType(nTypeIn), nVersion(nVersionIn) {}
109 
110  int GetType() const { return nType; }
111  int GetVersion() const { return nVersion; }
112 
113  void write(const char *pch, size_t size) {
114  ctx.Write({(const uint8_t *)pch, size});
115  }
116 
117  // invalidates the object
119  uint256 result;
120  ctx.Finalize(result);
121  return result;
122  }
123 
127  inline uint64_t GetCheapHash() {
128  uint8_t result[CHash256::OUTPUT_SIZE];
129  ctx.Finalize(result);
130  return ReadLE64(result);
131  }
132 
133  template <typename T> CHashWriter &operator<<(const T &obj) {
134  // Serialize to this stream
135  ::Serialize(*this, obj);
136  return (*this);
137  }
138 };
139 
143 template <typename Source> class CHashVerifier : public CHashWriter {
144 private:
145  Source *source;
146 
147 public:
148  explicit CHashVerifier(Source *source_)
149  : CHashWriter(source_->GetType(), source_->GetVersion()),
150  source(source_) {}
151 
152  void read(char *pch, size_t nSize) {
153  source->read(pch, nSize);
154  this->write(pch, nSize);
155  }
156 
157  void ignore(size_t nSize) {
158  char data[1024];
159  while (nSize > 0) {
160  size_t now = std::min<size_t>(nSize, 1024);
161  read(data, now);
162  nSize -= now;
163  }
164  }
165 
166  template <typename T> CHashVerifier<Source> &operator>>(T &&obj) {
167  // Unserialize from this stream
168  ::Unserialize(*this, obj);
169  return (*this);
170  }
171 };
172 
174 template <typename T>
175 uint256 SerializeHash(const T &obj, int nType = SER_GETHASH,
176  int nVersion = PROTOCOL_VERSION) {
177  CHashWriter ss(nType, nVersion);
178  ss << obj;
179  return ss.GetHash();
180 }
181 
182 uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash);
183 
184 void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
185  const uint8_t data[32], uint8_t output[64]);
186 
187 #endif // BITCOIN_HASH_H
CHashWriter::write
void write(const char *pch, size_t size)
Definition: hash.h:113
CHashVerifier::CHashVerifier
CHashVerifier(Source *source_)
Definition: hash.h:148
SerializeHash
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:175
CHashVerifier::source
Source * source
Definition: hash.h:145
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CHashVerifier
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:143
CHash256::Reset
CHash256 & Reset()
Definition: hash.h:41
CHashWriter::operator<<
CHashWriter & operator<<(const T &obj)
Definition: hash.h:133
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) uint8_t member types only.
Definition: span.h:311
uint256.h
BIP32Hash
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
Definition: hash.cpp:71
ReadLE64
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
CHashVerifier::ignore
void ignore(size_t nSize)
Definition: hash.h:157
CHashWriter::nType
const int nType
Definition: hash.h:103
CRIPEMD160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
CHash256::Finalize
void Finalize(Span< uint8_t > output)
Definition: hash.h:29
CSHA256::Reset
CSHA256 & Reset()
Definition: sha256.cpp:860
version.h
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:209
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
Unserialize
void Unserialize(Stream &s, char &a)
Definition: serialize.h:293
CSHA256::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
CHash256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:27
CHash160
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:48
CHashWriter::nVersion
const int nVersion
Definition: hash.h:104
CHash160::Write
CHash160 & Write(Span< const uint8_t > input)
Definition: hash.h:62
ChainCode
uint256 ChainCode
Definition: hash.h:19
CRIPEMD160::Write
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
CHashVerifier::read
void read(char *pch, size_t nSize)
Definition: hash.h:152
uint256
256-bit opaque blob.
Definition: uint256.h:127
CRIPEMD160::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:311
MurmurHash3
uint32_t MurmurHash3(uint32_t nHashSeed, Span< const uint8_t > vDataToHash)
Definition: hash.cpp:13
ripemd160.h
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
sha256.h
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:167
uint160
160-bit opaque blob.
Definition: uint256.h:115
Span::data
constexpr C * data() const noexcept
Definition: span.h:198
common.h
CHash160::sha
CSHA256 sha
Definition: hash.h:50
CHashWriter::CHashWriter
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:107
Hash160
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
CHashWriter::GetCheapHash
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:127
CHashVerifier::operator>>
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:166
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
CHashWriter::GetVersion
int GetVersion() const
Definition: hash.h:111
serialize.h
CSHA256::Write
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:22
prevector.h
CHash160::Finalize
void Finalize(Span< uint8_t > output)
Definition: hash.h:55
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:118
CHash256::Write
CHash256 & Write(Span< const uint8_t > input)
Definition: hash.h:36
CHash160::Reset
CHash160 & Reset()
Definition: hash.h:67
Serialize
void Serialize(Stream &s, char a)
Definition: serialize.h:241
CHash160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:53
CHash256::sha
CSHA256 sha
Definition: hash.h:24
CHashWriter::ctx
CHash256 ctx
Definition: hash.h:101
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
CHashWriter::GetType
int GetType() const
Definition: hash.h:110