I need to calculate the hash of a large string in windows and linux, and the result should be the same for both OS.
For a simple test code, I get different hashes for windows and linux using std::hash. This makes sense, since the actual implementation of std::hash for each compiler might use different algorithms.
Which brings the question: Is there a way to achieve this using the standard library?
The more straight forward answer for me is to implement my own hash algorithm, so its the same for both OS. But this seems like an overkill. I don't want to reinvent the wheel.
std::hash? Or could you just use a third-party MD5 or SHA-# library?std::hashreally is not a versatile hashing algo. It's purpose in life is to provide hashing machinery to unordered containers with life scope limited to program runs (and accessible from the same program). Because of that, it has no guarantees of compatibility between different compilers (or, in fact, between different runs of the same program!). You have no other option but to write your own hashing algo.std::hashyou can't even rely on the hash value being the same between different runs on the same platform. The Standard only guarantees thatstd::hashreturns the same value for the current invocation of the program. This is to allow implementers ofstd::hashto use a salted-hash. "...Hash functions are only required to produce the same result for the same input within a single execution of a program; this allows salted hashes that prevent collision denial-of-service attacks...." en.cppreference.com/w/cpp/utility/hash