简单Hash函数LongHash
import java.security.SecureRandom;
import java.util.Random; public class LongHash { private static long seed = 0xc4ceb9fe1a85ec53L;
private static Random rand = new SecureRandom(); /**
* Hash algorithm by Bob Jenkins, 1996.
*
* @param k utils on which hash is computed
* @param length utils size
* @param pc primary init value
* @param pb secondary init value
* @param is32BitHash true if just 32-bit hash is expected.
*
* @return
*/
private static long hash(byte[] k, int length, int pc, int pb, boolean is32BitHash) {
int a, b, c; a = b = c = 0xdeadbeef + length + pc;
c += pb; int offset = 0;
while (length > 12) {
a += k[offset + 0];
a += k[offset + 1] << 8;
a += k[offset + 2] << 16;
a += k[offset + 3] << 24;
b += k[offset + 4];
b += k[offset + 5] << 8;
b += k[offset + 6] << 16;
b += k[offset + 7] << 24;
c += k[offset + 8];
c += k[offset + 9] << 8;
c += k[offset + 10] << 16;
c += k[offset + 11] << 24; // mix(a, b, c);
a -= c;
a ^= rot(c, 4);
c += b;
b -= a;
b ^= rot(a, 6);
a += c;
c -= b;
c ^= rot(b, 8);
b += a;
a -= c;
a ^= rot(c, 16);
c += b;
b -= a;
b ^= rot(a, 19);
a += c;
c -= b;
c ^= rot(b, 4);
b += a; length -= 12;
offset += 12;
} switch (length) {
case 12:
c += k[offset + 11] << 24;
case 11:
c += k[offset + 10] << 16;
case 10:
c += k[offset + 9] << 8;
case 9:
c += k[offset + 8];
case 8:
b += k[offset + 7] << 24;
case 7:
b += k[offset + 6] << 16;
case 6:
b += k[offset + 5] << 8;
case 5:
b += k[offset + 4];
case 4:
a += k[offset + 3] << 24;
case 3:
a += k[offset + 2] << 16;
case 2:
a += k[offset + 1] << 8;
case 1:
a += k[offset + 0];
break;
case 0:
return is32BitHash ? c : ((((long) c) << 32)) | ((long) b &0xFFFFFFFFL);
} // Final mixing of thrree 32-bit values in to c
c ^= b;
c -= rot(b, 14);
a ^= c;
a -= rot(c, 11);
b ^= a;
b -= rot(a, 25);
c ^= b;
c -= rot(b, 16);
a ^= c;
a -= rot(c, 4);
b ^= a;
b -= rot(a, 14);
c ^= b;
c -= rot(b, 24); return is32BitHash ? c : ((((long) c) << 32)) | ((long) b &0xFFFFFFFFL);
} private static long rot(int x, int distance) {
return (x << distance) | (x >>> (32 - distance));
// return (x << distance) | (x >>> -distance);
} public static long hash(long a, long b){
byte[] buf = new byte[8*3];
long _seed = seed;
for (int i = 7; i >= 0; i--) {
buf[16 + i] = (byte)(_seed&0xFF);
_seed >>= 8;
}
for (int i = 7; i >= 0; i--) {
buf[8 + i] = (byte)(a & 0xFF);
a >>= 8;
}
for (int i = 7; i >= 0; i--) {
buf[i] = (byte)(b & 0xFF);
b >>= 8;
}
return hash(buf, buf.length, 0, 0, false) & Long.MAX_VALUE;
} public static long create() {
return rand.nextLong();
} public static void main(String[] args) {
System.out.println(hash(1, 2));
System.out.println(hash(2, 1));
System.out.println(hash(2691843745796435739L, 4372953417481662997L));
System.out.println(hash(4372953417481662997L, 2691843745796435739L));
}
}
简单Hash函数LongHash的更多相关文章
- Hash 函数及其重要性
不时会爆出网站的服务器和数据库被盗取,考虑到这点,就要确保用户一些敏感数据(例如密码)的安全性.今天,我们要学的是 hash 背后的基础知识,以及如何用它来保护你的 web 应用的密码. 申明 密码学 ...
- Hash函数及其应用
本文部分内容摘自网络,参考资料链接会在文后给出,在此感谢原作者的分享. 计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的数 ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 理解php Hash函数,增强密码安全
1.声明 密码学是一个复杂的话题,我也不是这方面的专家.许多高校和研究机构在这方面都有长期的研究.在这篇文章里,我希望尽量使用简单易懂的方式向你展示一种安全存储Web程序密码的方法. 2.“Hash” ...
- 长度有限制的字符串hash函数
长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...
- [转]各种字符串Hash函数比较
转自:https://www.byvoid.com/zht/blog/string-hash-compare 常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些 ...
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- 字符串hash函数
本文搜集了一些字符串的常用hash函数. 范例1:判断两个单词是否含有相同的字母,此时我们可以用hash做.例如,“aaabb”与"aabb"含有相同的单词.(参考:http:// ...
- 【转】各种字符串Hash函数比较
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
随机推荐
- Linux环境下mysql主从同步环境搭建
#my.cnf添加内容vim /etc/my.cnf添加以内容: ## replicationserver_id=195binlog-ignore-db=mysqlbinlog_format=mixe ...
- centos7 ,windows7 grub2 双系统引导
因为原先的windows7 和 centos6.3 安装在一台笔记本上.因为centos6.3不能识别无线网卡,在网上找了找,要升级内核到3.2以上. 因为本人初级水平,不敢擅自行动,怕把window ...
- JSP中重定向页面没有全屏显示的问题解决
<script type="text/javascript"> window.onload=function(){ if(window.parent != window ...
- Spring-AOP 基于注解的实现
一.AOP: 是对OOP编程方式的一种补充.翻译过来为“面向切面编程”. 可以理解为一个拦截器框架,但是这个拦截器会非常武断,如果它拦截一个类,那么它就会拦截这个类中的所有方法.如对一个目标列的代理, ...
- JavaScript 运动(缓冲运动,多物体运动 ,多物体多值运动+回调机制)
匀速运动 (当需要物体做匀速运动直接调用statMove函数) function startMove(dom,targetPosetion){ //dom : 运动对象,targetPositio ...
- 我的代码-sql query
# coding: utf-8 # In[ ]: WITH List AS (SELECT e.*,f.* FROM( SELECT DISTINCT c.lot_id, c.wafer_key,LE ...
- Servlet中的编码问题
对于response.setContentType()和response.setCharacterEncoding()的理解: 经过一些实践,对着两个方法有了一些自己的理解,有可能今后的学习中会发现自 ...
- 将jar包安装到本地repository中
mvn install:install-file -Dfile=G:/lcn_springboot2.0/tx-plugins-db-4.1.2.jar -DgroupId=com.codingapi ...
- C语言几个输入函数的区别(史上最详细)
The difference of the string and the character(char): 字符串是一个带有""的字符序列如 "I fuck xuqian ...
- 6. Vulnerability scanners (漏洞扫描器 11个)
Nessus是最流行和最有能力的漏洞扫描程序之一,特别为UNIX系统. 它最初是免费的和开源的,但是他们在2005年关闭了源代码,并在2008年删除了免费的“注册Feed”版本.现在要每年花费2,19 ...