std::hash algorithm and size

algorithm, c++, c++11, hash, stdhash

Solution

The algorithm chosen for `std::hash` is solely implementation dependant. Probably neither MD5 or SHA are used since they would be performance killers for its purpose.

Most implementation will be much more trivial than the above mentioned since there is no cryptographic requirement for `std::hash` while MD5 and SHA have been developed for cryptographic purposes.

The requirements of `std::hash` are much less strict:

- Accepts a single parameter of type `Key`.

- Returns a value of type `size_t` that represents the hash value of the parameter.

- Does not throw exceptions when called.

- For two parameters `k1` and `k2` that are equal, `std::hash<Key>()(k1) == std::hash<Key>()(k2)`.

- For two different parameters `k1` and `k2` that are not equal, the probability that `std::hash<Key>()(k1) == std::hash<Key>()(k2)` should be very small, approaching `1.0/std::numeric_limits<size_t>::max()`.

Problem

I'm using C++11 and the std::hash algorithm. I was wondering, what actual hash implementation is used? I would assume MD5 or SHA, but I can't dig any info from the internets. Also, I'd like to know the actual returned bit-width of the hash, as I have to store it in MySQL. Finally, is it preferable to use std::hash, over say some other library such as crypto++ ?

Original source