java Hash算法大全(转载的)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值