标签:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
class GeneralHashFunctionLibrary { /*RSHash*/ public long RSHash(String str) { int b = 378551; int a = 63689; long hash = 0; for ( int i = 0; i < str.length(); i++) { hash = hash * a + str.charAt(i); a = a * b; } return hash; } /*JSHash*/ public long JSHash(String str) { long hash = 1315423911; for ( int i = 0; i < str.length(); i++) hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2)); return hash; } /*PJWHash*/ public long PJWHash(String str) { long BitsInUnsignedInt = ( long )(4 * 8); long ThreeQuarters = ( long )((BitsInUnsignedInt * 3) / 4); long OneEighth = ( long )(BitsInUnsignedInt / 8); long HighBits = ( long )(0xFFFFFFFF)<<(BitsInUnsignedInt-OneEighth); long hash = 0; long test = 0; for ( int i = 0; i < str.length(); i++) { hash = (hash << OneEighth) + str.charAt(i); if ((test = hash & HighBits) != 0) hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits)); } return hash; } /*ELFHash*/ public long ELFHash(String str) { long hash = 0; long x = 0; for ( int i = 0; i < str.length(); i++) { hash = (hash << 4) + str.charAt(i); if (( x = hash & 0xF0000000L) != 0) hash ^= ( x >> 24); hash &= ~x; } return hash; } /*BKDRHash*/ public long BKDRHash(String str) { long seed = 131; //31131131313131131313etc.. long hash = 0; for ( int i = 0; i < str.length(); i++) hash = (hash * seed) + str.charAt(i); return hash; } /*SDBMHash*/ public long SDBMHash(String str) { long hash = 0; for ( int i = 0; i < str.length(); i++) hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash; return hash; } /*DJBHash*/ public long DJBHash(String str) { long hash = 5381; for ( int i = 0; i < str.length(); i++) hash = ((hash << 5) + hash) + str.charAt(i); return hash; } /*DEKHash*/ public long DEKHash(String str) { long hash = str.length(); for ( int i = 0; i < str.length(); i++) hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i); return hash; } /*BPHash*/ public long BPHash(String str) { long hash=0; for ( int i = 0;i < str.length(); i++) hash = hash << 7 ^ str.charAt(i); return hash; } /*FNVHash*/ public long FNVHash(String str) { long fnv_prime = 0x811C9DC5; long hash = 0; for ( int i = 0; i < str.length(); i++) { hash *= fnv_prime; hash ^= str.charAt(i); } return hash; } /*APHash*/ long APHash(String str) { long hash = 0xAAAAAAAA; for ( int i = 0; i < str.length(); i++) { if ((i & 1) == 0) hash ^=((hash << 7) ^ str.charAt(i) ^ (hash >> 3)); else hash ^= (~((hash << 11) ^ str.charAt(i) ^ (hash >> 5))); } return hash; } } |
计算方法 :
用来产生一些数据片段(例如消息或会话项)的哈希值的算法。使用好的哈希算法,在输入数据中所做的更改就可以更改结果哈希值中的所有位;因此,哈希对于检测数据对象(例如消息)中的修改很有用。此外,好的哈希算法使得构造两个相互独立且具有相同哈希的输入不能通过计算方法实现。典型的哈希算法包括 MD2、MD4、MD5 和 SHA-1。哈希算法也称为“哈希函数”。
标签:
原文地址:http://www.cnblogs.com/chenning/p/4886889.html