**
* Hash算法大全<br>
* 推荐使用FNV1算法
* @algorithm None
* @author Goodzzp 2006-11-20
* @lastEdit Goodzzp 2006-11-20
* @editDetail Create
*/
public class HashAlgorithms
{
/**//**
* 加法hash
* @param key 字符串
* @param prime 一个质数
* @return hash结果
*/
public static int additiveHash(String key, int prime)
{
int hash, i;
for (hash = key.length(), i = 0; i < key.length(); i++)
hash += key.charAt(i);
return (hash % prime);
}
/**//**
* 旋转hash
* @param key 输入字符串
* @param prime 质数
* @return hash值
*/
public static int rotatingHash(String key, int prime)
{
int hash, i;
for (hash=key.length(), i=0; i<key.length(); ++i)
hash = (hash<<4)^(hash>>28)^key.charAt(i);
return (hash % prime);
// return (hash ^ (hash>>10) ^ (hash>>20));
}
// 替代:
// 使用:hash = (hash ^ (hash>>10) ^ (hash>>20)) & mask;
// 替代:hash %= prime;
/**//**
* MASK值,随便找一个值,最好是质数
*/
static int M_MASK = 0x8765fed1;
/**//**
* 一次一个hash
* @param key 输入字符串
* @return 输出hash值
*/
public static int oneByOneHash(String key)
{
int hash, i;
for (hash=0, i=0; i<key.length(); ++i)
{
hash += key.charAt(i);
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
// return (hash & M_MASK);
return hash;
}
/**//**
* Bernstein's hash
* @param key 输入字节数组
* @param level 初始hash常量
* @return 结果hash
*/
public static int bernstein(String key)
{
int hash = 0;
int i;
for (i=0; i<key.length(); ++i) hash = 33*hash + key.charAt(i);
return hash;
}
//
/**///// Pearson's Hash
// char pearson(char[]key, ub4 len, char tab[256])
// {
// char hash;
// ub4 i;
// for (hash=len, i=0; i<len; ++i)
// hash=tab[hash^key[i]];
// return (hash);
// }
/**///// CRC Hashing,计算crc,具体代码见其他
// ub4 crc(char *key, ub4 len, ub4 mask, ub4 tab[256])
// {
// ub4 hash, i;
// for (hash=len, i=0; i<len; ++i)
// hash = (hash >> 8) ^ tab[(hash & 0xff) ^ key[i]];
// return (hash & mask);
// }
/**//**
* Universal Hashing
*/
public static int universal(char[]key, int mask, int[] tab)
{
int hash = key.length, i, len = key.length;
for (i=0; i<(len<<3); i+=8)
{
char k = key[i>>3];
if ((k&0x01) == 0) hash ^= tab[i+0];
if ((k&0x02) == 0) hash ^= tab[i+1];
if ((k&0x04) == 0) hash ^= tab[i+2];
if ((k&0x08) == 0) hash ^= tab[i+3];
if ((k&0x10) == 0) hash ^= tab[i+4];
if ((k&0x20) == 0) hash ^= tab[i+5];
if ((k&0x40) == 0) hash ^= tab[i+6];
if ((k&0x80) == 0) hash ^= tab[i+7];
}
return (hash & mask);
}
/**//**
* Zobrist Hashing
*/
public static int zobrist( char[] key,int mask, int[][] tab)
{
int hash, i;
for (hash=key.length, i=0; i<key.length; ++i)
hash ^= tab[i][key[i]];
return (hash & mask);
}
// LOOKUP3
// 见Bob Jenkins(3).c文件
// 32位FNV算法
static int M_SHIFT = 0;
/**//**
* 32位的FNV算法
* @param data 数组
* @return int值
*/
public static int FNVHash(byte[] data)
{
int hash = (int)2166136261L;
for(byte b : data)
hash = (hash * 16777619) ^ b;
if (M_SHIFT == 0)
return hash;
return (hash ^ (hash >> M_SHIFT)) & M_MASK;
}
/**//**
* 改进的32位FNV算法1
* @param data 数组
* @return int值
*/
public static int FNVHash1(byte[] data)
{
final int p = 16777619;
int hash = (int)2166136261L;
for(byte b:data)
hash = (hash ^ b) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
/**//**
* 改进的32位FNV算法1
* @param data 字符串
* @return int值
*/
public static int FNVHash1(String data)
{
final int p = 16777619;
int hash = (int)2166136261L;
for(int i=0;i<data.length();i++)
hash = (hash ^ data.charAt(i)) * p;
hash += hash << 13;
hash ^= hash >> 7;
hash += hash << 3;
hash ^= hash >> 17;
hash += hash << 5;
return hash;
}
/**//**
* Thomas Wang的算法,整数hash
*/
public static int intHash(int key)
{
key += ~(key << 15);
key ^= (key >>> 10);
key += (key << 3);
key ^= (key >>> 6);
key += ~(key << 11);
key ^= (key >>> 16);
return key;
}
/**//**
* RS算法hash
* @param str 字符串
*/
public static int RSHash(String str)
{
int b = 378551;
int a = 63689;
int hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = hash * a + str.charAt(i);
a = a * b;
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of RS Hash Function */
/**//**
* JS算法
*/
public static int JSHash(String str)
{
int hash = 1315423911;
for(int i = 0; i < str.length(); i++)
{
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of JS Hash Function */
/**//**
* PJW算法
*/
public static int PJWHash(String str)
{
int BitsInUnsignedInt = 32;
int ThreeQuarters = (BitsInUnsignedInt * 3) / 4;
int OneEighth = BitsInUnsignedInt / 8;
int HighBits = 0xFFFFFFFF << (BitsInUnsignedInt - OneEighth);
int hash = 0;
int 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 & 0x7FFFFFFF);
}
/**//* End Of P. J. Weinberger Hash Function */
/**//**
* ELF算法
*/
public static int ELFHash(String str)
{
int hash = 0;
int x = 0;
for(int i = 0; i < str.length(); i++)
{
hash = (hash << 4) + str.charAt(i);
if((x = (int)(hash & 0xF0000000L)) != 0)
{
hash ^= (x >> 24);
hash &= ~x;
}
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of ELF Hash Function */
/**//**
* BKDR算法
*/
public static int BKDRHash(String str)
{
int seed = 131; // 31 131 1313 13131 131313 etc..
int hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = (hash * seed) + str.charAt(i);
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of BKDR Hash Function */
/**//**
* SDBM算法
*/
public static int SDBMHash(String str)
{
int hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of SDBM Hash Function */
/**//**
* DJB算法
*/
public static int DJBHash(String str)
{
int hash = 5381;
for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) + hash) + str.charAt(i);
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of DJB Hash Function */
/**//**
* DEK算法
*/
public static int DEKHash(String str)
{
int hash = str.length();
for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}
return (hash & 0x7FFFFFFF);
}
/**//* End Of DEK Hash Function */
/**//**
* AP算法
*/
public static int APHash(String str)
{
int hash = 0;
for(int i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ? ( (hash << 7) ^ str.charAt(i) ^ (hash >> 3)) :
(~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
}
// return (hash & 0x7FFFFFFF);
return hash;
}
/**//* End Of AP Hash Function */
/**//**
* JAVA自己带的算法
*/
public static int java(String str)
{
int h = 0;
int off = 0;
int len = str.length();
for (int i = 0; i < len; i++)
{
h = 31 * h + str.charAt(off++);
}
return h;
}
/**//**
* 混合hash算法,输出64位的值
*/
public static long mixHash(String str)
{
long hash = str.hashCode();
hash <<= 32;
hash |= FNVHash1(str);
return hash;
}
}
分享到:
相关推荐
Hash (哈希或散列)算法是信息技术领域非常基础也非常重要的技术。它能任意长度的二进制值(明文)...也有一些 Hash 算法不是算力敏感的,例如 scrypt,需要大量的内存资源,节点不能通过简单的增加更多 CPU 来获得
该项目实现了基于Neo4j图形数据库的Geohash算法,并具有专门设计的trie树结构,可快速高效地进行检索。 Geohash实现是从借用的,并且该项目已获得LGPL许可。 对于商业用途,请联系中国Linkedin上的董斌(gmail.com...
目前如果服务器配套的使用的是openssl 1.1.1x, 目前到1.1.1k ,sm2,sm3,sm4都可以直接用openssl_xxx系列函数直接实现,不必大量的代码,但不支持sm2的签名 SM2 该算法主体基于PHPECC算法架构,添加了sm2的椭圆参数算法...
随着电子信息技术的发展,非接触式智能卡(如RFID卡)已经在我们的生活中随处可见。与传统的接触式卡、磁卡相比,利用射频...在软件系统实现方面,提出了一系列安全协议,如:Hash锁、随机Hash锁、Hash链以及改进的随机
随着电子信息技术的发展,非接触式智能卡(如RFID卡)已经在我们的生活中随处可见。与传统的接触式卡、磁卡相比,利用射频...在软件系统实现方面,提出了一系列安全协议,如:Hash锁、随机Hash锁、Hash链以及改进的随机
每个月都有成千上万的图标上传到Iconfinder,同时也伴随而来大量的盗版图。Iconfinder 工程师 Silviu Tantos 在本文中提出一个新颖巧妙的图像查重技术,以杜绝盗版。 我们将在未来几周之内推出一个检测上传图标是否...
它的涉及到技术包括对称加密算法,非对称加密算法,hash函数,数字签名 " "等主要技术,将这几种技术进行有效整合在一起来实现网络数据安全是非常重 " "要的网络安全实践内容。本实验的目的是综合应用这几个技术来...
这是Niels Provos和David Mazieres在“”中描述的OpenBSD Blowfish密码哈希算法的实现。 它的核心基于 ,但经过大量重构,现代化并具有大量更新和增强功能。 它支持所有常见,具有安全敏感的API,并且已针对一系列...
3、在HASH负载均衡算法的基础上结合对SIP协议进行的私有扩展,实现了分布式SIP处理系统的高级容错特性。 4、研究了IMS用户身份标识与鉴权卡号之间的多对多关系,分析了其对分布式SIP处理系统的影响及在不同分布关键字...
众所周知,微博已经为热点事件的传播平台,同时,也带来了大量无关的广告、重复字词(“分享图片”)等。本文利用了SVC对文本中无关性的微博进行了过滤。 (3)使用了朴素贝叶斯对微博进行情感分类。基于贝叶斯定理...
众所周知,微博已经为热点事件的传播平台,同时,也带来了大量无关的广告、重复字词(“分享图片”)等。本文利用了SVC对文本中无关性的微博进行了过滤。 (3)使用了朴素贝叶斯对微博进行情感分类。基于贝叶斯定理...
方案中综合采用时间戳、hash函数和随机数等技术手段设计动态会话密钥,并使用祖冲之加密算法对查询请求信息加密和提供信息完整性认证,能够实现数据机密性、完整性和双向认证等安全功能。为验证所提方案的系统性能,...
键钥则由用户提供的暗码进行SHA-256hash算法得到(使用大量迭代以使得对暗码的暴力解码更加困难) 7z压缩包的安装方法 双击打开下载好的安装包h7z_x86.exe,上图显示的是安装路径不需要管他点击「install」...
多选题 HASH加密使用复杂的数字算法来实现有效的加密,其算法包括:(ABC) A、MD2; B、MD4; C、MD5; D、Cost256 6. 使用Windows2000的组策略,可以限制用户对系统的操作权限,该实例 是何种功能的应用? (A) (单选)A、...
无锁并发编程:多线程竞争锁时,会引起上下文切换,所以多线程处理数据时,可以用一些办法来避免使用锁,如将数据的ID按照Hash算法取模分段,不同的线程处理不同段的数据 CAS算法:Java的Atomic包使用的CAS算法来...
分布式并行搜集技术、启发式搜集策略、镜像消除技术、中英文特征项提取技术、高效索引技术、词典更新技术、超链分析技术、快速检索技术、相关度评价策略、Hash排序算法、Cache策略、中文词汇学习技术和用户行为分析...
5. Hash表与Hash文件的唯一区别是Hash文件引入了‘桶’的概念。【南京航空航天大学1996六10(1分)】 6. 文件系统采用索引结构是为了节省存储空间。【北京邮电大学 2000 一、10 (1分)】 7. 对处理大量数据的外存...
用Typescript实现的经典和的集合。 点击 :video_camera: 表情符号教程。 该存储库的主要目标是教育。 因此,所有实施方式都包含大量指导读者的评论。 该项目的名字iruka,是鸣门人对Iruka sensei的颂歌。 他成为了...
leetcode中文版 Java常见面试算法考试大全 如何使用本仓库 我会在README里面写总结跟一些技巧 大量的细节跟注释都在代码附近,遵从一个知识离代码最近的原则...在达到8个hash冲突链表后会转成红黑树, 而在理论学习的阶
11 关于负载均衡下面说法错误的是( ) 单个重负载的运算分担到多台节点设备上做并行处理,每个节点设备处理结束后,将结果汇总,返回给用户,系统处理能力得到大幅度提高 大量的并发访问或数据流量分担到多台节点...