diff options
author | dbear496 <70439542+dbear496@users.noreply.github.com> | 2024-09-14 17:44:18 -0400 |
---|---|---|
committer | dbear496 <70439542+dbear496@users.noreply.github.com> | 2024-09-14 18:15:32 -0400 |
commit | 4f4cc1688a8e8a7c03f18603cc5a4159d9c89ca3 (patch) | |
tree | 9587fe9bb36ba175bbeaa4acc2a435fba805d090 /src/utils.h | |
parent | ad93c185644cfcb14fe4a673e74a0cb5c954a4b4 (diff) | |
download | fork-ledger-4f4cc1688a8e8a7c03f18603cc5a4159d9c89ca3.tar.gz fork-ledger-4f4cc1688a8e8a7c03f18603cc5a4159d9c89ca3.tar.bz2 fork-ledger-4f4cc1688a8e8a7c03f18603cc5a4159d9c89ca3.zip |
promote compatability with Boost <1.86.0
Diffstat (limited to 'src/utils.h')
-rw-r--r-- | src/utils.h | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/src/utils.h b/src/utils.h index 09dd0993..e09c603d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -578,29 +578,39 @@ inline int peek_next_nonws(std::istream& in) { *_p = '\0'; \ } -inline string to_hex(unsigned char * message_digest, const int len = 4) -{ +inline string digest_to_hex( + const boost::uuids::detail::sha1::digest_type& message_digest, + size_t len = sizeof(boost::uuids::detail::sha1::digest_type) * 2 +) { std::ostringstream buf; - - for(int i = 0; i < 20 ; i++) { - buf.width(2); - buf.fill('0'); - buf << std::hex << (int)message_digest[i]; - if (i + 1 >= len) - break; // only output the first LEN dwords + buf.setf(std::ios_base::hex, std::ios_base::basefield); + buf.fill('0'); + + // sha1::digest_type is an array type and may change between Boost versions + const size_t count = std::min( + sizeof(message_digest) / sizeof(message_digest[0]), + (len - 1) / (sizeof(message_digest[0]) * 2) + 1 + ); + for(size_t i = 0; i < count; i++) { + buf.width(sizeof(message_digest[i]) * 2); + buf << (unsigned int)message_digest[i]; } - return buf.str(); + string hex = buf.str(); + hex.resize(len, '0'); // in case a partial element is requested + return hex; } -inline string sha1sum(const string& str) -{ +inline string sha1sum( + const string& str, + size_t len = sizeof(boost::uuids::detail::sha1::digest_type) * 2 +) { boost::uuids::detail::sha1 sha; + boost::uuids::detail::sha1::digest_type message_digest; + sha.reset(); sha.process_bytes(str.c_str(), str.length()); - - unsigned char message_digest[20]; sha.get_digest(message_digest); - return to_hex(message_digest, 20); + return digest_to_hex(message_digest, len); } extern const string version; |