I'm looking to do a custom hash table implementation in C. Is there an MD5/SHA1 hash function already in the GNU library or do I have to use an external library for this?
Here's kinda what I'm looking for:
int hashValue;
hashValue = MD5_HASH(valToHash);
gcrypt and openssl can do MD5, SHA and other hashes here's an example with libgcrypt:
#include <gcrypt.h>
#include <stdio.h>
// compile gcc md5_test.c -lgcrypt
int main(int argc, char *argv[])
{
unsigned char digest[16];
char digest_ascii[32+1] = {0,};
int digest_length = gcry_md_get_algo_dlen (GCRY_MD_MD5);
int i;
printf("hashing=%s len=%d\n", argv[1], digest_length);
gcry_md_hash_buffer(GCRY_MD_MD5, digest, argv[1], strlen(argv[1]));
for (i=0; i < digest_length; i++) {
sprintf(digest_ascii+(i*2), "%02x", digest[i]);
}
printf("hash=%s\n", digest_ascii);
}
`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With