Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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 static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.util.Map;
A thread safe wrapper around java.security.MessageDigest that does not make use of ThreadLocal and - broadly - only creates enough MessageDigest objects to satisfy the concurrency requirements.
 
 public class ConcurrentMessageDigest {
 
     private static final String MD5 = "MD5";
 
     private static final Map<String,Queue<MessageDigest>> queues =
             new HashMap<String,Queue<MessageDigest>>();
 
 
     private ConcurrentMessageDigest() {
         // Hide default constructor for this utility class
     }
 
     static {
         try {
             // Init commonly used algorithms
             init();
         } catch (NoSuchAlgorithmException e) {
             throw new IllegalArgumentException(e);
         }
     }
 
     public static byte[] digestMD5(byte[] input) {
         return digest(input);
     }
 
     public static byte[] digest(String algorithmbyte[] input) {
 
         Queue<MessageDigestqueue = .get(algorithm);
         if (queue == null) {
             throw .uninitializedMessageDigest();
         }
 
         MessageDigest md = queue.poll();
         if (md == null) {
             try {
                 md = MessageDigest.getInstance(algorithm);
             } catch (NoSuchAlgorithmException e) {
                 // Ignore. Impossible if init() has been successfully called
                 // first.
                 throw .uninitializedMessageDigest();
             }
         }
 
         byte[] result = md.digest(input);
 
         queue.add(md);
 
         return result;
     }


    
Ensures that digest(java.lang.String,byte[]) and digestAsHex(java.lang.String,byte[]) will support the specified algorithm. This method must be called and return successfully before using digest(java.lang.String,byte[]) or digestAsHex(java.lang.String,byte[]).

Parameters:
algorithm The message digest algorithm to be supported
Throws:
java.security.NoSuchAlgorithmException If the algorithm is not supported by the JVM
 
     public static void init(String algorithmthrows NoSuchAlgorithmException {
         synchronized () {
             if (!.containsKey(algorithm)) {
                 MessageDigest md = MessageDigest.getInstance(algorithm);
                Queue<MessageDigestqueue = new ConcurrentLinkedQueue<MessageDigest>();
                queue.add(md);
                .put(algorithmqueue);
            }
        }
    }
New to GrepCode? Check out our FAQ X