Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/hash.h
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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
21
typedef uint256 ChainCode;
22
23
/** A hasher class for Bitcoin's 256-bit hash (double SHA-256). */
24
class CHash256 {
25
private:
26
    CSHA256 sha;
27
public:
28
    static const size_t OUTPUT_SIZE = CSHA256::OUTPUT_SIZE;
29
30
1.57M
    void Finalize(std::span<unsigned char> output) {
31
1.57M
        assert(output.size() == OUTPUT_SIZE);
32
1.57M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
33
1.57M
        sha.Finalize(buf);
34
1.57M
        sha.Reset().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
35
1.57M
    }
36
37
2.67M
    CHash256& Write(std::span<const unsigned char> input) {
38
2.67M
        sha.Write(input.data(), input.size());
39
2.67M
        return *this;
40
2.67M
    }
41
42
154k
    CHash256& Reset() {
43
154k
        sha.Reset();
44
154k
        return *this;
45
154k
    }
46
};
47
48
/** A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160). */
49
class CHash160 {
50
private:
51
    CSHA256 sha;
52
public:
53
    static const size_t OUTPUT_SIZE = CRIPEMD160::OUTPUT_SIZE;
54
55
6.12M
    void Finalize(std::span<unsigned char> output) {
56
6.12M
        assert(output.size() == OUTPUT_SIZE);
57
6.12M
        unsigned char buf[CSHA256::OUTPUT_SIZE];
58
6.12M
        sha.Finalize(buf);
59
6.12M
        CRIPEMD160().Write(buf, CSHA256::OUTPUT_SIZE).Finalize(output.data());
60
6.12M
    }
61
62
6.12M
    CHash160& Write(std::span<const unsigned char> input) {
63
6.12M
        sha.Write(input.data(), input.size());
64
6.12M
        return *this;
65
6.12M
    }
66
67
0
    CHash160& Reset() {
68
0
        sha.Reset();
69
0
        return *this;
70
0
    }
71
};
72
73
/** Compute the 256-bit hash of an object. */
74
template<typename T>
75
inline uint256 Hash(const T& in1)
76
397k
{
77
397k
    uint256 result;
78
397k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
397k
    return result;
80
397k
}
uint256 Hash<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
76
3.27k
{
77
3.27k
    uint256 result;
78
3.27k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
3.27k
    return result;
80
3.27k
}
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
76
276
{
77
276
    uint256 result;
78
276
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
276
    return result;
80
276
}
uint256 Hash<DataStream>(DataStream const&)
Line
Count
Source
76
1
{
77
1
    uint256 result;
78
1
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
1
    return result;
80
1
}
uint256 Hash<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
76
366k
{
77
366k
    uint256 result;
78
366k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
366k
    return result;
80
366k
}
uint256 Hash<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
76
26.8k
{
77
26.8k
    uint256 result;
78
26.8k
    CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
79
26.8k
    return result;
80
26.8k
}
81
82
/** Compute the 256-bit hash of the concatenation of two objects. */
83
template<typename T1, typename T2>
84
878k
inline uint256 Hash(const T1& in1, const T2& in2) {
85
878k
    uint256 result;
86
878k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
87
878k
    return result;
88
878k
}
uint256 Hash<uint256, uint256>(uint256 const&, uint256 const&)
Line
Count
Source
84
867k
inline uint256 Hash(const T1& in1, const T2& in2) {
85
867k
    uint256 result;
86
867k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
87
867k
    return result;
88
867k
}
uint256 Hash<CPubKey, std::vector<unsigned char, secure_allocator<unsigned char>>>(CPubKey const&, std::vector<unsigned char, secure_allocator<unsigned char>> const&)
Line
Count
Source
84
6.64k
inline uint256 Hash(const T1& in1, const T2& in2) {
85
6.64k
    uint256 result;
86
6.64k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
87
6.64k
    return result;
88
6.64k
}
uint256 Hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned char [8]>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char const (&) [8])
Line
Count
Source
84
4.55k
inline uint256 Hash(const T1& in1, const T2& in2) {
85
4.55k
    uint256 result;
86
4.55k
    CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
87
4.55k
    return result;
88
4.55k
}
89
90
/** Compute the 160-bit hash an object. */
91
template<typename T1>
92
inline uint160 Hash160(const T1& in1)
93
5.96M
{
94
5.96M
    uint160 result;
95
5.96M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
5.96M
    return result;
97
5.96M
}
uint160 Hash160<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
93
5.66M
{
94
5.66M
    uint160 result;
95
5.66M
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
5.66M
    return result;
97
5.66M
}
uint160 Hash160<XOnlyPubKey>(XOnlyPubKey const&)
Line
Count
Source
93
13.1k
{
94
13.1k
    uint160 result;
95
13.1k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
13.1k
    return result;
97
13.1k
}
uint160 Hash160<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
93
2
{
94
2
    uint160 result;
95
2
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
2
    return result;
97
2
}
uint160 Hash160<CPubKey>(CPubKey const&)
Line
Count
Source
93
2
{
94
2
    uint160 result;
95
2
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
2
    return result;
97
2
}
uint160 Hash160<CScript>(CScript const&)
Line
Count
Source
93
290k
{
94
290k
    uint160 result;
95
290k
    CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
96
290k
    return result;
97
290k
}
98
99
/** A writer stream (for serialization) that computes a 256-bit hash. */
100
class HashWriter
101
{
102
private:
103
    CSHA256 ctx;
104
105
public:
106
    void write(std::span<const std::byte> src)
107
344M
    {
108
344M
        ctx.Write(UCharCast(src.data()), src.size());
109
344M
    }
110
111
    /** Compute the double-SHA256 hash of all data written to this object.
112
     *
113
     * Invalidates this object.
114
     */
115
37.5M
    uint256 GetHash() {
116
37.5M
        uint256 result;
117
37.5M
        ctx.Finalize(result.begin());
118
37.5M
        ctx.Reset().Write(result.begin(), CSHA256::OUTPUT_SIZE).Finalize(result.begin());
119
37.5M
        return result;
120
37.5M
    }
121
122
    /** Compute the SHA256 hash of all data written to this object.
123
     *
124
     * Invalidates this object.
125
     */
126
1.34M
    uint256 GetSHA256() {
127
1.34M
        uint256 result;
128
1.34M
        ctx.Finalize(result.begin());
129
1.34M
        return result;
130
1.34M
    }
131
132
    /**
133
     * Returns the first 64 bits from the resulting hash.
134
     */
135
224k
    inline uint64_t GetCheapHash() {
136
224k
        uint256 result = GetHash();
137
224k
        return ReadLE64(result.begin());
138
224k
    }
139
140
    template <typename T>
141
    HashWriter& operator<<(const T& obj)
142
179M
    {
143
179M
        ::Serialize(*this, obj);
144
179M
        return *this;
145
179M
    }
HashWriter& HashWriter::operator<<<int>(int const&)
Line
Count
Source
142
436k
    {
143
436k
        ::Serialize(*this, obj);
144
436k
        return *this;
145
436k
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction>>(ParamsWrapper<TransactionSerParams, CMutableTransaction> const&)
Line
Count
Source
142
50.0k
    {
143
50.0k
        ::Serialize(*this, obj);
144
50.0k
        return *this;
145
50.0k
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 18446744073709551615ul>>(std::span<unsigned char const, 18446744073709551615ul> const&)
Line
Count
Source
142
966k
    {
143
966k
        ::Serialize(*this, obj);
144
966k
        return *this;
145
966k
    }
HashWriter& HashWriter::operator<<<std::vector<unsigned char, std::allocator<unsigned char>>>(std::vector<unsigned char, std::allocator<unsigned char>> const&)
Line
Count
Source
142
269k
    {
143
269k
        ::Serialize(*this, obj);
144
269k
        return *this;
145
269k
    }
HashWriter& HashWriter::operator<<<std::vector<bool, std::allocator<bool>>>(std::vector<bool, std::allocator<bool>> const&)
Line
Count
Source
142
1
    {
143
1
        ::Serialize(*this, obj);
144
1
        return *this;
145
1
    }
HashWriter& HashWriter::operator<<<transaction_identifier<true>>(transaction_identifier<true> const&)
Line
Count
Source
142
888
    {
143
888
        ::Serialize(*this, obj);
144
888
        return *this;
145
888
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char const, 32ul>>(std::span<unsigned char const, 32ul> const&)
Line
Count
Source
142
93.4M
    {
143
93.4M
        ::Serialize(*this, obj);
144
93.4M
        return *this;
145
93.4M
    }
HashWriter& HashWriter::operator<<<transaction_identifier<false>>(transaction_identifier<false> const&)
Line
Count
Source
142
3
    {
143
3
        ::Serialize(*this, obj);
144
3
        return *this;
145
3
    }
HashWriter& HashWriter::operator<<<uint256>(uint256 const&)
Line
Count
Source
142
2.28M
    {
143
2.28M
        ::Serialize(*this, obj);
144
2.28M
        return *this;
145
2.28M
    }
HashWriter& HashWriter::operator<<<unsigned long>(unsigned long const&)
Line
Count
Source
142
46.8k
    {
143
46.8k
        ::Serialize(*this, obj);
144
46.8k
        return *this;
145
46.8k
    }
HashWriter& HashWriter::operator<<<unsigned char>(unsigned char const&)
Line
Count
Source
142
1.31M
    {
143
1.31M
        ::Serialize(*this, obj);
144
1.31M
        return *this;
145
1.31M
    }
HashWriter& HashWriter::operator<<<COutPoint>(COutPoint const&)
Line
Count
Source
142
20.6M
    {
143
20.6M
        ::Serialize(*this, obj);
144
20.6M
        return *this;
145
20.6M
    }
HashWriter& HashWriter::operator<<<unsigned int>(unsigned int const&)
Line
Count
Source
142
8.24M
    {
143
8.24M
        ::Serialize(*this, obj);
144
8.24M
        return *this;
145
8.24M
    }
HashWriter& HashWriter::operator<<<CTxOut>(CTxOut const&)
Line
Count
Source
142
13.9M
    {
143
13.9M
        ::Serialize(*this, obj);
144
13.9M
        return *this;
145
13.9M
    }
HashWriter& HashWriter::operator<<<CBlockUndo>(CBlockUndo const&)
Line
Count
Source
142
102k
    {
143
102k
        ::Serialize(*this, obj);
144
102k
        return *this;
145
102k
    }
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&> const&)
Line
Count
Source
142
67.6k
    {
143
67.6k
        ::Serialize(*this, obj);
144
67.6k
        return *this;
145
67.6k
    }
HashWriter& HashWriter::operator<<<std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul> const&)
Line
Count
Source
142
8.90k
    {
143
8.90k
        ::Serialize(*this, obj);
144
8.90k
        return *this;
145
8.90k
    }
HashWriter& HashWriter::operator<<<Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&> const&)
Line
Count
Source
142
58.7k
    {
143
58.7k
        ::Serialize(*this, obj);
144
58.7k
        return *this;
145
58.7k
    }
HashWriter& HashWriter::operator<<<CBlockHeader>(CBlockHeader const&)
Line
Count
Source
142
35.0M
    {
143
35.0M
        ::Serialize(*this, obj);
144
35.0M
        return *this;
145
35.0M
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CMutableTransaction const>>(ParamsWrapper<TransactionSerParams, CMutableTransaction const> const&)
Line
Count
Source
142
684k
    {
143
684k
        ::Serialize(*this, obj);
144
684k
        return *this;
145
684k
    }
HashWriter& HashWriter::operator<<<ParamsWrapper<TransactionSerParams, CTransaction const>>(ParamsWrapper<TransactionSerParams, CTransaction const> const&)
Line
Count
Source
142
1.44M
    {
143
1.44M
        ::Serialize(*this, obj);
144
1.44M
        return *this;
145
1.44M
    }
HashWriter& HashWriter::operator<<<long>(long const&)
Line
Count
Source
142
190k
    {
143
190k
        ::Serialize(*this, obj);
144
190k
        return *this;
145
190k
    }
HashWriter& HashWriter::operator<<<CScript>(CScript const&)
Line
Count
Source
142
190k
    {
143
190k
        ::Serialize(*this, obj);
144
190k
        return *this;
145
190k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CTransaction> const&)
Line
Count
Source
142
40.0k
    {
143
40.0k
        ::Serialize(*this, obj);
144
40.0k
        return *this;
145
40.0k
    }
interpreter.cpp:HashWriter& HashWriter::operator<<<(anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>>((anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction> const&)
Line
Count
Source
142
82.6k
    {
143
82.6k
        ::Serialize(*this, obj);
144
82.6k
        return *this;
145
82.6k
    }
HashWriter& HashWriter::operator<<<CompactSizeWriter>(CompactSizeWriter const&)
Line
Count
Source
142
135k
    {
143
135k
        ::Serialize(*this, obj);
144
135k
        return *this;
145
135k
    }
HashWriter& HashWriter::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
142
52
    {
143
52
        ::Serialize(*this, obj);
144
52
        return *this;
145
52
    }
HashWriter& HashWriter::operator<<<CPubKey>(CPubKey const&)
Line
Count
Source
142
410
    {
143
410
        ::Serialize(*this, obj);
144
410
        return *this;
145
410
    }
HashWriter& HashWriter::operator<<<std::span<std::byte const, 18446744073709551615ul>>(std::span<std::byte const, 18446744073709551615ul> const&)
Line
Count
Source
142
26
    {
143
26
        ::Serialize(*this, obj);
144
26
        return *this;
145
26
    }
HashWriter& HashWriter::operator<<<unsigned char [384]>(unsigned char const (&) [384])
Line
Count
Source
142
4.09k
    {
143
4.09k
        ::Serialize(*this, obj);
144
4.09k
        return *this;
145
4.09k
    }
146
};
147
148
/** Reads data from an underlying stream, while hashing the read data. */
149
template <typename Source>
150
class HashVerifier : public HashWriter
151
{
152
private:
153
    Source& m_source;
154
155
public:
156
37.4k
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<DataStream>::HashVerifier(DataStream&)
Line
Count
Source
156
3
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<AutoFile>::HashVerifier(AutoFile&)
Line
Count
Source
156
597
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
HashVerifier<BufferedReader<AutoFile>>::HashVerifier(BufferedReader<AutoFile>&)
Line
Count
Source
156
36.8k
    explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
157
158
    void read(std::span<std::byte> dst)
159
1.11M
    {
160
1.11M
        m_source.read(dst);
161
1.11M
        this->write(dst);
162
1.11M
    }
HashVerifier<DataStream>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
159
1.08k
    {
160
1.08k
        m_source.read(dst);
161
1.08k
        this->write(dst);
162
1.08k
    }
HashVerifier<AutoFile>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
159
847k
    {
160
847k
        m_source.read(dst);
161
847k
        this->write(dst);
162
847k
    }
HashVerifier<BufferedReader<AutoFile>>::read(std::span<std::byte, 18446744073709551615ul>)
Line
Count
Source
159
266k
    {
160
266k
        m_source.read(dst);
161
266k
        this->write(dst);
162
266k
    }
163
164
    void ignore(size_t num_bytes)
165
0
    {
166
0
        std::byte data[1024];
167
0
        while (num_bytes > 0) {
168
0
            size_t now = std::min<size_t>(num_bytes, 1024);
169
0
            read({data, now});
170
0
            num_bytes -= now;
171
0
        }
172
0
    }
Unexecuted instantiation: HashVerifier<AutoFile>::ignore(unsigned long)
Unexecuted instantiation: HashVerifier<DataStream>::ignore(unsigned long)
Unexecuted instantiation: HashVerifier<BufferedReader<AutoFile>>::ignore(unsigned long)
173
174
    template <typename T>
175
    HashVerifier<Source>& operator>>(T&& obj)
176
122k
    {
177
122k
        ::Unserialize(*this, obj);
178
122k
        return *this;
179
122k
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
Line
Count
Source
176
1
    {
177
1
        ::Unserialize(*this, obj);
178
1
        return *this;
179
1
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&)
Line
Count
Source
176
2
    {
177
2
        ::Unserialize(*this, obj);
178
2
        return *this;
179
2
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><AddrMan&>(AddrMan&)
Line
Count
Source
176
2
    {
177
2
        ::Unserialize(*this, obj);
178
2
        return *this;
179
2
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><std::array<unsigned char, 4ul>&>(std::array<unsigned char, 4ul>&)
Line
Count
Source
176
597
    {
177
597
        ::Unserialize(*this, obj);
178
597
        return *this;
179
597
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><AddrMan&>(AddrMan&)
Line
Count
Source
176
570
    {
177
570
        ::Unserialize(*this, obj);
178
570
        return *this;
179
570
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>>>&)
Line
Count
Source
176
26
    {
177
26
        ::Unserialize(*this, obj);
178
26
        return *this;
179
26
    }
HashVerifier<AutoFile>& HashVerifier<AutoFile>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&)
Line
Count
Source
176
570
    {
177
570
        ::Unserialize(*this, obj);
178
570
        return *this;
179
570
    }
HashVerifier<DataStream>& HashVerifier<DataStream>::operator>><Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>>(Wrapper<CustomUintFormatter<1, false>, AddrManImpl::Format&>&&)
Line
Count
Source
176
2
    {
177
2
        ::Unserialize(*this, obj);
178
2
        return *this;
179
2
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><CBlockUndo&>(CBlockUndo&)
Line
Count
Source
176
36.8k
    {
177
36.8k
        ::Unserialize(*this, obj);
178
36.8k
        return *this;
179
36.8k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned long&>&&)
Line
Count
Source
176
27.8k
    {
177
27.8k
        ::Unserialize(*this, obj);
178
27.8k
        return *this;
179
27.8k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>>(Wrapper<VarIntFormatter<(VarIntMode)0>, unsigned int&>&&)
Line
Count
Source
176
27.8k
    {
177
27.8k
        ::Unserialize(*this, obj);
178
27.8k
        return *this;
179
27.8k
    }
HashVerifier<BufferedReader<AutoFile>>& HashVerifier<BufferedReader<AutoFile>>::operator>><std::span<unsigned char, 18446744073709551615ul>>(std::span<unsigned char, 18446744073709551615ul>&&)
Line
Count
Source
176
27.8k
    {
177
27.8k
        ::Unserialize(*this, obj);
178
27.8k
        return *this;
179
27.8k
    }
180
};
181
182
/** Writes data to an underlying source stream, while hashing the written data. */
183
template <typename Source>
184
class HashedSourceWriter : public HashWriter
185
{
186
private:
187
    Source& m_source;
188
189
public:
190
1.51k
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
HashedSourceWriter<DataStream>::HashedSourceWriter(DataStream&)
Line
Count
Source
190
1
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
HashedSourceWriter<AutoFile>::HashedSourceWriter(AutoFile&)
Line
Count
Source
190
1.51k
    explicit HashedSourceWriter(Source& source LIFETIMEBOUND) : HashWriter{}, m_source{source} {}
191
192
    void write(std::span<const std::byte> src)
193
2.18M
    {
194
2.18M
        m_source.write(src);
195
2.18M
        HashWriter::write(src);
196
2.18M
    }
HashedSourceWriter<DataStream>::write(std::span<std::byte const, 18446744073709551615ul>)
Line
Count
Source
193
2
    {
194
2
        m_source.write(src);
195
2
        HashWriter::write(src);
196
2
    }
HashedSourceWriter<AutoFile>::write(std::span<std::byte const, 18446744073709551615ul>)
Line
Count
Source
193
2.18M
    {
194
2.18M
        m_source.write(src);
195
2.18M
        HashWriter::write(src);
196
2.18M
    }
197
198
    template <typename T>
199
    HashedSourceWriter& operator<<(const T& obj)
200
3.02k
    {
201
3.02k
        ::Serialize(*this, obj);
202
3.02k
        return *this;
203
3.02k
    }
HashedSourceWriter<DataStream>& HashedSourceWriter<DataStream>::operator<<<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
Line
Count
Source
200
1
    {
201
1
        ::Serialize(*this, obj);
202
1
        return *this;
203
1
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<std::array<unsigned char, 4ul>>(std::array<unsigned char, 4ul> const&)
Line
Count
Source
200
1.51k
    {
201
1.51k
        ::Serialize(*this, obj);
202
1.51k
        return *this;
203
1.51k
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<AddrMan>(AddrMan const&)
Line
Count
Source
200
1.48k
    {
201
1.48k
        ::Serialize(*this, obj);
202
1.48k
        return *this;
203
1.48k
    }
HashedSourceWriter<AutoFile>& HashedSourceWriter<AutoFile>::operator<<<ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const>>(ParamsWrapper<CAddress::SerParams, std::vector<CAddress, std::allocator<CAddress>> const> const&)
Line
Count
Source
200
31
    {
201
31
        ::Serialize(*this, obj);
202
31
        return *this;
203
31
    }
204
};
205
206
/** Single-SHA256 a 32-byte input (represented as uint256). */
207
[[nodiscard]] uint256 SHA256Uint256(const uint256& input);
208
209
unsigned int MurmurHash3(unsigned int nHashSeed, std::span<const unsigned char> vDataToHash);
210
211
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64]);
212
213
/** Return a HashWriter primed for tagged hashes (as specified in BIP 340).
214
 *
215
 * The returned object will have SHA256(tag) written to it twice (= 64 bytes).
216
 * A tagged hash can be computed by feeding the message into this object, and
217
 * then calling HashWriter::GetSHA256().
218
 */
219
HashWriter TaggedHash(const std::string& tag);
220
221
/** Compute the 160-bit RIPEMD-160 hash of an array. */
222
inline uint160 RIPEMD160(std::span<const unsigned char> data)
223
2.15k
{
224
2.15k
    uint160 result;
225
2.15k
    CRIPEMD160().Write(data.data(), data.size()).Finalize(result.begin());
226
2.15k
    return result;
227
2.15k
}
228
229
#endif // BITCOIN_HASH_H