diff --git a/src/base58.cpp b/src/base58.cpp --- a/src/base58.cpp +++ b/src/base58.cpp @@ -155,7 +155,7 @@ std::string EncodeBase58Check(const std::vector &vchIn) { // add 4-byte hash check to the end std::vector vch(vchIn); - uint256 hash = Hash(vch.begin(), vch.end()); + uint256 hash = Hash(vch); vch.insert(vch.end(), (uint8_t *)&hash, (uint8_t *)&hash + 4); return EncodeBase58(vch); } @@ -171,7 +171,7 @@ return false; } // re-calculate the checksum, ensure it matches the included 4-byte checksum - uint256 hash = Hash(vchRet.begin(), vchRet.end() - 4); + uint256 hash = Hash(MakeSpan(vchRet).first(vchRet.size() - 4)); if (memcmp(&hash, &vchRet[vchRet.size() - 4], 4) != 0) { vchRet.clear(); return false; diff --git a/src/hash.h b/src/hash.h --- a/src/hash.h +++ b/src/hash.h @@ -71,53 +71,30 @@ }; /** Compute the 256-bit hash of an object. */ -template inline uint256 Hash(const T1 pbegin, const T1 pend) { - static const uint8_t pblank[1] = {}; +template inline uint256 Hash(const T &in1) { uint256 result; - CHash256() - .Write({pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], - (pend - pbegin) * sizeof(pbegin[0])}) - .Finalize(result); + CHash256().Write(MakeUCharSpan(in1)).Finalize(result); return result; } /** Compute the 256-bit hash of the concatenation of two objects. */ template -inline uint256 Hash(const T1 p1begin, const T1 p1end, const T2 p2begin, - const T2 p2end) { - static const uint8_t pblank[1] = {}; +inline uint256 Hash(const T1 &in1, const T2 &in2) { uint256 result; CHash256() - .Write({p1begin == p1end ? pblank : (const uint8_t *)&p1begin[0], - (p1end - p1begin) * sizeof(p1begin[0])}) - .Write({p2begin == p2end ? pblank : (const uint8_t *)&p2begin[0], - (p2end - p2begin) * sizeof(p2begin[0])}) + .Write(MakeUCharSpan(in1)) + .Write(MakeUCharSpan(in2)) .Finalize(result); return result; } /** Compute the 160-bit hash an object. */ -template inline uint160 Hash160(const T1 pbegin, const T1 pend) { - static uint8_t pblank[1] = {}; +template inline uint160 Hash160(const T1 &in1) { uint160 result; - CHash160() - .Write({pbegin == pend ? pblank : (const uint8_t *)&pbegin[0], - (pend - pbegin) * sizeof(pbegin[0])}) - .Finalize(result); + CHash160().Write(MakeUCharSpan(in1)).Finalize(result); return result; } -/** Compute the 160-bit hash of a vector. */ -inline uint160 Hash160(const std::vector &vch) { - return Hash160(vch.begin(), vch.end()); -} - -/** Compute the 160-bit hash of a vector. */ -template -inline uint160 Hash160(const prevector &vch) { - return Hash160(vch.begin(), vch.end()); -} - /** A writer stream (for serialization) that computes a 256-bit hash. */ class CHashWriter { private: diff --git a/src/merkleblock.cpp b/src/merkleblock.cpp --- a/src/merkleblock.cpp +++ b/src/merkleblock.cpp @@ -66,7 +66,7 @@ } // Combine subhashes. - return Hash(left.begin(), left.end(), right.begin(), right.end()); + return Hash(left, right); } void CPartialMerkleTree::TraverseAndBuild(int height, size_t pos, @@ -139,7 +139,7 @@ } // and combine them before returning. - return Hash(left.begin(), left.end(), right.begin(), right.end()); + return Hash(left, right); } CPartialMerkleTree::CPartialMerkleTree(const std::vector &vTxid, diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -780,7 +780,7 @@ CSerializedNetMsg &msg, std::vector &header) { // create dbl-sha256 checksum - uint256 hash = Hash(msg.data.begin(), msg.data.end()); + uint256 hash = Hash(msg.data); // create header CMessageHeader hdr(config.GetChainParams().NetMagic(), msg.m_type.c_str(), diff --git a/src/netaddress.cpp b/src/netaddress.cpp --- a/src/netaddress.cpp +++ b/src/netaddress.cpp @@ -552,7 +552,7 @@ } uint64_t CNetAddr::GetHash() const { - uint256 hash = Hash(&ip[0], &ip[16]); + uint256 hash = Hash(ip); uint64_t nRet; memcpy(&nRet, &hash, sizeof(nRet)); return nRet; diff --git a/src/pubkey.h b/src/pubkey.h --- a/src/pubkey.h +++ b/src/pubkey.h @@ -134,10 +134,12 @@ } //! Get the KeyID of this public key (hash of its serialization) - CKeyID GetID() const { return CKeyID(Hash160(vch, vch + size())); } + CKeyID GetID() const { + return CKeyID(Hash160(MakeSpan(vch).first(size()))); + } //! Get the 256-bit hash of this public key. - uint256 GetHash() const { return Hash(vch, vch + size()); } + uint256 GetHash() const { return Hash(MakeSpan(vch).first(size())); } /* * Check syntactic correctness. diff --git a/src/script/standard.cpp b/src/script/standard.cpp --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -14,13 +14,11 @@ bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER; -CScriptID::CScriptID(const CScript &in) - : BaseHash(Hash160(in.begin(), in.end())) {} +CScriptID::CScriptID(const CScript &in) : BaseHash(Hash160(in)) {} CScriptID::CScriptID(const ScriptHash &in) : BaseHash(static_cast(in)) {} -ScriptHash::ScriptHash(const CScript &in) - : BaseHash(Hash160(in.begin(), in.end())) {} +ScriptHash::ScriptHash(const CScript &in) : BaseHash(Hash160(in)) {} ScriptHash::ScriptHash(const CScriptID &in) : BaseHash(static_cast(in)) {} diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -153,7 +153,7 @@ break; } if (vRecv.GetVersion() >= 209) { - uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize); + uint256 hash = Hash(MakeSpan(vRecv).first(nMessageSize)); if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0) { continue; diff --git a/src/seeder/test/message_writer_tests.cpp b/src/seeder/test/message_writer_tests.cpp --- a/src/seeder/test/message_writer_tests.cpp +++ b/src/seeder/test/message_writer_tests.cpp @@ -44,8 +44,7 @@ CMessageHeader versionhdr(Params().NetMagic(), NetMsgType::VERSION, versionPayload.size()); - uint256 hash = Hash(versionPayload.data(), - versionPayload.data() + versionPayload.size()); + uint256 hash = Hash(versionPayload); memcpy(versionhdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); CDataStream expectedVersion(SER_NETWORK, PROTOCOL_VERSION); @@ -63,7 +62,7 @@ CDataStream expectedVerack(SER_NETWORK, PROTOCOL_VERSION); // This is an empty payload, but is still necessary for the checksum std::vector payload; - uint256 hash = Hash(payload.data(), payload.data() + payload.size()); + uint256 hash = Hash(payload); memcpy(verackHeader.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); expectedVerack << verackHeader; @@ -79,7 +78,7 @@ std::vector vlocator(1, bhash); CBlockLocator locatorhash(vlocator); payload << locatorhash << uint256(); - uint256 hash = Hash(payload.data(), payload.data() + payload.size()); + uint256 hash = Hash(payload); CMessageHeader msgHeader(Params().NetMagic(), NetMsgType::GETHEADERS, payload.size()); diff --git a/src/test/fuzz/crypto.cpp b/src/test/fuzz/crypto.cpp --- a/src/test/fuzz/crypto.cpp +++ b/src/test/fuzz/crypto.cpp @@ -61,9 +61,8 @@ (void)sha512.Write(data.data(), data.size()); (void)sip_hasher.Write(data.data(), data.size()); - (void)Hash(data.begin(), data.end()); + (void)Hash(data); (void)Hash160(data); - (void)Hash160(data.begin(), data.end()); (void)sha512.Size(); break; } diff --git a/src/test/fuzz/key.cpp b/src/test/fuzz/key.cpp --- a/src/test/fuzz/key.cpp +++ b/src/test/fuzz/key.cpp @@ -85,7 +85,7 @@ assert(negated_key == key); } - const uint256 random_uint256 = Hash(buffer.begin(), buffer.end()); + const uint256 random_uint256 = Hash(buffer); { CKey child_key; diff --git a/src/test/key_tests.cpp b/src/test/key_tests.cpp --- a/src/test/key_tests.cpp +++ b/src/test/key_tests.cpp @@ -174,7 +174,7 @@ for (int n = 0; n < 16; n++) { std::string strMsg = strprintf("Very secret message %i: 11", n); - uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); + uint256 hashMsg = Hash(strMsg); // normal ECDSA signatures @@ -282,7 +282,7 @@ std::vector detsig, detsigc; std::string strMsg = "Very deterministic message"; - uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); + uint256 hashMsg = Hash(strMsg); // ECDSA BOOST_CHECK(key1.SignECDSA(hashMsg, detsig)); BOOST_CHECK(key1C.SignECDSA(hashMsg, detsigc)); @@ -341,7 +341,7 @@ // within 20 signatures CKey key = DecodeSecret(strSecret1); std::string msg = "A message to be signed"; - uint256 msg_hash = Hash(msg.begin(), msg.end()); + uint256 msg_hash = Hash(msg); std::vector sig; bool found = false; @@ -363,7 +363,7 @@ for (int i = 0; i < 256; ++i) { sig.clear(); msg = "A message to be signed" + ToString(i); - msg_hash = Hash(msg.begin(), msg.end()); + msg_hash = Hash(msg); BOOST_CHECK(key.SignECDSA(msg_hash, sig)); found = sig[3] == 0x20; BOOST_CHECK(sig.size() <= 70); diff --git a/src/test/merkle_tests.cpp b/src/test/merkle_tests.cpp --- a/src/test/merkle_tests.cpp +++ b/src/test/merkle_tests.cpp @@ -20,9 +20,9 @@ for (std::vector::const_iterator it = vMerkleBranch.begin(); it != vMerkleBranch.end(); ++it) { if (nIndex & 1) { - hash = Hash(it->begin(), it->end(), hash.begin(), hash.end()); + hash = Hash(*it, hash); } else { - hash = Hash(hash.begin(), hash.end(), it->begin(), it->end()); + hash = Hash(hash, *it); } nIndex >>= 1; } @@ -172,8 +172,7 @@ mutated = true; } vMerkleTree.push_back( - Hash(vMerkleTree[j + i].begin(), vMerkleTree[j + i].end(), - vMerkleTree[j + i2].begin(), vMerkleTree[j + i2].end())); + Hash(vMerkleTree[j + i], vMerkleTree[j + i2])); } j += nSize; } diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -151,7 +151,7 @@ for (int i = 0; i < 1000; i++) { ss << float(i); } - BOOST_CHECK(Hash(ss.begin(), ss.end()) == + BOOST_CHECK(Hash(ss) == uint256S("8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85" "099d7f3fe0c")); @@ -169,7 +169,7 @@ for (int i = 0; i < 1000; i++) { ss << double(i); } - BOOST_CHECK(Hash(ss.begin(), ss.end()) == + BOOST_CHECK(Hash(ss) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0" "d7878b23f96")); diff --git a/src/test/sigcache_tests.cpp b/src/test/sigcache_tests.cpp --- a/src/test/sigcache_tests.cpp +++ b/src/test/sigcache_tests.cpp @@ -77,8 +77,8 @@ for (int n = 0; n < 16; n++) { std::string strMsg = strprintf("Sigcache test1 %i: xx", n); - uint256 hashMsg = Hash(strMsg.begin(), strMsg.end()); - uint256 hashMsg2 = Hash(strMsg.begin() + 1, strMsg.end()); + uint256 hashMsg = Hash(strMsg); + uint256 hashMsg2 = Hash(MakeSpan(strMsg).last(strMsg.size() - 1)); std::vector sig; BOOST_CHECK(key1.SignECDSA(hashMsg, sig)); diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -2515,9 +2515,8 @@ std::string(1, (char)MESSAGE_MAGIC.length()) + MESSAGE_MAGIC + std::string(1, (char)unsigned_tx.length()) + unsigned_tx; - const uint256 signature_hash = Hash(unsigned_tx.begin(), unsigned_tx.end()); - const uint256 message_hash1 = - Hash(prefixed_message.begin(), prefixed_message.end()); + const uint256 signature_hash = Hash(unsigned_tx); + const uint256 message_hash1 = Hash(prefixed_message); const uint256 message_hash2 = MessageHash(unsigned_tx); BOOST_CHECK_EQUAL(message_hash1, message_hash2); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -119,9 +119,8 @@ vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); vchKey.insert(vchKey.end(), vchPrivKey.begin(), vchPrivKey.end()); - return WriteIC( - std::make_pair(DBKeys::KEY, vchPubKey), - std::make_pair(vchPrivKey, Hash(vchKey.begin(), vchKey.end())), false); + return WriteIC(std::make_pair(DBKeys::KEY, vchPubKey), + std::make_pair(vchPrivKey, Hash(vchKey)), false); } bool WalletBatch::WriteCryptedKey(const CPubKey &vchPubKey, @@ -132,7 +131,7 @@ } // Compute a checksum of the encrypted key - uint256 checksum = Hash(vchCryptedSecret.begin(), vchCryptedSecret.end()); + uint256 checksum = Hash(vchCryptedSecret); const auto key = std::make_pair(DBKeys::CRYPTED_KEY, vchPubKey); if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) { @@ -226,8 +225,7 @@ return WriteIC(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(desc_id, pubkey)), - std::make_pair(privkey, Hash(key.begin(), key.end())), - false); + std::make_pair(privkey, Hash(key)), false); } bool WalletBatch::WriteCryptedDescriptorKey( @@ -403,7 +401,7 @@ vchKey.insert(vchKey.end(), vchPubKey.begin(), vchPubKey.end()); vchKey.insert(vchKey.end(), pkey.begin(), pkey.end()); - if (Hash(vchKey.begin(), vchKey.end()) != hash) { + if (Hash(vchKey) != hash) { strErr = "Error reading wallet database: CPubKey/CPrivKey " "corrupt"; return false; @@ -454,8 +452,7 @@ if (!ssValue.eof()) { uint256 checksum; ssValue >> checksum; - if ((checksum_valid = Hash(vchPrivKey.begin(), - vchPrivKey.end()) != checksum)) { + if ((checksum_valid = Hash(vchPrivKey) != checksum)) { strErr = "Error reading wallet database: Crypted key corrupt"; return false; @@ -620,7 +617,7 @@ to_hash.insert(to_hash.end(), pubkey.begin(), pubkey.end()); to_hash.insert(to_hash.end(), pkey.begin(), pkey.end()); - if (Hash(to_hash.begin(), to_hash.end()) != hash) { + if (Hash(to_hash) != hash) { strErr = "Error reading wallet database: CPubKey/CPrivKey corrupt"; return false;