Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::hash algorithm and size

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++ ?

like image 723
Ælex Avatar asked Dec 25 '22 17:12

Ælex


1 Answers

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:

  1. Accepts a single parameter of type Key.
  2. Returns a value of type size_t that represents the hash value of the parameter.
  3. Does not throw exceptions when called.
  4. For two parameters k1 and k2 that are equal, std::hash<Key>()(k1) == std::hash<Key>()(k2).
  5. 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().
like image 179
Jack Avatar answered Jan 06 '23 22:01

Jack