Bitcoin Core 28.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-2022 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 <span.h>
16#include <uint256.h>
17
18#include <string>
19#include <vector>
20
22
24class CHash256 {
25private:
27public:
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);
35 }
36
38 sha.Write(input.data(), input.size());
39 return *this;
40 }
41
43 sha.Reset();
44 return *this;
45 }
46};
47
49class CHash160 {
50private:
52public:
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
74template<typename T>
75inline uint256 Hash(const T& in1)
76{
77 uint256 result;
78 CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79 return result;
80}
81
83template<typename T1, typename T2>
84inline uint256 Hash(const T1& in1, const T2& in2) {
85 uint256 result;
87 return result;
88}
89
91template<typename T1>
92inline uint160 Hash160(const T1& in1)
93{
94 uint160 result;
95 CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96 return result;
97}
98
101{
102private:
104
105public:
107 {
108 ctx.Write(UCharCast(src.data()), src.size());
109 }
110
116 uint256 result;
117 ctx.Finalize(result.begin());
118 ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
119 return result;
120 }
121
127 uint256 result;
128 ctx.Finalize(result.begin());
129 return result;
130 }
131
135 inline uint64_t GetCheapHash() {
136 uint256 result = GetHash();
137 return ReadLE64(result.begin());
138 }
139
140 template <typename T>
142 {
143 ::Serialize(*this, obj);
144 return *this;
145 }
146};
147
149template <typename Source>
151{
152private:
153 Source& m_source;
154
155public:
157
159 {
160 m_source.read(dst);
161 this->write(dst);
162 }
163
164 void ignore(size_t num_bytes)
165 {
166 std::byte data[1024];
167 while (num_bytes > 0) {
168 size_t now = std::min<size_t>(num_bytes, 1024);
169 read({data, now});
170 num_bytes -= now;
171 }
172 }
173
174 template <typename T>
176 {
177 ::Unserialize(*this, obj);
178 return *this;
179 }
180};
181
183template <typename Source>
185{
186private:
187 Source& m_source;
188
189public:
191
193 {
194 m_source.write(src);
196 }
197
198 template <typename T>
200 {
201 ::Serialize(*this, obj);
202 return *this;
203 }
204};
205
207[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
208
209unsigned int MurmurHash3(unsigned int nHashSeed, Span<const unsigned char> vDataToHash);
210
211void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
212
219HashWriter TaggedHash(const std::string& tag);
220
223{
224 uint160 result;
225 CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
226 return result;
227}
228
229#endif // BITCOIN_HASH_H
#define LIFETIMEBOUND
Definition: attributes.h:16
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
Definition: hash.h:49
CHash160 & Reset()
Definition: hash.h:67
static const size_t OUTPUT_SIZE
Definition: hash.h:53
void Finalize(Span< unsigned char > output)
Definition: hash.h:55
CHash160 & Write(Span< const unsigned char > input)
Definition: hash.h:62
CSHA256 sha
Definition: hash.h:51
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
static const size_t OUTPUT_SIZE
Definition: hash.h:28
CHash256 & Reset()
Definition: hash.h:42
CSHA256 sha
Definition: hash.h:26
A hasher class for RIPEMD-160.
Definition: ripemd160.h:13
CRIPEMD160 & Write(const unsigned char *data, size_t len)
Definition: ripemd160.cpp:247
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: ripemd160.cpp:273
static const size_t OUTPUT_SIZE
Definition: ripemd160.h:20
A hasher class for SHA-256.
Definition: sha256.h:14
CSHA256 & Reset()
Definition: sha256.cpp:744
static const size_t OUTPUT_SIZE
Definition: sha256.h:21
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:727
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:701
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:151
Source & m_source
Definition: hash.h:153
HashVerifier(Source &source LIFETIMEBOUND)
Definition: hash.h:156
void read(Span< std::byte > dst)
Definition: hash.h:158
void ignore(size_t num_bytes)
Definition: hash.h:164
HashVerifier< Source > & operator>>(T &&obj)
Definition: hash.h:175
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:101
CSHA256 ctx
Definition: hash.h:103
void write(Span< const std::byte > src)
Definition: hash.h:106
HashWriter & operator<<(const T &obj)
Definition: hash.h:141
uint64_t GetCheapHash()
Returns the first 64 bits from the resulting hash.
Definition: hash.h:135
uint256 GetHash()
Compute the double-SHA256 hash of all data written to this object.
Definition: hash.h:115
uint256 GetSHA256()
Compute the SHA256 hash of all data written to this object.
Definition: hash.h:126
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:185
HashedSourceWriter & operator<<(const T &obj)
Definition: hash.h:199
Source & m_source
Definition: hash.h:187
void write(Span< const std::byte > src)
Definition: hash.h:192
HashedSourceWriter(Source &source LIFETIMEBOUND)
Definition: hash.h:190
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:98
constexpr std::size_t size() const noexcept
Definition: span.h:187
constexpr C * data() const noexcept
Definition: span.h:174
constexpr unsigned char * begin()
Definition: uint256.h:104
160-bit opaque blob.
Definition: uint256.h:178
256-bit opaque blob.
Definition: uint256.h:190
static uint64_t ReadLE64(const unsigned char *ptr)
Definition: common.h:27
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
unsigned int MurmurHash3(unsigned int nHashSeed, Span< const unsigned char > vDataToHash)
Definition: hash.cpp:13
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
Definition: hash.cpp:85
uint160 RIPEMD160(Span< const unsigned char > data)
Compute the 160-bit RIPEMD-160 hash of an array.
Definition: hash.h:222
uint256 ChainCode
Definition: hash.h:21
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
uint256 SHA256Uint256(const uint256 &input)
Single-SHA256 a 32-byte input (represented as uint256).
Definition: hash.cpp:78
const char * source
Definition: rpcconsole.cpp:62
void Serialize(Stream &, V)=delete
void Unserialize(Stream &, V)=delete
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:297
unsigned char * UCharCast(char *c)
Definition: span.h:281
assert(!tx.IsCoinBase())