一些常用的hash方法
unsigned int RSHash(const std::string& str)
{
unsigned int b = 378551;
unsigned int a = 63689;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash * a + str[i];
a = a * b;
}
return hash;
}
/* End Of RS Hash Function */
unsigned int JSHash(const std::string& str)
{
unsigned int hash = 1315423911;
for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str[i] + (hash >> 2));
}
return hash;
}
/* End Of JS Hash Function */
unsigned int PJWHash(const std::string& str)
{
unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
unsigned int hash = 0;
unsigned int test = 0;
for(std::size_t 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 */
unsigned int ELFHash(const std::string& str)
{
unsigned int hash = 0;
unsigned int x = 0;
for(std::size_t 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 */
unsigned int BKDRHash(const std::string& str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str[i];
}
return hash;
}
/* End Of BKDR Hash Function */
unsigned int SDBMHash(const std::string& str)
{
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = str[i] + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
/* End Of SDBM Hash Function */
unsigned int DJBHash(const std::string& str)
{
unsigned int hash = 5381;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = ((hash << 5) + hash) + str[i];
}
return hash;
}
/* End Of DJB Hash Function */
unsigned int DEKHash(const std::string& str)
{
unsigned int hash = static_cast<unsigned int>(str.length());
for(std::size_t i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str[i];
}
return hash;
}
/* End Of DEK Hash Function */
unsigned int BPHash(const std::string& str)
{
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash = hash << 7 ^ str[i];
}
return hash;
}
/* End Of BP Hash Function */
unsigned int FNVHash(const std::string& str)
{
const unsigned int fnv_prime = 0x811C9DC5;
unsigned int hash = 0;
for(std::size_t i = 0; i < str.length(); i++)
{
hash *= fnv_prime;
hash ^= str[i];
}
return hash;
}
/* End Of FNV Hash Function */
unsigned int APHash(const std::string& str)
{
unsigned int hash = 0xAAAAAAAA;
for(std::size_t i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str[i] * (hash >> 3)) :
(~((hash << 11) + (str[i] ^ (hash >> 5))));
}
return hash;
}
/* End Of AP Hash Function */
from:
http://www.cnblogs.com/luxiaoxun/archive/2012/08/06/2625659.html
分享到:
相关推荐
RS-Hash Function Value: " + ghl.RSHash(key)); System.out.println(" 2. JS-Hash Function Value: " + ghl.JSHash(key)); System.out.println(" 3. PJW-Hash Function Value: " + ghl.PJWHash(key)); System....
关于hash函数的ppt
Hash函数和数字签名 Hash函数和数字签名 Hash函数和数字签名
hash函数与消息认证讲义 包括 5.1 Hash函数概述 5.1.1 Hash函数定义 5.1.2 Hash函数的安全性 5.1.3 Hash函数的迭代构造法 5.2 Hash函数MD5 5.2.1 MD5算法 5.2.2 MD5的安全性 5.3 安全Hash算法SHA-1 5.3.1 SHA-1...
Hash函数代码,便于结合原理与实践。你可以下载后学习,更希望你能分享出更好的代码
Hash函数集合,包含主流的hash函数: nginx_hash算法,OpenSSL_hash算法,RSHash,JSHash,PJWHash,ELFHash,BKDRHash,DJBHash,DEKHash,APHash等等!
Hash函数研究综述 一篇非常不错的关于哈希函数的研究综述 07年发表的
Hash函数及数字签名应用实验 验证码的实现过程
HASH函数,用来提取摘要160bits。HASH函数,用来提取摘要160bits。HASH函数,用来提取摘要160bits。
该函数可以在输入Hash_QueryParameters.txt文件下,每行为一个数据(可以随便写)输入哈希表中,可以查找,删除,插入等操作
hash函数实例 c语言版 不喜勿喷!
该文档中包含 bloomFilter过滤器中用到的对于字符串进行hash的hash函数共十一个,并带有测试程序..
这是清华大学王老师报告PPT,介绍了密码HASH函数上研究的最新成果
本文主要介绍Hash函数的设计优化,包括数字、字符串、排列等,并给出相关的代码。
HAsh函数的加密算法将的很浅显,但是很实用,适合初学者和有编程经验的人参阅,收获很大
提出了一种基于可并行和变参数的混沌分段线性映射hash函数算法。该函数通过明文扩展将并行处理的明文消息矩阵元素信息关联起来,实现了并行性。由矩阵元素位置标号决定的可变参数和矩阵元素相应的ASCII码值分别作为...
hash函数 的 c语言版 大家可以试试 不喜勿喷!
HASH函数及其应用_朱全民.ppt
uthash开源的hash函数实现,里面的uthash.h可用
所有有关HASH函数的论文,hash,安全散列算法,值得参考有几十篇关于密码学的论文