static unsigned long hashpjw(char *arKey, unsigned int nKeyLength) { unsigned long h = 0, g; char *arEnd=arKey+nKeyLength;
while (arKey < arEnd) { h = (h << 4) + *arKey++; if ((g = (h & 0xF0000000))) { h = h ^ (g >> 24); h = h ^ g; } }
return h; }
●OpenSSL中出现的字符串Hash函数
unsigned long lh_strhash(char *str) { int i,l; unsigned long ret=0; unsigned short *s;
if (str == NULL) return(0); l=(strlen(str)+1)/2; s=(unsigned short *)str;
for (i=0; i<l; i++) ret^=(s[i]<<(i&0×0f));
return(ret); }
/* The following hash seems to work very well on normal text strings * no collisions on /usr/dict/words and it distributes on %2^n quite * well, not as good as MD5, but still good. */ unsigned long lh_strhash(constchar *c) { unsigned long ret=0; long n; unsigned long v; int r;
#else /* * Fowler/Noll/Vo hash * * The basis of the hash algorithm was taken from an idea sent by email to the * IEEE Posix P1003.2 mailing list from Phong Vo (kpv@research.att.com) and * Glenn Fowler (gsf@research.att.com). Landon Curt Noll (chongo@toad.com) * later improved on their algorithm. * * The magic is in the interesting relationship between the special prime * 16777619 (2^24 + 403) and 2^32 and 2^8. * * This hash produces the fewest collisions of any function that we’ve seen so * far, and works well on both numbers and strings. */