Skip to content

Commit 77c5073

Browse files
committed
Make Hash[160] consume range-like objects
1 parent 02c4cc5 commit 77c5073

16 files changed

+40
-63
lines changed

src/base58.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
141141
{
142142
// add 4-byte hash check to the end
143143
std::vector<unsigned char> vch(vchIn);
144-
uint256 hash = Hash(vch.begin(), vch.end());
144+
uint256 hash = Hash(vch);
145145
vch.insert(vch.end(), (unsigned char*)&hash, (unsigned char*)&hash + 4);
146146
return EncodeBase58(vch);
147147
}
@@ -154,7 +154,7 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int
154154
return false;
155155
}
156156
// re-calculate the checksum, ensure it matches the included 4-byte checksum
157-
uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4);
157+
uint256 hash = Hash(MakeSpan(vchRet).first(vchRet.size() - 4));
158158
if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) {
159159
vchRet.clear();
160160
return false;

src/hash.h

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,52 +69,31 @@ class CHash160 {
6969
};
7070

7171
/** Compute the 256-bit hash of an object. */
72-
template<typename T1>
73-
inline uint256 Hash(const T1 pbegin, const T1 pend)
72+
template<typename T>
73+
inline uint256 Hash(const T& in1)
7474
{
75-
static const unsigned char pblank[1] = {};
7675
uint256 result;
77-
CHash256().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
78-
.Finalize(result);
76+
CHash256().Write(MakeUCharSpan(in1)).Finalize(result);
7977
return result;
8078
}
8179

8280
/** Compute the 256-bit hash of the concatenation of two objects. */
8381
template<typename T1, typename T2>
84-
inline uint256 Hash(const T1 p1begin, const T1 p1end,
85-
const T2 p2begin, const T2 p2end) {
86-
static const unsigned char pblank[1] = {};
82+
inline uint256 Hash(const T1& in1, const T2& in2) {
8783
uint256 result;
88-
CHash256().Write({p1begin == p1end ? pblank : (const unsigned char*)&p1begin[0], (p1end - p1begin) * sizeof(p1begin[0])})
89-
.Write({p2begin == p2end ? pblank : (const unsigned char*)&p2begin[0], (p2end - p2begin) * sizeof(p2begin[0])})
90-
.Finalize(result);
84+
CHash256().Write(MakeUCharSpan(in1)).Write(MakeUCharSpan(in2)).Finalize(result);
9185
return result;
9286
}
9387

9488
/** Compute the 160-bit hash an object. */
9589
template<typename T1>
96-
inline uint160 Hash160(const T1 pbegin, const T1 pend)
90+
inline uint160 Hash160(const T1& in1)
9791
{
98-
static unsigned char pblank[1] = {};
9992
uint160 result;
100-
CHash160().Write({pbegin == pend ? pblank : (const unsigned char*)&pbegin[0], (pend - pbegin) * sizeof(pbegin[0])})
101-
.Finalize(result);
93+
CHash160().Write(MakeUCharSpan(in1)).Finalize(result);
10294
return result;
10395
}
10496

105-
/** Compute the 160-bit hash of a vector. */
106-
inline uint160 Hash160(const std::vector<unsigned char>& vch)
107-
{
108-
return Hash160(vch.begin(), vch.end());
109-
}
110-
111-
/** Compute the 160-bit hash of a vector. */
112-
template<unsigned int N>
113-
inline uint160 Hash160(const prevector<N, unsigned char>& vch)
114-
{
115-
return Hash160(vch.begin(), vch.end());
116-
}
117-
11897
/** A writer stream (for serialization) that computes a 256-bit hash. */
11998
class CHashWriter
12099
{

src/merkleblock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ uint256 CPartialMerkleTree::CalcHash(int height, unsigned int pos, const std::ve
7070
else
7171
right = left;
7272
// combine subhashes
73-
return Hash(left.begin(), left.end(), right.begin(), right.end());
73+
return Hash(left, right);
7474
}
7575
}
7676

@@ -126,7 +126,7 @@ uint256 CPartialMerkleTree::TraverseAndExtract(int height, unsigned int pos, uns
126126
right = left;
127127
}
128128
// and combine them before returning
129-
return Hash(left.begin(), left.end(), right.begin(), right.end());
129+
return Hash(left, right);
130130
}
131131
}
132132

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ CNetMessage V1TransportDeserializer::GetMessage(const CMessageHeader::MessageSta
736736

737737
void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) {
738738
// create dbl-sha256 checksum
739-
uint256 hash = Hash(msg.data.begin(), msg.data.end());
739+
uint256 hash = Hash(msg.data);
740740

741741
// create header
742742
CMessageHeader hdr(Params().MessageStart(), msg.m_type.c_str(), msg.data.size());

src/netaddress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) co
553553

554554
uint64_t CNetAddr::GetHash() const
555555
{
556-
uint256 hash = Hash(&ip[0], &ip[16]);
556+
uint256 hash = Hash(ip);
557557
uint64_t nRet;
558558
memcpy(&nRet, &hash, sizeof(nRet));
559559
return nRet;

src/pubkey.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ class CPubKey
157157
//! Get the KeyID of this public key (hash of its serialization)
158158
CKeyID GetID() const
159159
{
160-
return CKeyID(Hash160(vch, vch + size()));
160+
return CKeyID(Hash160(MakeSpan(vch).first(size())));
161161
}
162162

163163
//! Get the 256-bit hash of this public key.
164164
uint256 GetHash() const
165165
{
166-
return Hash(vch, vch + size());
166+
return Hash(MakeSpan(vch).first(size()));
167167
}
168168

169169
/*

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ static UniValue decodescript(const JSONRPCRequest& request)
601601
UniValue sr(UniValue::VOBJ);
602602
CScript segwitScr;
603603
if (which_type == TxoutType::PUBKEY) {
604-
segwitScr = GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0].begin(), solutions_data[0].end())));
604+
segwitScr = GetScriptForDestination(WitnessV0KeyHash(Hash160(solutions_data[0])));
605605
} else if (which_type == TxoutType::PUBKEYHASH) {
606606
segwitScr = GetScriptForDestination(WitnessV0KeyHash(uint160{solutions_data[0]}));
607607
} else {

src/script/standard.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ typedef std::vector<unsigned char> valtype;
1616
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
1717
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
1818

19-
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in.begin(), in.end())) {}
19+
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
2020
CScriptID::CScriptID(const ScriptHash& in) : BaseHash(static_cast<uint160>(in)) {}
2121

22-
ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in.begin(), in.end())) {}
22+
ScriptHash::ScriptHash(const CScript& in) : BaseHash(Hash160(in)) {}
2323
ScriptHash::ScriptHash(const CScriptID& in) : BaseHash(static_cast<uint160>(in)) {}
2424

2525
PKHash::PKHash(const CPubKey& pubkey) : BaseHash(pubkey.GetID()) {}
@@ -318,7 +318,7 @@ CScript GetScriptForWitness(const CScript& redeemscript)
318318
std::vector<std::vector<unsigned char> > vSolutions;
319319
TxoutType typ = Solver(redeemscript, vSolutions);
320320
if (typ == TxoutType::PUBKEY) {
321-
return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
321+
return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0])));
322322
} else if (typ == TxoutType::PUBKEYHASH) {
323323
return GetScriptForDestination(WitnessV0KeyHash(uint160{vSolutions[0]}));
324324
}

src/test/fuzz/crypto.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ void test_one_input(const std::vector<uint8_t>& buffer)
5454
(void)sha512.Write(data.data(), data.size());
5555
(void)sip_hasher.Write(data.data(), data.size());
5656

57-
(void)Hash(data.begin(), data.end());
57+
(void)Hash(data);
5858
(void)Hash160(data);
59-
(void)Hash160(data.begin(), data.end());
6059
(void)sha512.Size();
6160
break;
6261
}

src/test/fuzz/key.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
8585
assert(negated_key == key);
8686
}
8787

88-
const uint256 random_uint256 = Hash(buffer.begin(), buffer.end());
88+
const uint256 random_uint256 = Hash(buffer);
8989

9090
{
9191
CKey child_key;

0 commit comments

Comments
 (0)