forked from redis/jedis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashing.java
More file actions
39 lines (32 loc) · 1.01 KB
/
Copy pathHashing.java
File metadata and controls
39 lines (32 loc) · 1.01 KB
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
package redis.clients.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public interface Hashing {
Hashing MURMUR_HASH = new MurmurHash();
ThreadLocal<MessageDigest> md5Holder = new ThreadLocal<MessageDigest>();
Hashing MD5 = new Hashing() {
@Override
public long hash(String key) {
return hash(SafeEncoder.encode(key));
}
@Override
public long hash(byte[] key) {
try {
if (md5Holder.get() == null) {
md5Holder.set(MessageDigest.getInstance("MD5"));
}
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("++++ no md5 algorithm found");
}
MessageDigest md5 = md5Holder.get();
md5.reset();
md5.update(key);
byte[] bKey = md5.digest();
long res = ((long) (bKey[3] & 0xFF) << 24) | ((long) (bKey[2] & 0xFF) << 16)
| ((long) (bKey[1] & 0xFF) << 8) | (long) (bKey[0] & 0xFF);
return res;
}
};
long hash(String key);
long hash(byte[] key);
}