xxhash_cpp icon indicating copy to clipboard operation
xxhash_cpp copied to clipboard

Hash output reversed

Open lexnicolaes opened this issue 5 years ago • 1 comments

Hi,

I'm attempting to implement xxhash_cpp in a project, where unsurprisingly, I need to generate the xxhash64 for a file and check it against a known hash string. But I've run into the problem that my output hash is reversed.

Output from QuickHash: 9FD684E536C4C0B9 Output from xxhsum: 9fd684e536c4c0b9

Output from my implementation B9C0C436E584D69F

This seems like an endianness problem, but I can't see any option for setting a specific endianness.

What would be the best strategy to to an equality check for the known hash string?

  1. Create hash, convert to hex string and check
  2. Somehow convert my hash string to a hash64_t object and compare with the hash object created from the file

My current implementation:

  std::string filename = "/my_input_file";
  std::ifstream filestream(filename, std::ifstream::in | std::istream::binary);
  xxh::hash_state64_t hash_stream;
  std::vector<uint8_t> contents((std::istreambuf_iterator<char>(filestream)), std::istreambuf_iterator<char>());
  hash_stream.update(contents);

  xxh::hash64_t final_hash = hash_stream.digest();
  std::cout << byte_print(final_hash) <<std::endl;

lexnicolaes avatar Jul 10 '20 11:07 lexnicolaes

What is most likely happenning is that QuickHash and xxhsum give you a canonical representation of the hash (big endian), where as xxhash_cpp is returning a native represenation (little endian). xxhash_cpp does have a xxh::canonical_t for canonical representations, but for the time being it's just a struct that has to be memcmp'd with the hashes from other hashing algorithms.

TODO: Make a helper function to convert the usual output of the hash to a canonical representation while still keeping it as an integer

redspah avatar Jul 12 '20 18:07 redspah

Added a from_canonical function with the new release.

Updated to 0.8.1. Closing.

redspah avatar Nov 10 '22 01:11 redspah