Bitcoin ABC  0.29.2
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 
114  ctx.Write(UCharCast(src.data()), src.size());
115  }
116 
123  uint256 result;
124  ctx.Finalize(result.begin());
125  ctx.Reset()
126  .Write(result.begin(), CSHA256::OUTPUT_SIZE)
127  .Finalize(result.begin());
128  return result;
129  }
130 
137  uint256 result;
138  ctx.Finalize(result.begin());
139  return result;
140  }
141 
145  inline uint64_t GetCheapHash() {
146  uint256 result = GetHash();
147  return ReadLE64(result.begin());
148  }
149 
150  template <typename T> CHashWriter &operator<<(const T &obj) {
151  // Serialize to this stream
152  ::Serialize(*this, obj);
153  return (*this);
154  }
155 };
156 
160 template <typename Source> class CHashVerifier : public CHashWriter {
161 private:
162  Source *source;
163 
164 public:
165  explicit CHashVerifier(Source *source_)
166  : CHashWriter(source_->GetType(), source_->GetVersion()),
167  source(source_) {}
168 
169  void read(Span<std::byte> dst) {
170  source->read(dst);
171  this->write(dst);
172  }
173 
174  void ignore(size_t nSize) {
175  std::byte data[1024];
176  while (nSize > 0) {
177  size_t now = std::min<size_t>(nSize, 1024);
178  read({data, now});
179  nSize -= now;
180  }
181  }
182 
183  template <typename T> CHashVerifier<Source> &operator>>(T &&obj) {
184  // Unserialize from this stream
185  ::Unserialize(*this, obj);
186  return (*this);
187  }
188 };
189 
191 template <typename T>
192 uint256 SerializeHash(const T &obj, int nType = SER_GETHASH,
193  int nVersion = PROTOCOL_VERSION) {
194  CHashWriter ss(nType, nVersion);
195  ss << obj;
196  return ss.GetHash();
197 }
198 
199 uint32_t MurmurHash3(uint32_t nHashSeed, Span<const uint8_t> vDataToHash);
200 
201 void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header,
202  const uint8_t data[32], uint8_t output[64]);
203 
204 #endif // BITCOIN_HASH_H
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:48
static const size_t OUTPUT_SIZE
Definition: hash.h:53
CSHA256 sha
Definition: hash.h:50
CHash160 & Reset()
Definition: hash.h:67
CHash160 & Write(Span< const uint8_t > input)
Definition: hash.h:62
void Finalize(Span< uint8_t > output)
Definition: hash.h:55
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:22
void Finalize(Span< uint8_t > output)
Definition: hash.h:29
static const size_t OUTPUT_SIZE
Definition: hash.h:27
CHash256 & Reset()
Definition: hash.h:41
CSHA256 sha
Definition: hash.h:24
CHash256 & Write(Span< const uint8_t > input)
Definition: hash.h:36
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:160
Source * source
Definition: hash.h:162
CHashVerifier(Source *source_)
Definition: hash.h:165
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:183
void ignore(size_t nSize)
Definition: hash.h:174
void read(Span< std::byte > dst)
Definition: hash.h:169
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:145
int GetType() const
Definition: hash.h:110
const int nType
Definition: hash.h:103
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:136
CHashWriter & operator<<(const T &obj)
Definition: hash.h:150
void write(Span< const std::byte > src)
Definition: hash.h:113
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:107
int GetVersion() const
Definition: hash.h:111
CSHA256 ctx
Definition: hash.h:101
const int nVersion
Definition: hash.h:104
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:122
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CRIPEMD160 & Write(const uint8_t *data, size_t len)
Definition: ripemd160.cpp:286
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:311
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:19
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Reset()
Definition: sha256.cpp:860
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
static const size_t OUTPUT_SIZE
Definition: sha256.h:20
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:209
constexpr C * data() const noexcept
Definition: span.h:198
uint8_t * begin()
Definition: uint256.h:85
160-bit opaque blob.
Definition: uint256.h:117
256-bit opaque blob.
Definition: uint256.h:129
static uint64_t ReadLE64(const uint8_t *ptr)
Definition: common.h:29
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
uint32_t MurmurHash3(uint32_t nHashSeed, Span< const uint8_t > vDataToHash)
Definition: hash.cpp:14
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
Definition: hash.cpp:72
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:192
uint256 ChainCode
Definition: hash.h:19
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
@ SER_GETHASH
Definition: serialize.h:154
void Unserialize(Stream &, char)=delete
void Serialize(Stream &, char)=delete
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
Definition: span.h:337
uint8_t * UCharCast(char *c)
Definition: span.h:309
assert(!tx.IsCoinBase())
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11