Is there a simple C or C++ function that calculates the sha1 hash of a string?
Solution 1
Not built-in. Try openssl's crypto library.
(https://www.openssl.org/source/)
(https://github.com/openssl/openssl/blob/master/include/openssl/sha.h)
(https://www.openssl.org/docs/man1.1.0/crypto/SHA1.html)
#include <openssl/sha.h>
int main()
{
const unsigned char str[] = "Original String";
unsigned char hash[SHA_DIGEST_LENGTH]; // == 20
SHA1(str, sizeof(str) - 1, hash);
// do some stuff with the hash
return 0;
}
Link with -lssl
, which will imply -lcrypto
. If you are linking statically you might need to link both.
Solution 2
CryptoPP is a great C++ library for cryptographic functions. It has a method for calculating a SHA1 digest. See examples of the hashing functions here.
Solution 3
Here's an example: http://www.codeproject.com/KB/recipes/csha1.aspx#csha1is
Also, this question was already addressed on this thread. They have a link for some further help. Check it out.
Solution 4
Cory
Updated on December 02, 2020Comments
-
Cory over 3 years
Possible Duplicate:
sha1 function in cpp (C++)Hi,I was just looking for a function that calculates the sha1 hash of string and returns the result.
-
ntc2 over 10 yearsFYI: need to link with
-lcrypto
to compile. E.g.gcc example.c -lcrypto
. -
ntc2 over 10 yearsAlso: the package to install on Debian based GNU/Linuxes is
libssl-dev
. -
stakx - no longer contributing almost 8 yearsInstead of just linking to the Boost home page, could you link to the library that you claim exists, or at least mention that library's name?