ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/security/ConcurrentMessageDigest.java

Path
ttomcat-1778514358873.zip-extract/apache-tomcat-11.0.18-src/java/org/apache/tomcat/util/security/ConcurrentMessageDigest.java
Status
scanned
Type
file
Name
ConcurrentMessageDigest.java
Extension
.java
Programming language
Java
Mime type
text/plain
File type
ASCII text, with CRLF line terminators
Tag

      
    
Rootfs path

      
    
Size
5014 (4.9 KB)
MD5
2c634e7f108bcb665dca633bcdcdb0e0
SHA1
f8ed02cec5c09d5ef3156514f3b9e2327442184f
SHA256
00ea9377f321d397aa17d1251d17b9dc1e24a99fe5ad062e9a542787415d4b64
SHA512

      
    
SHA1_git
338a7ed33d9b7e1a553e8409d9a4f1818215dd64
Is binary

      
    
Is text
True
Is archive

      
    
Is media

      
    
Is legal

      
    
Is manifest

      
    
Is readme

      
    
Is top level

      
    
Is key file

      
    
ConcurrentMessageDigest.java | 4.9 KB |

/* * 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.tomcat.util.security; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import org.apache.juli.logging.Log; import org.apache.juli.logging.LogFactory; import org.apache.tomcat.util.res.StringManager; /** * A thread safe wrapper around {@link 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 StringManager sm = StringManager.getManager(ConcurrentMessageDigest.class); private static final Log log = LogFactory.getLog(ConcurrentMessageDigest.class); private static final String MD5 = "MD5"; private static final String SHA1 = "SHA-1"; private static final String SHA256 = "SHA-256"; private static final Map<String,Queue<MessageDigest>> queues = new ConcurrentHashMap<>(); private ConcurrentMessageDigest() { // Hide default constructor for this utility class } static { // Init commonly used algorithms try { init(MD5); } catch (NoSuchAlgorithmException e) { log.warn(sm.getString("concurrentMessageDigest.noDigest"), e); } try { init(SHA1); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(sm.getString("concurrentMessageDigest.noDigest"), e); } try { init(SHA256); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException(sm.getString("concurrentMessageDigest.noDigest"), e); } } public static byte[] digestMD5(byte[]... input) { return digest(MD5, input); } public static byte[] digestSHA1(byte[]... input) { return digest(SHA1, input); } public static byte[] digestSHA256(byte[]... input) { return digest(SHA256, input); } public static byte[] digest(String algorithm, byte[]... input) { return digest(algorithm, 1, input); } public static byte[] digest(String algorithm, int iterations, byte[]... input) { Queue<MessageDigest> queue = queues.get(algorithm); if (queue == null) { throw new IllegalStateException(sm.getString("concurrentMessageDigest.noDigest")); } 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 new IllegalStateException(sm.getString("concurrentMessageDigest.noDigest"), e); } } // Round 1 for (byte[] bytes : input) { md.update(bytes); } byte[] result = md.digest(); // Subsequent rounds if (iterations > 1) { for (int i = 1; i < iterations; i++) { md.update(result); result = md.digest(); } } queue.add(md); return result; } /** * Ensures that {@link #digest(String, byte[][])} will support the specified algorithm. This method <b>must</b> be * called and return successfully before using {@link #digest(String, byte[][])}. * * @param algorithm The message digest algorithm to be supported * * @throws NoSuchAlgorithmException If the algorithm is not supported by the JVM */ public static void init(String algorithm) throws NoSuchAlgorithmException { if (!queues.containsKey(algorithm)) { MessageDigest md = MessageDigest.getInstance(algorithm); Queue<MessageDigest> queue = new ConcurrentLinkedQueue<>(); queue.add(md); queues.putIfAbsent(algorithm, queue); } } }
Detected license expression
apache-2.0
Detected license expression (SPDX)
Apache-2.0
Percentage of license text
23.33
Copyrights

      
    
Holders

      
    
Authors

      
    
License detections License expression License expression SPDX
apache_2_0-4bde3f57-78aa-4201-96bf-531cba09e7de apache-2.0 Apache-2.0
URL Start line End line
http://www.apache.org/licenses/LICENSE-2.0 9 9