4

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

1 Answer 1

10

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().
Sign up to request clarification or add additional context in comments.

13 Comments

You have to understand that the uniqueness provided by std::hash nothing has to do with cryptographic security. Its implementation is mostly used with associative unordered containers like map. The implementation doesn't care about cryptographic, it cares about efficiency when used to manage hash tables. If you need cryptographic security then you should really point toward an external library meant to be used for that purpose.
What are you talking about? Collisions are expected to happen with any hash table!
"I'm extensively using std::hash for creating unique identifiers." I'm taking this as: "I want a library to generate UUIDs". That's not std::hash.
@Alex, @peppe. I agree, you want a UID type, such as a standards-defined UUID, implemented in several libaries, including boost::uuid.
@Alex, UUIDs implement SHA and MD5 hashes of string-data, as well as completely pseudo-random generated, if you're interested. Read-up on UUIDs
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.