fix md5 hashing as MessageDigest is not threadsafe, now using ThreadLocal

This commit is contained in:
Jonathan Leibiusky
2011-05-11 19:45:07 -03:00
parent 0d6d37b95f
commit 66b0a5784d

View File

@@ -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);