---
title: "Hash算法大全(java实现）"
author: "ssxxjjii"
date: "2013-11-01T18:29:28Z"
tags: []
categories: ["互联网"]
slug: "hash-java"
draft: false
lastmod: "2013-11-01T19:03:45Z"
id: 1968756
url: "https://tdcq.iteye.com/blog/1968756"
---

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

[http://www.java3z.com/cwbwebhome/article/article5/51002.html](http://www.java3z.com/cwbwebhome/article/article5/51002.html)

Hash算法有很多很多种类。具体的可以参考之前我写的Hash算法的一些分析。本处给大家提供一个集合了很多使用的Hash算法的类，应该可以满足不少人的需要的：

Java代码

001. /\**
002. * Hash算法大全&lt;br&gt;
003. * 推荐使用FNV1算法
004. * @algorithm None
005. * @author Goodzzp 2006-11-20
006. * @lastEdit Goodzzp 2006-11-20
007. * @editDetail Create
008. \*/
009. **public** **class** HashAlgorithms
010. {
011. /\**
012. * 加法hash
013. * @param key 字符串
014. * @param prime 一个质数
015. * @return hash结果
016. \*/
017. **public** **static** **int** additiveHash(String key, **int** prime)
018. {
019. **int** hash, i;
020. **for** (hash = key.length(), i = 0; i &lt; key.length(); i++)
021. hash += key.charAt(i);
022. **return** (hash % prime);
023. }
024. /\**
025. * 旋转hash
026. * @param key 输入字符串
027. * @param prime 质数
028. * @return hash值
029. \*/
030. **public** **static** **int** rotatingHash(String key, **int** prime)
031. {
032. **int** hash, i;
033. **for** (hash=key.length(), i=0; i&lt;key.length(); ++i)
034. hash = (hash&lt;&lt;4)^(hash&gt;&gt;28)^key.charAt(i);
035. **return** (hash % prime);
036. //   return (hash ^ (hash&gt;&gt;10) ^ (hash&gt;&gt;20));
037. }
038. // 替代：
039. // 使用：hash = (hash ^ (hash&gt;&gt;10) ^ (hash&gt;&gt;20)) & mask;
040. // 替代：hash %= prime;
041. /\**
042. * MASK值，随便找一个值，最好是质数
043. \*/
044. **static** **int** M\_MASK = 0x8765fed1;
045. /\**
046. * 一次一个hash
047. * @param key 输入字符串
048. * @return 输出hash值
049. \*/
050. **public** **static** **int** oneByOneHash(String key)
051. {
052. **int**   hash, i;
053. **for** (hash=0, i=0; i&lt;key.length(); ++i)
054. {
055. hash += key.charAt(i);
056. hash += (hash &lt;&lt; 10);
057. hash ^= (hash &gt;&gt; 6);
058. }
059. hash += (hash &lt;&lt; 3);
060. hash ^= (hash &gt;&gt; 11);
061. hash += (hash &lt;&lt; 15);
062. //   return (hash & M\_MASK);
063. **return** hash;
064. }
065. /\**
066. * Bernstein's hash
067. * @param key 输入字节数组
068. * @param level 初始hash常量
069. * @return 结果hash
070. \*/
071. **public** **static** **int** bernstein(String key)
072. {
073. **int** hash = 0;
074. **int** i;
075. **for** (i=0; i&lt;key.length(); ++i) hash = 33\*hash + key.charAt(i);
076. **return** hash;
077. }
078. //
079. //// Pearson's Hash
080. // char pearson(char\[]key, ub4 len, char tab\[256])
081. // {
082. //   char hash;
083. //   ub4 i;
084. //   for (hash=len, i=0; i&lt;len; ++i)
085. //     hash=tab\[hash^key\[i]];
086. //   return (hash);
087. // }
088. //// CRC Hashing，计算crc,具体代码见其他
089. // ub4 crc(char \*key, ub4 len, ub4 mask, ub4 tab\[256])
090. // {
091. //   ub4 hash, i;
092. //   for (hash=len, i=0; i&lt;len; ++i)
093. //     hash = (hash &gt;&gt; 8) ^ tab\[(hash & 0xff) ^ key\[i]];
094. //   return (hash & mask);
095. // }
096. /\**
097. * Universal Hashing
098. \*/
099. **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&lt;(len&lt;&lt;3); i+=8)
103. {
104. **char** k = key\[i&gt;&gt;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&lt;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 &gt;&gt; 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 &lt;&lt; 13;
156. hash ^= hash &gt;&gt; 7;
157. hash += hash &lt;&lt; 3;
158. hash ^= hash &gt;&gt; 17;
159. hash += hash &lt;&lt; 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&lt;data.length();i++)
172. hash = (hash ^ data.charAt(i)) * p;
173. hash += hash &lt;&lt; 13;
174. hash ^= hash &gt;&gt; 7;
175. hash += hash &lt;&lt; 3;
176. hash ^= hash &gt;&gt; 17;
177. hash += hash &lt;&lt; 5;
178. **return** hash;
179. }
180. /\**
181. * Thomas Wang的算法，整数hash
182. \*/
183. **public** **static** **int** intHash(**int** key)
184. {
185. key += ~(key &lt;&lt; 15);
186. key ^= (key &gt;&gt;&gt; 10);
187. key += (key &lt;&lt; 3);
188. key ^= (key &gt;&gt;&gt; 6);
189. key += ~(key &lt;&lt; 11);
190. key ^= (key &gt;&gt;&gt; 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 &lt; 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 &lt; str.length(); i++)
217. {
218. hash ^= ((hash &lt;&lt; 5) + str.charAt(i) + (hash &gt;&gt; 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 &lt;&lt; (BitsInUnsignedInt - OneEighth);
232. **int** hash              = 0;
233. **int** test              = 0;
234. **for**(**int** i = 0; i &lt; str.length();i++)
235. {
236. hash = (hash &lt;&lt; OneEighth) + str.charAt(i);
237. **if**((test = hash & HighBits) != 0)
238. {
239. hash = (( hash ^ (test &gt;&gt; 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 &lt; str.length(); i++)
253. {
254. hash = (hash &lt;&lt; 4) + str.charAt(i);
255. **if**((x = (**int**)(hash & 0xF0000000L)) != 0)
256. {
257. hash ^= (x &gt;&gt; 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 &lt; 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 &lt; str.length(); i++)
285. {
286. hash = str.charAt(i) + (hash &lt;&lt; 6) + (hash &lt;&lt; 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 &lt; str.length(); i++)
298. {
299. hash = ((hash &lt;&lt; 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 &lt; str.length(); i++)
311. {
312. hash = ((hash &lt;&lt; 5) ^ (hash &gt;&gt; 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 &lt; str.length(); i++)
324. {
325. hash ^= ((i & 1) == 0) ? ( (hash &lt;&lt; 7) ^ str.charAt(i) ^ (hash &gt;&gt; 3)) :
326. (~((hash &lt;&lt; 11) ^ str.charAt(i) ^ (hash &gt;&gt; 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 &lt; 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 &lt;&lt;= 32;
353. hash |= FNVHash1(str);
354. **return** hash;
355. }