Implementation of several classic hash algorithms (source code)

Source: Internet
Author: User

SOURCE statement: http://blog.minidx.com/2008/01/27/446.html

Save it for later research, and now you can not understand!

The hashing algorithm maps a binary value of any length to a small, fixed-length binary value, a small binary value called a hash value. A hash value is a unique and extremely compact numeric representation of a piece of data. If you hash a clear text and even change only one letter of the paragraph, subsequent hashes will produce different values. To find two different inputs that hash the same value, it is not possible to compute, so the hash value of the data can verify the integrity of the data.

The time efficiency of the list lookup is O (N), the binary method is log2n,b+ tree is log2n, but the time efficiency of hash list lookup is O (1).

Design efficient algorithms often need to use a hash list, constant-level search speed is unmatched by any other algorithm, the structure of the hash list and the different methods of conflict implementation of course have a certain impact on efficiency, but the hash function is the core of the hash list of parts, The following is a few classic software used in the string hash function implementation, by reading these code, we can in the hash algorithm execution efficiency, dispersion, space utilization and other aspects have a more profound understanding.

The following sections describe the string hash functions that appear in several classic software.

String hash function appearing in PHP

StaticUnsignedLongHASHPJW (Char*arkey, unsignedintnkeylength) {unsignedLongh =0, G;Char*arend=arkey+nkeylength; while(Arkey <Arend) {h= (h <<4) + *arkey++;if(g = (H &0xf0000000)) ) {h= h ^ (g >> -); H= h ^g;}}returnh;}

The string hash function appearing in OpenSSL

UnsignedLongLh_strhash (Char*str) {inti,l;unsignedLongret=0; unsigned Short*s;if(str = = NULL)return(0); L= (strlen (str) +1)/2; s= (unsigned Short*) str;  for(i=0; Iret^= (s[i]<< (i&0x0f));return(ret);} /*The following hash seems to work very well on normal text strings * No collisions on/usr/dict/words and it distribut ES on%2^n quite * well, not as good as MD5, but still good. */unsignedLongLh_strhash (Const Char*c) {unsignedLongret=0;Longn;unsignedLongv;intR;if((c = = NULL) | | (*c = =' /'))return(ret);/*unsigned char b[16]; MD5 (C,strlen (c), b); Return (b[0]| ( B[1]<<8) | (b[2]<<16) | (b[3]<<24)); */N=0x100; while(*c) {v=n| (*c); n+=0x100; R= (int) ((v>>2) ^v) &0x0f; RET= (Ret ( +-R)); RET&=0xFFFFFFFFL; RET^=v*v;c++;} return((ret>> -)^ret);}

String hash function appearing in MySQL

#ifndef new_hash_function/*Calc HashValue for a key*/Static UINTCALC_HASHNR (Const byte*key,UINTlength) {RegisterUINTNr=1, nr2=4;  while(length--) {nr^= (((Nr & the) * ((+NR2) * (UINT) (Uchar) *key++) + (nr <<8); NR2+=3;} return((UINT) (NR);} /*Calc HashValue for a key, case indepenently*/Static UINTCalc_hashnr_caseup (Const byte*key,UINTlength) {RegisterUINTNr=1, nr2=4;  while(length--) {nr^= (((Nr & the) * ((+NR2) * (UINT) (Uchar) ToUpper (*key++)) + (NR <<8); NR2+=3;} return((UINT) (NR);}#else/** FOWLER/NOLL/VO Hash * * The basis of the hash algorithm is taken from a idea sent by email to the * IEEE Posix P1 003.2 mailing list from Phong Vo ([email protected]) and * Glenn Fowler ([email protected]). Landon Curt Noll ([email protected]) * 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 all function that we ' ve seen so * far, and works well on both numbers and Strings. */UINTCALC_HASHNR (Const byte*key,UINTLen) {Const byte*end=key+Len;UINTHash; for(hash =0; Key < end; key++) {Hash*=16777619; hash^= (UINT) * (uchar*) key;} return(hash);} UINTCalc_hashnr_caseup (Const byte*key,UINTLen) {Const byte*end=key+Len;UINTHash; for(hash =0; Key < end; key++) {Hash*=16777619; hash^= (UINT) (UCHAR) toupper (*key);} return(hash);}#endif

The string hash function in MySQL is also distinguished by the casing

Another classic string hash function

int Hash (char *intChar *for (h=0 Char *) str; *p; p++ * H + *return  h;}

Implementation of several classic hash algorithms (source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.