0

I'm trying to cast checksum256 to uint64_t

Here is conversion code:

uint64_t checksum_to_uint64(const eosio::checksum256 & digest) {
        auto checksum_bytes = digest.extract_as_byte_array().data();
        uint64_t result = reinterpret_cast<uint64_t>(&checksum_bytes);
        return result;
}

But when I try to pass hashes of strings of the same length there, this function produces the same numbers

example:

sha256(d83631286d922f747aaee529d86cb566) converts to 49 sha256(7dc0cc92aa96cf8e8909cbe5468c1b4d) converts to 49 sha256(b97280a2a88868684dca726570435c96) converts to 49

Can anybody explain this to me

  • The type of checksum_bytes is uint8_t*, and you're trying to convert uint8_t** (&checksum_bytes) to uint64_t. – conr2d Feb 7 at 17:12
0

Don't know where the 49 comes from but you are doing a reinterpret_cast from std::array<uint8_t,32> which is 256 bit to uint64_t which is 64 bit - this doesn't not seem like a good idea in principle.

Are these values real, tested examples?

sha256(d83631286d922f747aaee529d86cb566) converts to 49 sha256(7dc0cc92aa96cf8e8909cbe5468c1b4d) converts to 49 sha256(b97280a2a88868684dca726570435c96) converts to 49

To ensure to know what's happening in the background, I'd personally (except I wouldn't truncate a 256-bit hash to 64 bits) use memcpy for what you want to achieve.

auto checksum_bytes = digest.extract_as_byte_array().data();
uint64_t x;
std::memcpy(&x, &checksum_bytes, sizeof(x));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.