java

位置:IT落伍者 >> java >> 浏览文章

Hash算法大全(java实现)


发布日期:2024年01月31日
 
Hash算法大全(java实现)

Hash算法有很多很多种类具体的可以参考之前我写的Hash算法的一些分析本处给大家提供一个集合了很多使用的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 prime)

{

int hash i;

for (hash = keylength() i = ; i < keylength(); i++)

hash += keycharAt(i);

return (hash % prime);

}

/**

* 旋转hash

* @param key 输入字符串

* @param prime 质数

* @return hash值

*/

public static int rotatingHash(String key int prime)

{

int hash i;

for (hash=keylength() i=; i<keylength(); ++i)

hash = (hash<<)^(hash>>)^keycharAt(i);

return (hash % prime);

// return (hash ^ (hash>>) ^ (hash>>));

}

// 替代

// 使用hash = (hash ^ (hash>>) ^ (hash>>)) & mask;

// 替代hash %= prime;

/**

* MASK值随便找一个值最好是质数

*/

static int M_MASK = xfed;

/**

* 一次一个hash

* @param key 输入字符串

* @return 输出hash值

*/

public static int oneByOneHash(String key)

{

int hash i;

for (hash= i=; i<keylength(); ++i)

{

hash += keycharAt(i);

hash += (hash << );

hash ^= (hash >> );

}

hash += (hash << );

hash ^= (hash >> );

hash += (hash << );

// return (hash & M_MASK);

return hash;

}

/**

* Bernsteins hash

* @param key 输入字节数组

* @param level 初始hash常量

* @return 结果hash

*/

public static int bernstein(String key)

{

int hash = ;

int i;

for (i=; i<keylength(); ++i) hash = *hash + keycharAt(i);

return hash;

}

//

//// Pearsons Hash

// char pearson(char[]key ub len char tab[])

// {

// char hash;

// ub i;

// for (hash=len i=; i<len; ++i)

// hash=tab[hash^key[i]];

// return (hash);

// }

//// CRC Hashing计算crc具体代码见其他

// ub crc(char *key ub len ub mask ub tab[])

// {

// ub hash i;

// for (hash=len i=; i<len; ++i)

// hash = (hash >> ) ^ tab[(hash & xff) ^ key[i]];

// return (hash & mask);

// }

/**

* Universal Hashing

*/

public static int universal(char[]key int mask int[] tab)

{

int hash = keylength i len = keylength;

for (i=; i<(len<<); i+=)

{

char k = key[i>>];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

if ((k&x) == ) hash ^= tab[i+];

}

return (hash & mask);

}

/**

* Zobrist Hashing

*/

public static int zobrist( char[] keyint mask int[][] tab)

{

int hash i;

for (hash=keylength i=; i<keylength; ++i)

hash ^= tab[i][key[i]];

return (hash & mask);

}

// LOOKUP

// 见Bob Jenkins()c文件

// 位FNV算法

static int M_SHIFT = ;

/**

* 位的FNV算法

* @param data 数组

* @return int值

*/

public static int FNVHash(byte[] data)

{

int hash = (int)L;

for(byte b : data)

hash = (hash * ) ^ b;

if (M_SHIFT == )

return hash;

return (hash ^ (hash >> M_SHIFT)) & M_MASK;

}

/**

* 改进的位FNV算法

* @param data 数组

* @return int值

*/

public static int FNVHash(byte[] data)

{

final int p = ;

int hash = (int)L;

for(byte b:data)

hash = (hash ^ b) * p;

hash += hash << ;

hash ^= hash >> ;

hash += hash << ;

hash ^= hash >> ;

hash += hash << ;

return hash;

}

/**

* 改进的位FNV算法

* @param data 字符串

* @return int值

*/

public static int FNVHash(String data)

{

final int p = ;

int hash = (int)L;

for(int i=;i<datalength();i++)

hash = (hash ^ datacharAt(i)) * p;

hash += hash << ;

hash ^= hash >> ;

hash += hash << ;

hash ^= hash >> ;

hash += hash << ;

return hash;

}

上一篇:Java socket 入门编程实例

下一篇:java技巧之时间计算