mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
Partial #19660: Make HexStr take a span
Comment from 6f7b52ac63a71d2706022ca58d69a1a622e0fa37: "The fix for CPubKey is a part of `#13557: BIP 174 PSBT Serializations and RPCs` which wasn't backported yet"
This commit is contained in:
parent
73b5359532
commit
bc2f11f230
@ -57,13 +57,14 @@ std::string FormatScript(const CScript& script)
|
||||
}
|
||||
}
|
||||
if (vch.size() > 0) {
|
||||
ret += strprintf("0x%x 0x%x ", HexStr(it2, it - vch.size()), HexStr(it - vch.size(), it));
|
||||
ret += strprintf("0x%x 0x%x ", HexStr(std::vector<uint8_t>(it2, it - vch.size())),
|
||||
HexStr(std::vector<uint8_t>(it - vch.size(), it)));
|
||||
} else {
|
||||
ret += strprintf("0x%x ", HexStr(it2, it));
|
||||
ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, it)));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ret += strprintf("0x%x ", HexStr(it2, script.end()));
|
||||
ret += strprintf("0x%x ", HexStr(std::vector<uint8_t>(it2, script.end())));
|
||||
break;
|
||||
}
|
||||
return ret.substr(0, ret.size() - 1);
|
||||
|
@ -3691,13 +3691,13 @@ bool PeerLogicValidation::ProcessMessages(CNode* pfrom, std::atomic<bool>& inter
|
||||
|
||||
// Checksum
|
||||
CDataStream& vRecv = msg.vRecv;
|
||||
const uint256& hash = msg.GetMessageHash();
|
||||
uint256 hash = msg.GetMessageHash();
|
||||
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
|
||||
{
|
||||
LogPrint(BCLog::NET, "%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
|
||||
SanitizeString(strCommand), nMessageSize,
|
||||
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
|
||||
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
|
||||
HexStr(Span<uint8_t>(hash.begin(), hash.begin() + CMessageHeader::CHECKSUM_SIZE)),
|
||||
HexStr(hdr.pchChecksum));
|
||||
return fMoreWork;
|
||||
}
|
||||
|
||||
|
@ -107,6 +107,7 @@ public:
|
||||
|
||||
//! Simple read-only vector-like interface to the pubkey data.
|
||||
unsigned int size() const { return GetLen(vch[0]); }
|
||||
const unsigned char* data() const { return vch; }
|
||||
const unsigned char* begin() const { return vch; }
|
||||
const unsigned char* end() const { return vch + size(); }
|
||||
const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
|
||||
|
@ -81,8 +81,7 @@ bool GenerateAuthCookie(std::string *cookie_out)
|
||||
const size_t COOKIE_SIZE = 32;
|
||||
unsigned char rand_pwd[COOKIE_SIZE];
|
||||
GetRandBytes(rand_pwd, COOKIE_SIZE);
|
||||
|
||||
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd, rand_pwd+COOKIE_SIZE);
|
||||
std::string cookie = COOKIEAUTH_USER + ":" + HexStr(rand_pwd);
|
||||
|
||||
/** the umask determines what permissions are used to create this file -
|
||||
* these are set to 077 in init.cpp unless overridden with -sysperms.
|
||||
|
@ -533,19 +533,19 @@ BOOST_AUTO_TEST_CASE(pbkdf2_hmac_sha512_test) {
|
||||
|
||||
strcpy((char *)s, "salt");
|
||||
PKCS5_PBKDF2_HMAC("password", 8, s, 4, 1, EVP_sha512(), 64, k);
|
||||
BOOST_CHECK(HexStr(k, k + 64) == "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce");
|
||||
BOOST_CHECK(HexStr(k) == "867f70cf1ade02cff3752599a3a53dc4af34c7a669815ae5d513554e1c8cf252c02d470a285a0501bad999bfe943c08f050235d7d68b1da55e63f73b60a57fce");
|
||||
|
||||
strcpy((char *)s, "salt");
|
||||
PKCS5_PBKDF2_HMAC("password", 8, s, 4, 2, EVP_sha512(), 64, k);
|
||||
BOOST_CHECK(HexStr(k, k + 64) == "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e");
|
||||
BOOST_CHECK(HexStr(k) == "e1d9c16aa681708a45f5c7c4e215ceb66e011a2e9f0040713f18aefdb866d53cf76cab2868a39b9f7840edce4fef5a82be67335c77a6068e04112754f27ccf4e");
|
||||
|
||||
strcpy((char *)s, "salt");
|
||||
PKCS5_PBKDF2_HMAC("password", 8, s, 4, 4096, EVP_sha512(), 64, k);
|
||||
BOOST_CHECK(HexStr(k, k + 64) == "d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5");
|
||||
BOOST_CHECK(HexStr(k) == "d197b1b33db0143e018b12f3d1d1479e6cdebdcc97c5c0f87f6902e072f457b5143f30602641b3d55cd335988cb36b84376060ecd532e039b742a239434af2d5");
|
||||
|
||||
strcpy((char *)s, "saltSALTsaltSALTsaltSALTsaltSALTsalt");
|
||||
PKCS5_PBKDF2_HMAC("passwordPASSWORDpassword", 3*8, s, 9*4, 4096, EVP_sha512(), 64, k);
|
||||
BOOST_CHECK(HexStr(k, k + 64) == "8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8");
|
||||
BOOST_CHECK(HexStr(k) == "8c0511f4c6e597c6ac6315d8f0362e225f3c501495ba23b868c005174dc4ee71115b59f9e60cd9532fa33e0f75aefe30225c583a186cd82bd4daea9724a3d3b8");
|
||||
}
|
||||
|
||||
|
||||
|
@ -74,87 +74,24 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
|
||||
BOOST_AUTO_TEST_CASE(util_HexStr)
|
||||
{
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)),
|
||||
HexStr(ParseHex_expected),
|
||||
"04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected, ParseHex_expected + 5, true),
|
||||
"04 67 8a fd b0");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected + sizeof(ParseHex_expected),
|
||||
ParseHex_expected + sizeof(ParseHex_expected)),
|
||||
HexStr(Span<const unsigned char>(
|
||||
ParseHex_expected + sizeof(ParseHex_expected),
|
||||
ParseHex_expected + sizeof(ParseHex_expected))),
|
||||
"");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected + sizeof(ParseHex_expected),
|
||||
ParseHex_expected + sizeof(ParseHex_expected), true),
|
||||
"");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected, ParseHex_expected),
|
||||
"");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_expected, ParseHex_expected, true),
|
||||
HexStr(Span<const unsigned char>(ParseHex_expected, ParseHex_expected)),
|
||||
"");
|
||||
|
||||
std::vector<unsigned char> ParseHex_vec(ParseHex_expected, ParseHex_expected + 5);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_vec, true),
|
||||
"04 67 8a fd b0");
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend()),
|
||||
"b0fd8a6704"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(ParseHex_vec.rbegin(), ParseHex_vec.rend(), true),
|
||||
"b0 fd 8a 67 04"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
|
||||
""
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
|
||||
""
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
|
||||
"04"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 1),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
|
||||
"04"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
|
||||
"b0fd8a6704"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 5),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected), true),
|
||||
"b0 fd 8a 67 04"
|
||||
);
|
||||
|
||||
BOOST_CHECK_EQUAL(
|
||||
HexStr(std::reverse_iterator<const uint8_t *>(ParseHex_expected + 65),
|
||||
std::reverse_iterator<const uint8_t *>(ParseHex_expected)),
|
||||
"5f1df16b2b704c8a578d0bbaf74d385cde12c11ee50455f3c438ef4c3fbcf649b6de611feae06279a60939e028a8d65c10b73071a6f16719274855feb0fd8a6704"
|
||||
HexStr(ParseHex_vec),
|
||||
"04678afdb0"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,11 @@ base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
|
||||
template <unsigned int BITS>
|
||||
std::string base_blob<BITS>::GetHex() const
|
||||
{
|
||||
return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(data));
|
||||
uint8_t m_data_rev[WIDTH];
|
||||
for (int i = 0; i < WIDTH; ++i) {
|
||||
m_data_rev[i] = data[WIDTH - 1 - i];
|
||||
}
|
||||
return HexStr(m_data_rev);
|
||||
}
|
||||
|
||||
template <unsigned int BITS>
|
||||
|
@ -543,3 +543,16 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string HexStr(const Span<const uint8_t> s)
|
||||
{
|
||||
std::string rv;
|
||||
static constexpr char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
rv.reserve(s.size() * 2);
|
||||
for (uint8_t v: s) {
|
||||
rv.push_back(hexmap[v >> 4]);
|
||||
rv.push_back(hexmap[v & 15]);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
@ -10,6 +10,7 @@
|
||||
#define BITCOIN_UTILSTRENCODINGS_H
|
||||
|
||||
#include <attributes.h>
|
||||
#include <span.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
@ -108,30 +109,11 @@ NODISCARD bool ParseUInt64(const std::string& str, uint64_t *out);
|
||||
*/
|
||||
NODISCARD bool ParseDouble(const std::string& str, double *out);
|
||||
|
||||
template<typename T>
|
||||
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
|
||||
{
|
||||
std::string rv;
|
||||
static const char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
rv.reserve((itend-itbegin)*3);
|
||||
for(T it = itbegin; it < itend; ++it)
|
||||
{
|
||||
unsigned char val = (unsigned char)(*it);
|
||||
if(fSpaces && it != itbegin)
|
||||
rv.push_back(' ');
|
||||
rv.push_back(hexmap[val>>4]);
|
||||
rv.push_back(hexmap[val&15]);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline std::string HexStr(const T& vch, bool fSpaces=false)
|
||||
{
|
||||
return HexStr(vch.begin(), vch.end(), fSpaces);
|
||||
}
|
||||
/**
|
||||
* Convert a span of bytes to a lower-case hexadecimal string.
|
||||
*/
|
||||
std::string HexStr(const Span<const uint8_t> s);
|
||||
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
|
||||
|
||||
/**
|
||||
* Format a paragraph of text to a fixed width, adding spaces for
|
||||
|
@ -42,7 +42,7 @@ void CheckUniqueFileid(const BerkeleyEnvironment& env, const std::string& filena
|
||||
for (const auto& item : env.m_fileids) {
|
||||
if (fileid == item.second && &fileid != &item.second) {
|
||||
throw std::runtime_error(strprintf("BerkeleyBatch: Can't open database %s (duplicates fileid %s from %s)", filename,
|
||||
HexStr(std::begin(item.second.value), std::end(item.second.value)), item.first));
|
||||
HexStr(item.second.value), item.first));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ std::string static EncodeDumpString(const std::string &str) {
|
||||
std::stringstream ret;
|
||||
for (unsigned char c : str) {
|
||||
if (c <= 32 || c >= 128 || c == '%') {
|
||||
ret << '%' << HexStr(&c, &c + 1);
|
||||
ret << '%' << HexStr(Span<const unsigned char>(&c, 1));
|
||||
} else {
|
||||
ret << c;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user