How to hash std :: string?

I am doing a little utility to help me remember passwords by repetition. I would like to enter a password that needs to be remembered only once every day, and not before each session. Of course, I would not have stored the password myself, but I would have gladly saved its hash.

So what are the easiest ways to get the hash from std::string using the standard C ++ library?

+4
source share
4 answers

You can use one of the SHA functions from the OpenSSL library http://www.openssl.org/docs/crypto/sha.html#

+5
source

For a quick solution without external libraries, you can use hash<std::string> for the string s hash. It is determined by the inclusion of hash_map or unordered_map (or some other) header files.

 #include <string> #include <unordered_map> hash<string> hasher; string s = "heyho"; size_t hash = hasher(s); 

If you decide that you want the added SHA security, you do not need to download the large Crypto ++ library unless you need all its other functions; There are many standalone implementations on the Internet, just search for "sha implementation C ++".

+15
source

using C ++ 11, you can:

 #include <string> #include <unordered_map> std::size_t h1 = std::hash<std::string>{}("MyString"); std::size_t h2 = std::hash<double>{}(3.14159); 

more details here .

+3
source

You can use the STL hash functor . See if you have STL lib.

Note that this returns size_t , so the range is numeric_limits<size_t>::min() numeric_limits<size_t>::max() . You will have to use SHA or something if this is unacceptable.

+1
source

Source: https://habr.com/ru/post/1379895/


All Articles