You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2011/10/10 12:45:17 UTC

svn commit: r1180870 - /tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java

Author: markt
Date: Mon Oct 10 10:45:17 2011
New Revision: 1180870

URL: http://svn.apache.org/viewvc?rev=1180870&view=rev
Log:
Add initial implementation of a thread safe MessageDigest helper class

Added:
    tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java

Added: tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java?rev=1180870&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java (added)
+++ tomcat/trunk/java/org/apache/catalina/util/MsgDigest.java Mon Oct 10 10:45:17 2011
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.catalina.util;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
+
+/**
+ * Helper class for generating message digests in multi-threaded environments.
+ */
+public class MsgDigest {
+    
+    private static Helper md5 = new Helper("MD5");
+    private static Helper sha1 = new Helper("SHA1");
+
+    public static byte[] getMD5(byte[] input) {
+        return md5.digest(input);
+    }
+
+    public static byte[] getSHA1(byte[] input) {
+        return sha1.digest(input);
+    }
+    
+    private static class Helper {
+        
+        private final String digestName;
+        private final Queue<MessageDigest> digesters =
+                new ConcurrentLinkedQueue<MessageDigest>();
+        
+        public Helper(String digestName) {
+            this.digestName = digestName;
+        }
+        
+        public byte[] digest(byte[] input) {
+            
+            // Get a digest from the queue
+            MessageDigest digest = digesters.poll();
+            if (digest == null) {
+                // Queue empty, create a new one
+                try {
+                    digest = MessageDigest.getInstance(digestName);
+                } catch (NoSuchAlgorithmException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            }
+            
+            // Reset the digest before we use it
+            digest.reset();
+            
+            // Create the digest for the provided input
+            digest.update(input);
+            byte[] result = digest.digest();
+            
+            // Return the digester to the queue
+            digesters.add(digest);
+            
+            return result;
+        }
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org