Hash算法有很多很多種類
Java代碼
/**
* Hash算法大全<br>
* 推薦使用FNV
* @algorithm None
* @author Goodzzp
* @lastEdit Goodzzp
* @editDetail Create
*/
public class HashAlgorithms
{
/**
* 加法hash
* @param key 字符串
* @param prime 一個質數
* @return hash結果
*/
public static int additiveHash(String key
{
int hash
for (hash = key
hash += key
return (hash % prime);
}
/**
* 旋轉hash
* @param key 輸入字符串
* @param prime 質數
* @return hash值
*/
public static int rotatingHash(String key
{
int hash
for (hash=key
hash = (hash<<
return (hash % prime);
// return (hash ^ (hash>>
}
// 替代
// 使用
// 替代
/**
* MASK值
*/
static int M_MASK =
/**
* 一次一個hash
* @param key 輸入字符串
* @return 輸出hash值
*/
public static int oneByOneHash(String key)
{
int hash
for (hash=
{
hash += key
hash += (hash <<
hash ^= (hash >>
}
hash += (hash <<
hash ^= (hash >>
hash += (hash <<
// return (hash & M_MASK);
return hash;
}
/**
* Bernstein
* @param key 輸入字節數組
* @param level 初始hash常量
* @return 結果hash
*/
public static int bernstein(String key)
{
int hash =
int i;
for (i=
return hash;
}
//
//// Pearson
// char pearson(char[]key
// {
// char hash;
// ub
// for (hash=len
// hash=tab[hash^key[i]];
// return (hash);
// }
//// CRC Hashing
// ub
// {
// ub
// for (hash=len
// hash = (hash >>
// return (hash & mask);
// }
/**
* Universal Hashing
*/
public static int universal(char[]key
{
int hash = key
for (i=
{
char k = key[i>>
if ((k&
if ((k&
if ((k&
if ((k&
if ((k&
if ((k&
if ((k&
if ((k&
}
return (hash & mask);
}
/**
* Zobrist Hashing
*/
public static int zobrist( char[] key
{
int hash
for (hash=key
hash ^= tab[i][key[i]];
return (hash & mask);
}
// LOOKUP
// 見Bob Jenkins(
//
static int M_SHIFT =
/**
*
* @param data 數組
* @return int值
*/
public static int FNVHash(byte[] data)
{
int hash = (int)
for(byte b : data)
hash = (hash *
if (M_SHIFT ==
return hash;
return (hash ^ (hash >> M_SHIFT)) & M_MASK;
}
/**
* 改進的
* @param data 數組
* @return int值
*/
public static int FNVHash
{
final int p =
int hash = (int)
for(byte b:data)
hash = (hash ^ b) * p;
hash += hash <<
hash ^= hash >>
hash += hash <<
hash ^= hash >>
hash += hash <<
return hash;
}
/**
* 改進的
* @param data 字符串
* @return int值
*/
public static int FNVHash
{
final int p =
int hash = (int)
for(int i=
hash = (hash ^ data
hash += hash <<
hash ^= hash >>
hash += hash <<
hash ^= hash >>
hash += hash <<
return hash;
}
[NextPage]
/**
* Thomas Wang的算法
*/
public static int intHash(int key)
{
key += ~(key <<
key ^= (key >>>
key += (key <<
key ^= (key >>>
key += ~(key <<
key ^= (key >>>
return key;
}
/**
* RS算法hash
* @param str 字符串
*/
public static int RSHash(String str)
{
int b =
int a =
int hash =
for(int i =
{
hash = hash * a + str
a = a * b;
}
return (hash &
}
/* End Of RS Hash Function */
/**
* JS算法
*/
public static int JSHash(String str)
{
int hash =
for(int i =
{
hash ^= ((hash <<
}
return (hash &
}
/* End Of JS Hash Function */
/**
* PJW算法
*/
public static int PJWHash(String str)
{
int BitsInUnsignedInt =
int ThreeQuarters = (BitsInUnsignedInt *
int OneEighth = BitsInUnsignedInt /
int HighBits =
int hash =
int test =
for(int i =
{
hash = (hash << OneEighth) + str
if((test = hash & HighBits) !=
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return (hash &
}
/* End Of P
/**
* ELF算法
*/
public static int ELFHash(String str)
{
int hash =
int x =
for(int i =
{
hash = (hash <<
if((x = (int)(hash &
{
hash ^= (x >>
hash &= ~x;
}
}
return (hash &
}
/* End Of ELF Hash Function */
/**
* BKDR算法
*/
public static int BKDRHash(String str)
{
int seed =
int hash =
for(int i =
{
hash = (hash * seed) + str
}
return (hash &
}
/* End Of BKDR Hash Function */
/**
* SDBM算法
*/
public static int SDBMHash(String str)
{
int hash =
for(int i =
{
hash = str
}
return (hash &
}
/* End Of SDBM Hash Function */
/**
* DJB算法
*/
public static int DJBHash(String str)
{
int hash =
for(int i =
{
hash = ((hash <<
}
return (hash &
}
/* End Of DJB Hash Function */
/**
* DEK算法
*/
public static int DEKHash(String str)
{
int hash = str
for(int i =
{
hash = ((hash <<
}
return (hash &
}
/* End Of DEK Hash Function */
/**
* AP算法
*/
public static int APHash(String str)
{
int hash =
for(int i =
{
hash ^= ((i &
(~((hash <<
}
// return (hash &
return hash;
}
/* End Of AP Hash Function */
/**
* JAVA自己帶的算法
*/
public static int java(String str)
{
int h =
int off =
int len = str
for (int i =
{
h =
}
return h;
}
/**
* 混合hash算法
*/
public static long mixHash(String str)
{
long hash = str
hash <<=
hash |= FNVHash
return hash;
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25947.html