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;
}