常用的哈希函数
 
通用的哈希函数库有下面这些混合了加法和一位操作的字符串哈希算法。下面的这些算法在用法和功能方面各有不同,但是都可以作为学习哈希算法的实现的例子。
 
1.RS 
从Robert Sedgwicks的 Algorithms in C一书中得到了。已经添加了一些简单的优化的算法,以加快其散列过程。
  1. public long RSHash(String str)
  2. {
  3. int b     = 378551;
  4. int a     = 63689;
  5. long hash = 0;
  6. for(int i = 0; i < str.length(); i++)
  7. {
  8. hash = hash * a + str.charAt(i);
  9. a    = a * b;
  10. }
  11. return hash;
  12. }
2.JS
Justin Sobel写的一个位操作的哈希函数。
  1. public long JSHash(String str)
  2. {
  3. long hash = 1315423911;
  4. for(int i = 0; i < str.length(); i++)
  5. {
  6. hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
  7. }
  8. return hash;
  9. }
3.PJW 
该散列算法是基于贝尔实验室的彼得J温伯格的的研究。在Compilers一书中(原则,技术和工具),建议采用这个算法的散列函数的哈希方法。
  1. public long PJWHash(String str)
  2. {
  3. long BitsInUnsignedInt = (long)(4 * 8);
  4. long ThreeQuarters     = (long)((BitsInUnsignedInt  * 3) / 4);
  5. long OneEighth         = (long)(BitsInUnsignedInt / 8);
  6. long HighBits          = (long)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
  7. long hash              = 0;
  8. long test              = 0;
  9. for(int i = 0; i < str.length(); i++)
  10. {
  11. hash = (hash << OneEighth) + str.charAt(i);
  12. if((test = hash & HighBits)  != 0)
  13. {
  14. hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
  15. }
  16. }
  17. return hash;
  18. }
4.ELF 
和PJW很相似,在Unix系统中使用的较多。
  1. public long ELFHash(String str)
  2. {
  3. long hash = 0;
  4. long x    = 0;
  5. for(int i = 0; i < str.length(); i++)
  6. {
  7. hash = (hash << 4) + str.charAt(i);
  8. if((x = hash & 0xF0000000L) != 0)
  9. {
  10. hash ^= (x >> 24);
  11. }
  12. hash &= ~x;
  13. }
  14. return hash;
  15. }
5.BKDR
这个算法来自Brian Kernighan 和 Dennis Ritchie的 The C Programming Language。这是一个很简单的哈希算法,使用了一系列奇怪的数字,形式如31,3131,31...31,看上去和DJB算法很相似。(这个就是Java的字符串哈希函数)
  1. public long BKDRHash(String str)
  2. {
  3. long seed = 131; // 31 131 1313 13131 131313 etc..
  4. long hash = 0;
  5. for(int i = 0; i < str.length(); i++)
  6. {
  7. hash = (hash * seed) + str.charAt(i);
  8. }
  9. return hash;
  10. }
6.SDBM
这个算法在开源的SDBM中使用,似乎对很多不同类型的数据都能得到不错的分布。
  1. public long SDBMHash(String str)
  2. {
  3. long hash = 0;
  4. for(int i = 0; i < str.length(); i++)
  5. {
  6. hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
  7. }
  8. return hash;
  9. }
7.DJB
这个算法是Daniel J.Bernstein 教授发明的,是目前公布的最有效的哈希函数。
  1. public long DJBHash(String str)
  2. {
  3. long hash = 5381;
  4. for(int i = 0; i < str.length(); i++)
  5. {
  6. hash = ((hash << 5) + hash) + str.charAt(i);
  7. }
  8. return hash;
  9. }
8.DEK
由伟大的Knuth在《编程的艺术 第三卷》的第六章排序和搜索中给出。
  1. public long DEKHash(String str)
  2. {
  3. long hash = str.length();
  4. for(int i = 0; i < str.length(); i++)
  5. {
  6. hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
  7. }
  8. return hash;
  9. }
9.AP
这是Arash Partow贡献的一个哈希函数,继承了上面以旋转以为和加操作。代数描述:
  1. public long APHash(String str)
  2. {
  3. long hash = 0xAAAAAAAA;
  4. for(int i = 0; i < str.length(); i++)
  5. {
  6. if ((i & 1) == 0)
  7. {
  8. hash ^= ((hash << 7) ^ str.charAt(i) * (hash >> 3));
  9. }
  10. else
  11. {
  12. hash ^= (~((hash << 11) + str.charAt(i) ^ (hash >> 5)));
  13. }
  14. }
  15. return hash;
  16. }

常用hash函数的更多相关文章

  1. 字符串hash函数

    本文搜集了一些字符串的常用hash函数. 范例1:判断两个单词是否含有相同的字母,此时我们可以用hash做.例如,“aaabb”与"aabb"含有相同的单词.(参考:http:// ...

  2. Hash 函数及其重要性

    不时会爆出网站的服务器和数据库被盗取,考虑到这点,就要确保用户一些敏感数据(例如密码)的安全性.今天,我们要学的是 hash 背后的基础知识,以及如何用它来保护你的 web 应用的密码. 申明 密码学 ...

  3. 【学】常用hash算法的介绍

    基本知识 Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映 ...

  4. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  5. hash函数为什么要选择对素数求余?

    常用的hash函数是选一个数m取模(余数),这个数在课本中推荐m是素数,但是经常见到选择m=2^n,因为对2^n求余数更快,并认为在key分布均匀的情况下,key%m也是在[0,m-1]区间均匀分布的 ...

  6. 理解php Hash函数,增强密码安全

    1.声明 密码学是一个复杂的话题,我也不是这方面的专家.许多高校和研究机构在这方面都有长期的研究.在这篇文章里,我希望尽量使用简单易懂的方式向你展示一种安全存储Web程序密码的方法. 2.“Hash” ...

  7. [转]各种字符串Hash函数比较

    转自:https://www.byvoid.com/zht/blog/string-hash-compare 常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些 ...

  8. 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数

    ---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...

  9. 【转】各种字符串Hash函数比较

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

随机推荐

  1. 30 个 Python 语言的特点技巧

    1   介绍 从我开始学习Python时我就决定维护一个经常使用的“窍门”列表.不论何时当我看到一段让我觉得“酷,这样也行!”的代码时(在一个例子中.在StackOverflow.在开源码软件中,等等 ...

  2. poj 2195Going Home

    http://poj.org/problem?id=2195 #include<cstdio> #include<cstring> #include<cmath> ...

  3. 统计指定时间段的访问真正WEB页面(去除静态请求)的IP的TOP100排行

    最近就在磨这个脚本以达到SEO同事要求哈. awk -v b=[21/Apr/2015:15:46 -v a=[21/Apr/2015:16:46 '$4 > b && $4 & ...

  4. 基于单例使用ThreadLocal对多线程下数据的访问修改

    package cn.lyy.thread; import java.util.Random; /** * 基于单例模式的基础上,使用ThreadLocal为每一个进入的线程生成一个实例, * 用来对 ...

  5. Triangle——LeetCode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  6. Foundation Data Structure

    LinkedList : /** * 考虑的比较的周全,并且包含了全部的情况,代码也不乱<b></b> * * @param index * 插入的位置 * @param c ...

  7. ArcGis : unable to save as template this document is already based on another template

    原文:http://forums.esri.com/Thread.asp?c=93&f=989&t=289930 ----------------- I ran into this p ...

  8. ebtablesBridge

    ebtables和iptables类似,都是Linux系统下网络数据包过滤的配置工具.既然称之为配置工具,就是说过滤功能是由内核底层提供支持的,这两个工具只是负责制定过滤的rules. ebtable ...

  9. php 中的$argv与$argc

    例如 php test.php as a joke print_r($argv); echo $argc; print_r($argv); array_shift($argv); echo $argc ...

  10. 使用jquery生成二维码

    二维码已经渗透到生活中的方方面面,不管到哪,我们都可以用扫一扫解决大多数问题.二狗为了准备应对以后项目中会出现的二维码任务,就上网了解了如何使用jquery.qrcode生成二维码.方法简单粗暴,[] ...