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
  * 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 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
         } 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);
         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[]).

algorithm The message digest algorithm to be supported
Throws: 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>();
New to GrepCode? Check out our FAQ X