hex to checksum256
eosio::checksum256 hex_to_checksum256(const std::string& in) {
eosio::check(in.length() == 64, "checksum size is error");
std::string hex{"0123456789abcdef"};
eosio::checksum256 out;
for (int i = 0; i < 32; i++) {
auto d1 = hex.find(in[2 * i]);
auto d2 = hex.find(in[2 * i + 1]);
eosio::check(d1 != std::string::npos || d2 != std::string::npos,
"invalid sha256");
// checksum256 is composed of little endian int128_t
reinterpret_cast<char*>(out.data())[i / 16 * 16 + 15 - (i % 16)] =
(d1 << 4) + d2;
}
return out;
}
checksum256 to hex string
static string to_hex(const checksum256 &hashed) {
// Construct variables
string result;
const char *hex_chars = "0123456789abcdef";
const auto bytes = hashed.extract_as_byte_array();
// Iterate hash and build result
for (uint32_t i = 0; i < bytes.size(); ++i) {
(result += hex_chars[(bytes.at(i) >> 4)]) += hex_chars[(bytes.at(i) & 0x0f)];
}
// Return string
return result;
}
参考
https://github.com/EOSIO/eos/issues/4012
版权属于:区块链中文技术社区 / 转载原创者
本文链接:https://bcskill.com/index.php/archives/967.html
相关技术文章仅限于相关区块链底层技术研究,禁止用于非法用途,后果自负!本站严格遵守一切相关法律政策!