fix md5 hashing as MessageDigest is not threadsafe, now using ThreadLocal
This commit is contained in:
@@ -5,23 +5,22 @@ import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public interface Hashing {
|
||||
public static final Hashing MURMUR_HASH = new MurmurHash();
|
||||
public ThreadLocal<MessageDigest> md5Holder = new ThreadLocal<MessageDigest>();
|
||||
|
||||
public static final Hashing MD5 = new Hashing() {
|
||||
private MessageDigest md5 = null; // avoid recurring construction
|
||||
|
||||
public long hash(String key) {
|
||||
return hash(SafeEncoder.encode(key));
|
||||
}
|
||||
|
||||
public long hash(byte[] key) {
|
||||
if (md5 == null) {
|
||||
try {
|
||||
md5 = MessageDigest.getInstance("MD5");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(
|
||||
"++++ no md5 algorythm found");
|
||||
try {
|
||||
if (md5Holder.get() == null) {
|
||||
md5Holder.set(MessageDigest.getInstance("MD5"));
|
||||
}
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("++++ no md5 algorythm found");
|
||||
}
|
||||
MessageDigest md5 = md5Holder.get();
|
||||
|
||||
md5.reset();
|
||||
md5.update(key);
|
||||
|
||||
Reference in New Issue
Block a user