package redis.clients.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public interface Hashing {
    public static final Hashing MURMUR_HASH = new MurmurHash();

    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");
                }
            }

            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;
        }
    };

    public long hash(String key);

    public long hash(byte[] key);
}
@@ -197,17 +197,16 @@
 Hash();%0A
-%0A
     publ
@@ -212,125 +212,135 @@
 lic 
-static final Hashing MD5 = new Hashing() %7B%0A        private MessageDigest md5 = null; // avoid recurring construction%0A
+ThreadLocal%3CMessageDigest%3E md5Holder = new ThreadLocal%3CMessageDigest%3E();%0A%0A    public static final Hashing MD5 = new Hashing() %7B
 %0A   
@@ -491,24 +491,11 @@
     
-if (md5 == null)
+try
  %7B%0A 
@@ -501,35 +501,60 @@
                 
-try
+if (md5Holder.get() == null)
  %7B%0A             
@@ -563,19 +563,27 @@
      md5
- = 
+Holder.set(
 MessageD
@@ -606,16 +606,17 @@
 e(%22MD5%22)
+)
 ;%0A      
@@ -626,16 +626,30 @@
        %7D
+%0A            %7D
  catch (
@@ -694,20 +694,16 @@
         
-    
 throw ne
@@ -730,37 +730,8 @@
 ion(
-%0A                            
 %22+++
@@ -762,36 +762,32 @@
 %22);%0A            
-    
 %7D%0A            %7D%0A
@@ -776,33 +776,68 @@
   %7D%0A            
-%7D
+MessageDigest md5 = md5Holder.get();
 %0A%0A            md

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