Bitcoin Core  22.99.0
P2P Digital Currency
hash.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <attributes.h>
10 #include <crypto/common.h>
11 #include <crypto/ripemd160.h>
12 #include <crypto/sha256.h>
13 #include <prevector.h>
14 #include <serialize.h>
15 #include <uint256.h>
16 #include <version.h>
17 
18 #include <string>
19 #include <vector>
20 
22 
24 class CHash256 {
25 private:
27 public:
28  static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29 
31  assert(output.size() == OUTPUT_SIZE);
32  unsigned char buf[CSHA256::OUTPUT_SIZE];
33  sha.Finalize(buf);
34  sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
35  }
36 
38  sha.Write(input.data(), input.size());
39  return *this;
40  }
41 
43  sha.Reset();
44  return *this;
45  }
46 };
47 
49 class CHash160 {
50 private:
52 public:
53  static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54 
56  assert(output.size() == OUTPUT_SIZE);
57  unsigned char 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>
75 inline uint256 Hash(const T& in1)
76 {
77  uint256 result;
78  CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79  return result;
80 }
81 
83 template<typename T1, typename T2>
84 inline uint256 Hash(const T1& in1, const T2& in2) {
85  uint256 result;
87  return result;
88 }
89 
91 template<typename T1>
92 inline uint160 Hash160(const T1& in1)
93 {
94  uint160 result;
95  CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96  return result;
97 }
98 
101 {
102 private:
104 
105  const int nType;
106  const int nVersion;
107 public:
108 
109  CHashWriter(int nTypeIn, int nVersionIn) : nType(nTypeIn), nVersion(nVersionIn) {}
110 
111  int GetType() const { return nType; }
112  int GetVersion() const { return nVersion; }
113 
115  {
116  ctx.Write(UCharCast(src.data()), src.size());
117  }
118 
124  uint256 result;
125  ctx.Finalize(result.begin());
126  ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
127  return result;
128  }
129 
135  uint256 result;
136  ctx.Finalize(result.begin());
137  return result;
138  }
139 
143  inline uint64_t GetCheapHash() {
144  uint256 result = GetHash();
145  return ReadLE64(result.begin());
146  }
147 
148  template<typename T>
149  CHashWriter& operator<<(const T& obj) {
150  // Serialize to this stream
151  ::Serialize(*this, obj);
152  return (*this);
153  }
154 };
155 
157 template<typename Source>
159 {
160 private:
161  Source* source;
162 
163 public:
164  explicit CHashVerifier(Source* source_) : CHashWriter(source_->GetType(), source_->GetVersion()), source(source_) {}
165 
167  {
168  source->read(dst);
169  this->write(dst);
170  }
171 
172  void ignore(size_t nSize)
173  {
174  std::byte data[1024];
175  while (nSize > 0) {
176  size_t now = std::min<size_t>(nSize, 1024);
177  read({data, now});
178  nSize -= now;
179  }
180  }
181 
182  template<typename T>
184  {
185  // Unserialize from this stream
186  ::Unserialize(*this, obj);
187  return (*this);
188  }
189 };
190 
192 template<typename T>
193 uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
194 {
195  CHashWriter ss(nType, nVersion);
196  ss << obj;
197  return ss.GetHash();
198 }
199 
201 [[nodiscard]] uint256 SHA256Uint256(const uint256& input);
202 
203 unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
204 
205 void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
206 
213 CHashWriter TaggedHash(const std::string& tag);
214 
215 #endif // BITCOIN_HASH_H
ReadLE64
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:31
CHashVerifier::CHashVerifier
CHashVerifier(Source *source_)
Definition: hash.h:164
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:193
CHashVerifier::source
Source * source
Definition: hash.h:161
MakeUCharSpan
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
assert
assert(!tx.IsCoinBase())
CSHA256::Write
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:689
CRIPEMD160
A hasher class for RIPEMD-160.
Definition: ripemd160.h:12
CHash256::Write
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
CHashVerifier
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:158
CHash256::Reset
CHash256 & Reset()
Definition: hash.h:42
CHashWriter::operator<<
CHashWriter & operator<<(const T &obj)
Definition: hash.h:149
CRIPEMD160::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
uint256.h
CHashWriter::GetSHA256
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:134
CHashVerifier::ignore
void ignore(size_t nSize)
Definition: hash.h:172
CHashWriter::nType
const int nType
Definition: hash.h:105
SHA256Uint256
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:82
CRIPEMD160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
CSHA256::Reset
CSHA256 & Reset()
Definition: sha256.cpp:732
common.h
version.h
Unserialize
void Unserialize(Stream &, char)=delete
Serialize
void Serialize(Stream &, char)=delete
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:186
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
CHash256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:28
UCharCast
unsigned char * UCharCast(char *c)
Definition: span.h:275
CHash160
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
CHashWriter::nVersion
const int nVersion
Definition: hash.h:106
CRIPEMD160::Write
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
CHash160::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
ChainCode
uint256 ChainCode
Definition: hash.h:21
CHash256::Finalize
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
uint256
256-bit opaque blob.
Definition: uint256.h:126
CSHA256::Finalize
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:715
attributes.h
ripemd160.h
CHashWriter::ctx
CSHA256 ctx
Definition: hash.h:103
CSHA256::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
sha256.h
CHashVerifier::read
void read(Span< std::byte > dst)
Definition: hash.h:166
MurmurHash3
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:17
uint160
160-bit opaque blob.
Definition: uint256.h:115
Span::data
constexpr C * data() const noexcept
Definition: span.h:173
CHash160::sha
CSHA256 sha
Definition: hash.h:51
CHashWriter::CHashWriter
CHashWriter(int nTypeIn, int nVersionIn)
Definition: hash.h:109
Hash160
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
TaggedHash
CHashWriter TaggedHash(const std::string &tag)
Return a CHashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:89
CHashWriter::GetCheapHash
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:143
BIP32Hash
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:75
CHashVerifier::operator>>
CHashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:183
ByteUnit::T
@ T
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
CHash160::Write
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:62
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
CHashWriter::GetVersion
int GetVersion() const
Definition: hash.h:112
serialize.h
CHash256
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
prevector.h
CHashWriter::GetHash
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:123
CHash160::Reset
CHash160 & Reset()
Definition: hash.h:67
CHash160::OUTPUT_SIZE
static const size_t OUTPUT_SIZE
Definition: hash.h:53
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:133
CHash256::sha
CSHA256 sha
Definition: hash.h:26
base_blob::begin
unsigned char * begin()
Definition: uint256.h:60
CHashWriter::write
void write(Span< const std::byte > src)
Definition: hash.h:114
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
CHashWriter::GetType
int GetType() const
Definition: hash.h:111