GeneralHashFunctionLibrary

General Purpose Hash Function Algorithms Test By Arash Partow - 2002"

Converted from Java to C# By Iggy P. - 2009

GeneralHashFunctionLibrary.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HashFunctionz
{
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[i];
a = a * b;
}
return hash;
}
/* End Of RS Hash Function */
public long JSHash(string str)
{
long hash = 1315423911;
for (int i = 0; i < str.Length; i++)
{
hash ^= ((hash << 5) + str[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)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
//    long hash = 0;
//    long test = 0;
//    for (int i = 0; i < str.Length; i++)
//    {
//        hash = (hash << OneEighth) + str[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[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[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[i] + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
/* End Of SDBM Hash Function */
public long DJBHash(string str)
{
long hash = 5381;
for (int i = 0; i < str.Length; i++)
{
hash = ((hash << 5) + hash) + str[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[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[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[i];
}
return hash;
}
///* End Of FNV Hash Function */
public long APHash(string str)
{
long hash = 0xAAAAAAAA;
for (int i = 0; i < str.Length; i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ str[i] * (hash >> 3));
}
else
{
hash ^= (~((hash << 11) + str[i] ^ (hash >> 5)));
}
}
return hash;
}
/* End Of AP Hash Function */
}
}

HashTest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HashFunctionz
{
public class GeneralHashTest
{
public static void Main(string [] args)
{
GeneralHashFunctionLibrary ghl = new GeneralHashFunctionLibrary();
//string key = "abcdefghijklmnopqrstuvwxyz1234567890";
Console.WriteLine("General Purpose Hash Function Algorithms Test");
Console.WriteLine("Enter alpha-numeric key, then press 'ENTER' to calculate hash code:");
string key = "";
key = Console.ReadLine();
Console.WriteLine("General Purpose Hash Function Algorithms Test");
Console.WriteLine("By Arash Partow - 2002");
Console.WriteLine("Converted from Java to C# By Iggy P. - 2009\n");
Console.WriteLine("Key: " + key);
Console.WriteLine(" 1. RS-Hash Function Value:   " + ghl.RSHash(key));
Console.WriteLine(" 2. JS-Hash Function Value:   " + ghl.JSHash(key));
Console.WriteLine(" 3. PJW-Hash Function Value:  " + ghl.PJWHash(key));
Console.WriteLine(" 4. ELF-Hash Function Value:  " + ghl.ELFHash(key));
Console.WriteLine(" 5. BKDR-Hash Function Value: " + ghl.BKDRHash(key));
Console.WriteLine(" 6. SDBM-Hash Function Value: " + ghl.SDBMHash(key));
Console.WriteLine(" 7. DJB-Hash Function Value:  " + ghl.DJBHash(key));
Console.WriteLine(" 8. DEK-Hash Function Value:  " + ghl.DEKHash(key));
Console.WriteLine(" 9. BP-Hash Function Value:   " + ghl.BPHash(key));
Console.WriteLine(" 9. FNV-Hash Function Value:  " + ghl.FNVHash(key));
Console.WriteLine("10. AP-Hash Function Value:   " + ghl.APHash(key));
Console.WriteLine("Press 'ENTER' to exit...");
key = Console.ReadLine();
}
}
}

I just got to be distinctly aware of your blog through Google, and found that it's genuinely informative. I will watch your writing skill and ideas. I will acknowledge on the off chance that you proceed with this in future. Various individuals will be profited from your writing.
[ Reply ]

Analysis techniques such a Poisson dissemination can be utilized to investigate the impact rates of various hash capacities for various gatherings of information. In general, there is a hypothetical hash work known as the ideal hash work for any gathering of information. The ideal hash work by definition expresses that no impacts will happen meaning no rehashing hash values will arise from different elements of the group.
[ Reply ]

The general hash functional library coding was difficult for me as there were bugs in it and I can't figure it out but now I came to this website and copy the all content which they have shared and guess what, this code is working properly.
[ Reply ]

The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!
[ Reply ]

Paloalto Networks Paloalto Networks ACE Certification certification lead you to numerous opportunities in career development and shaping your future. Dumpsleader is your ally in achieving your targeted Paloalto Networks ACE Certification certification, providing you easy and interactive PCNSE7 exam dumps.
[ Reply ]

This blog is extremely cool, I'm lucky to the point that I have come to here and got this magnificent data
[ Reply ]

I am very happy to visit in the blog. In the blog is very interesting and informative for me. Some people visited to here in the blog review is very good. Thanks for sharing information. keep it up.
[ Reply ]

You have posted a great tutorial which i like it a lot.I must say you have made a great effort in making this post which i appreciate it a lot.
[ Reply ]

Great informative site. I am really impressed after reading this blog post. I really appreciate the time and effort you spend to share this with us! I do hope to read more updates from you. Thanks and keep it up
[ Reply ]

I have just found this website while surfing over the internet.You have shared very knowledgeable tutorial which i like reading.Keep posting more similar articles.
[ Reply ]

Previous page 1 2 Next page
12 comments

Comments

Write the captcha code you are seeing.
Comment XML feeds: RSS | Atom