ITeye博客

Hash算法大全(java实现)

ssxxjjii · 2013-11-01 18:29 · core java · 互联网 · Markdown

https://weblogs.java.net/blog/2007/11/27/consistent-hashing  java 版本的例子

http://www.java3z.com/cwbwebhome/article/article5/51002.html

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。