Java之——Hash算法大全

https://blog.csdn.net/l1028386804/article/details/54573106

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/54573106

实际工作过程中,要用到各种各样的Hash算法,今天就给大家带来一篇基于java实现的各类Hash算法,其他语言本质上是一样的,大家可以拿来做个参考,好了,不多说了,我们直接上代码

  1. package com.lyz.hash;
  2. /**
  3.  * Hash算法大全<br>
  4.  * 推荐使用FNV1算法
  5.  * @author liuyazhuang
  6.  */
  7. public class HashAlgorithms {
  8. /**
  9.         * 加法hash
  10.         *
  11.         * @param key
  12.         *            字符串
  13.         * @param prime
  14.         *            一个质数
  15.         * @return hash结果
  16.         */
  17.        public static int additiveHash(String key, int prime) {
  18.               int hash, i;
  19.               for (hash = key.length(), i = 0; i < key.length(); i++)
  20.                      hash += key.charAt(i);
  21.               return (hash % prime);
  22.        }
  23.        /**
  24.         * 旋转hash
  25.         *
  26.         * @param key
  27.         *            输入字符串
  28.         * @param prime
  29.         *            质数
  30.         * @return hash
  31.         */
  32.        public static int rotatingHash(String key, int prime) {
  33.               int hash, i;
  34.               for (hash = key.length(), i = 0; i < key.length(); ++i)
  35.                      hash = (hash << 4) ^ (hash >> 28) ^ key.charAt(i);
  36.               return (hash % prime);
  37.               // return (hash ^ (hash>>10) ^ (hash>>20));
  38.        }
  39.        // 替代:
  40.        // 使用:hash = (hash ^ (hash>>10) ^ (hash>>20)) & mask;
  41.        // 替代:hash %= prime;
  42.        /**
  43.         * MASK值,随便找一个值,最好是质数
  44.         */
  45.        static int M_MASK = 0x8765fed1;
  46.        /**
  47.         * 一次一个hash
  48.         *
  49.         * @param key
  50.         *            输入字符串
  51.         * @return 输出hash
  52.         */
  53.        public static int oneByOneHash(String key) {
  54.               int hash, i;
  55.               for (hash = 0, i = 0; i < key.length(); ++i) {
  56.                      hash += key.charAt(i);
  57.                      hash += (hash << 10);
  58.                      hash ^= (hash >> 6);
  59.               }
  60.               hash += (hash << 3);
  61.               hash ^= (hash >> 11);
  62.               hash += (hash << 15);
  63.               // return (hash & M_MASK);
  64.               return hash;
  65.        }
  66.        /**
  67.         * Bernstein's hash
  68.         *
  69.         * @param key
  70.         *            输入字节数组
  71.         * @param level
  72.         *            初始hash常量
  73.         * @return 结果hash
  74.         */
  75.        public static int bernstein(String key) {
  76.               int hash = 0;
  77.               int i;
  78.               for (i = 0; i < key.length(); ++i)
  79.                      hash = 33 * hash + key.charAt(i);
  80.               return hash;
  81.        }
  82.        /**
  83.         * Universal Hashing
  84.         */
  85.        public static int universal(char[] key, int mask, int[] tab) {
  86.               int hash = key.length, i, len = key.length;
  87.               for (i = 0; i < (len << 3); i += 8) {
  88.                      char k = key[i >> 3];
  89.                      if ((k & 0x01) == 0)
  90.                             hash ^= tab[i + 0];
  91.                      if ((k & 0x02) == 0)
  92.                             hash ^= tab[i + 1];
  93.                      if ((k & 0x04) == 0)
  94.                             hash ^= tab[i + 2];
  95.                      if ((k & 0x08) == 0)
  96.                             hash ^= tab[i + 3];
  97.                      if ((k & 0x10) == 0)
  98.                             hash ^= tab[i + 4];
  99.                      if ((k & 0x20) == 0)
  100.                             hash ^= tab[i + 5];
  101.                      if ((k & 0x40) == 0)
  102.                             hash ^= tab[i + 6];
  103.                      if ((k & 0x80) == 0)
  104.                             hash ^= tab[i + 7];
  105.               }
  106.               return (hash & mask);
  107.        }
  108.        /**
  109.         * Zobrist Hashing
  110.         */
  111.        public static int zobrist(char[] key, int mask, int[][] tab) {
  112.               int hash, i;
  113.               for (hash = key.length, i = 0; i < key.length; ++i)
  114.                      hash ^= tab[i][key[i]];
  115.               return (hash & mask);
  116.        }
  117.        // LOOKUP3
  118.        // Bob Jenkins(3).c文件
  119.        // 32FNV算法
  120.        static int M_SHIFT = 0;
  121.        /**
  122.         * 32位的FNV算法
  123.         *
  124.         * @param data
  125.         *            数组
  126.         * @return int
  127.         */
  128.        public static int FNVHash(byte[] data) {
  129.               int hash = (int) 2166136261L;
  130.               for (byte b : data)
  131.                      hash = (hash * 16777619) ^ b;
  132.               if (M_SHIFT == 0)
  133.                      return hash;
  134.               return (hash ^ (hash >> M_SHIFT)) & M_MASK;
  135.        }
  136.        /**
  137.         * 改进的32FNV算法1
  138.         *
  139.         * @param data
  140.         *            数组
  141.         * @return int
  142.         */
  143.        public static int FNVHash1(byte[] data) {
  144.               final int p = 16777619;
  145.               int hash = (int) 2166136261L;
  146.               for (byte b : data)
  147.                      hash = (hash ^ b) * p;
  148.               hash += hash << 13;
  149.               hash ^= hash >> 7;
  150.               hash += hash << 3;
  151.               hash ^= hash >> 17;
  152.               hash += hash << 5;
  153.               return hash;
  154.        }
  155.        /**
  156.         * 改进的32FNV算法1
  157.         *
  158.         * @param data
  159.         *            字符串
  160.         * @return int
  161.         */
  162.        public static int FNVHash1(String data) {
  163.               final int p = 16777619;
  164.               int hash = (int) 2166136261L;
  165.               for (int i = 0; i < data.length(); i++)
  166.                      hash = (hash ^ data.charAt(i)) * p;
  167.               hash += hash << 13;
  168.               hash ^= hash >> 7;
  169.               hash += hash << 3;
  170.               hash ^= hash >> 17;
  171.               hash += hash << 5;
  172.               return hash;
  173.        }
  174.        /**
  175.         * Thomas Wang的算法,整数hash
  176.         */
  177.        public static int intHash(int key) {
  178.               key += ~(key << 15);
  179.               key ^= (key >>> 10);
  180.               key += (key << 3);
  181.               key ^= (key >>> 6);
  182.               key += ~(key << 11);
  183.               key ^= (key >>> 16);
  184.               return key;
  185.        }
  186.        /**
  187.         * RS算法hash
  188.         *
  189.         * @param str
  190.         *            字符串
  191.         */
  192.        public static int RSHash(String str) {
  193.               int b = 378551;
  194.               int a = 63689;
  195.               int hash = 0;
  196.               for (int i = 0; i < str.length(); i++) {
  197.                      hash = hash * a + str.charAt(i);
  198.                      a = a * b;
  199.               }
  200.               return (hash & 0x7FFFFFFF);
  201.        }
  202.        /* End Of RS Hash Function */
  203.        /**
  204.         * JS算法
  205.         */
  206.        public static int JSHash(String str) {
  207.               int hash = 1315423911;
  208.               for (int i = 0; i < str.length(); i++) {
  209.                      hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
  210.               }
  211.               return (hash & 0x7FFFFFFF);
  212.        }
  213.        /* End Of JS Hash Function */
  214.        /**
  215.         * PJW算法
  216.         */
  217.        public static int PJWHash(String str) {
  218.               int BitsInUnsignedInt = 32;
  219.               int ThreeQuarters = (BitsInUnsignedInt * 3) / 4;
  220.               int OneEighth = BitsInUnsignedInt / 8;
  221.               int HighBits = 0xFFFFFFFF << (BitsInUnsignedInt - OneEighth);
  222.               int hash = 0;
  223.               int test = 0;
  224.               for (int i = 0; i < str.length(); i++) {
  225.                      hash = (hash << OneEighth) + str.charAt(i);
  226.                      if ((test = hash & HighBits) != 0) {
  227.                             hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
  228.                      }
  229.               }
  230.               return (hash & 0x7FFFFFFF);
  231.        }
  232.        /* End Of P. J. Weinberger Hash Function */
  233.        /**
  234.         * ELF算法
  235.         */
  236.        public static int ELFHash(String str) {
  237.               int hash = 0;
  238.               int x = 0;
  239.               for (int i = 0; i < str.length(); i++) {
  240.                      hash = (hash << 4) + str.charAt(i);
  241.                      if ((x = (int) (hash & 0xF0000000L)) != 0) {
  242.                             hash ^= (x >> 24);
  243.                             hash &= ~x;
  244.                      }
  245.               }
  246.               return (hash & 0x7FFFFFFF);
  247.        }
  248.        /* End Of ELF Hash Function */
  249.        /**
  250.         * BKDR算法
  251.         */
  252.        public static int BKDRHash(String str) {
  253.               int seed = 131; // 31 131 1313 13131 131313 etc..
  254.               int hash = 0;
  255.               for (int i = 0; i < str.length(); i++) {
  256.                      hash = (hash * seed) + str.charAt(i);
  257.               }
  258.               return (hash & 0x7FFFFFFF);
  259.        }
  260.        /* End Of BKDR Hash Function */
  261.        /**
  262.         * SDBM算法
  263.         */
  264.        public static int SDBMHash(String str) {
  265.               int hash = 0;
  266.               for (int i = 0; i < str.length(); i++) {
  267.                      hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
  268.               }
  269.               return (hash & 0x7FFFFFFF);
  270.        }
  271.        /* End Of SDBM Hash Function */
  272.        /**
  273.         * DJB算法
  274.         */
  275.        public static int DJBHash(String str) {
  276.               int hash = 5381;
  277.               for (int i = 0; i < str.length(); i++) {
  278.                      hash = ((hash << 5) + hash) + str.charAt(i);
  279.               }
  280.               return (hash & 0x7FFFFFFF);
  281.        }
  282.        /* End Of DJB Hash Function */
  283.        /**
  284.         * DEK算法
  285.         */
  286.        public static int DEKHash(String str) {
  287.               int hash = str.length();
  288.               for (int i = 0; i < str.length(); i++) {
  289.                      hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
  290.               }
  291.               return (hash & 0x7FFFFFFF);
  292.        }
  293.        /* End Of DEK Hash Function */
  294.        /**
  295.         * AP算法
  296.         */
  297.        public static int APHash(String str) {
  298.               int hash = 0;
  299.               for (int i = 0; i < str.length(); i++) {
  300.                      hash ^= ((i & 1) == 0) ? ((hash << 7) ^ str.charAt(i) ^ (hash >> 3))
  301.                                    : (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
  302.               }
  303.               // return (hash & 0x7FFFFFFF);
  304.               return hash;
  305.        }
  306.        /* End Of AP Hash Function */
  307.        /**
  308.         * JAVA自己带的算法
  309.         */
  310.        public static int java(String str) {
  311.               int h = 0;
  312.               int off = 0;
  313.               int len = str.length();
  314.               for (int i = 0; i < len; i++) {
  315.                      h = 31 * h + str.charAt(off++);
  316.               }
  317.               return h;
  318.        }
  319.        /**
  320.         * 混合hash算法,输出64位的值
  321.         */
  322.        public static long mixHash(String str) {
  323.               long hash = str.hashCode();
  324.               hash <<= 32;
  325.               hash |= FNVHash1(str);
  326.               return hash;
  327.        }
  328. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值