Several well-known Hash Algorithms

Source: Internet
Author: User

/*
**************************************** **********************************
**
* General Purpose hash function algorithms library *
**
* Author: Arash partow-2002 *
* URL: http://www.partow.net *
* URL: http://www.partow.net/programming/hashfunctions/index.html *
**
* Copyright notice :*
* Free use of the general purpose hash function algorithms library is *
* Permitted under the Guidelines and in accordance with the most current *
* Version of the common public license .*
* Http://www.opensource.org/licenses/cpl.php *
**
**************************************** **********************************
*/

Class generalhashfunctionlibrary
{

Public long rshash (string Str)
{
Int B = 378551;
Int A = 63689;
Long hash = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = hash * A + Str. charat (I );
A = A * B;
}

Return hash;
}
/* End of RS hash function */

Public long jshash (string Str)
{
Long hashing = 1315423911;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash ^ = (hash <5) + Str. charat (I) + (hash> 2 ));
}

Return hash;
}
/* End of JS hash function */

Public long pjwhash (string Str)
{
Long bitsinunsignedint = (long) (4*8 );
Long threequarters = (long) (bitsinunsignedint * 3)/4 );
Long oneeighth = (long) (bitsinunsignedint/8 );
Long highbits = (long) (0 xffffffff) <(bitsinunsignedint-oneeighth );
Long hash = 0;
Long test = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = (hash <oneeighth) + Str. charat (I );

If (test = hash & highbits )! = 0)
{
Hash = (hash ^ (test> threequarters ))&(~ Highbits ));
}
}

Return hash;
}
/* End of P. J. Weinberger hash function */

Public long elfhash (string Str)
{
Long hash = 0;
Long x = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = (hash <4) + Str. charat (I );

If (x = hash & 0xf0000000l )! = 0)
{
Hash ^ = (x> 24 );
}
Hash & = ~ X;
}

Return hash;
}
/* End of ELF hash function */

Public long bkdrhash (string Str)
{
Long seed = 131; // 31 131 1313 13131 131313 Etc ..
Long hash = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = (hash * seed) + Str. charat (I );
}

Return hash;
}
/* End of bkdr hash function */

Public long sdbmhash (string Str)
{
Long hash = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = Str. charat (I) + (hash <6) + (hash <16)-Hash;
}

Return hash;
}
/* End of sdbm hash function */

Public long djbhash (string Str)
{
Long hashing = 5381;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = (hash <5) + hash) + Str. charat (I );
}

Return hash;
}
/* End of djb hash function */

Public long dekhash (string Str)
{
Long hash = Str. Length ();

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = (hash <5) ^ (hash> 27) ^ Str. charat (I );
}

Return hash;
}
/* End of Dek hash function */

Public long bphash (string Str)
{
Long hash = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash = hash <7 ^ Str. charat (I );
}

Return hash;
}
/* End of BP hash function */

Public long fnvhash (string Str)
{
Long fnv_prime = 0x811c9dc5;
Long hash = 0;

For (INT I = 0; I <Str. Length (); I ++)
{
Hash * = fnv_prime;
Hash ^ = Str. charat (I );
}

Return hash;
}
/* End of FNV hash function */

Public long aphash (string Str)
{
Long hash = 0 xaaaaaaaa;

For (INT I = 0; I <Str. Length (); I ++)
{
If (I & 1) = 0)
{
Hash ^ = (hash <7) ^ Str. charat (I) ^ (hash> 3 ));
}
Else
{
Hash ^ = (~ (Hash <11) ^ Str. charat (I) ^ (hash> 5 )));
}
}

Return hash;
}
/* End of AP hash function */

}
 

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.