开发者

Is there a glibc hash function?

I'm looking to do a custom hash table implementation in C. Is there an开发者_如何学Go 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);


You can take a look at Bob Jenkin's survey and analysis of many hash functions:

  • http://www.burtleburtle.net/bob/hash/doobs.html

Or just drop his lookup3 routines (which he's put into the public domain) into your project:

  • http://www.burtleburtle.net/bob/c/lookup3.c


For a hash table, you do not need cryptographic strength, only good randomization properties. Broken cryptographic hash functions (like MD5) are fine for that, but you may want to use MD4, which is both faster and simpler, to the point that you could simply include an implementation directly in your code. It is not difficult to rewrite it from the specification (and since you want only a function for a hash table, it is not really a problem if you get it wrong at some point). Shameless plug: there is an optimized C implementation of MD4 in sphlib.


Unless you already have a good reason for using MD5, you may want to reconsider. What makes for a "good" hash function in a hash table is pretty dependent on what you're trying to accomplish. You may want to read the comments in Python's dictobject.c to see the sorts of tradeoffs others have made.


There are a few trusted, simple versions available -- I have a few in the sources of the digest for R. Here is what I wrote in the DESCRIPTION file:

Description: The digest package provides functions for the creation of `hash' digests of arbitrary R objects using the md5, sha-1, sha-256 and crc32 algorithms permitting easy comparison of R language objects. The md5 algorithm by Ron Rivest is specified in RFC 1321, the SHA-1 and SHA-256 algorithms are specified in FIPS-180-1 and FIPS-180-2, and the crc32 algorithm is described in
ftp://ftp.rocksoft.com/cliens/rocksoft/papers/crc_v3.txt. For md5, sha-1 and sha-256, this packages uses small standalone implementations that were provided by Christophe Devine. For crc32, code from the zlib library is used.

I think some of Christophe's code is no longer at cr0.net, but searches should lead you to several other projects incorporating it. His file headers were pretty clear:

/*                                                   
 * FIPS-180-1 compliant SHA-1 implementation,   
 * by Christophe Devine <devine@cr0.net>;   
 * this program is licensed under the GPL.  
 */     

and his code matches the reference output.


Glibc's crypt() uses a MD5 based algorhytm if salt starts with $1$. But since you mention that you are going to do a hash table implementation, maybe Jenkins hash would be more appropiate.


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

`


The OpenSSL library has all the crypto routines you could ever want, including cryptographic hashes.


Murmur3 is a fast noncryptographic algorithm that you can use.

A good speed comparation of murmur against other algorithms can be found in this thread https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed

One possible implementation: https://github.com/PeterScott/murmur3

Example:

uint32_t hash;
uint32_t seed = 42;
char* input = "HelloWorld";

MurmurHash3_x86_32(input, strlen(input), seed, &hash);
printf("x86_32:  %08x\n", hash);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜