Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a glibc hash function?

Tags:

c

linux

hash

md5

gnu

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);
like image 412
themaestro Avatar asked Oct 14 '10 18:10

themaestro


1 Answers

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);
}

`

like image 198
karl Avatar answered Sep 22 '22 07:09

karl