Coverage Summary for Class: StandardByteDigesterPool (com.acciente.oacc.encryptor.jasypt)

Class Class, % Method, % Line, %
StandardByteDigesterPool 100% (1/ 1) 100% (4/ 4) 100% (19/ 19)


1 /* 2  * Copyright 2009-2017, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * License. You may obtain a copy of the License at 8  * 9  * http://www.apache.org/licenses/LICENSE-2.0 10  * 11  * Unless required by applicable law or agreed to in 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18 package com.acciente.oacc.encryptor.jasypt; 19  20 import org.jasypt.digest.StandardByteDigester; 21  22 import java.util.Map; 23 import java.util.concurrent.ConcurrentHashMap; 24  25 /** 26  * Keeps a pool of Jasypt digester instances, there is one instance in the pool per unique digester configuration. 27  */ 28 class StandardByteDigesterPool { 29  private final Map<String, Map<Integer, Map<Integer, StandardByteDigester>>> algorithmMap = new ConcurrentHashMap<>(); 30  31  public StandardByteDigester getStandardByteDigester(String algorithm, int iterations, int saltSizeBytes) { 32  final Map<Integer, StandardByteDigester> saltSizeBytesMap = getInnerMap(getInnerMap(algorithmMap, algorithm), iterations); 33  34  StandardByteDigester standardByteDigester = saltSizeBytesMap.get(saltSizeBytes); 35  if (standardByteDigester == null) { 36  standardByteDigester = newStandardByteDigester(algorithm, iterations, saltSizeBytes); 37  saltSizeBytesMap.put(saltSizeBytes, standardByteDigester); 38  } 39  40  return standardByteDigester; 41  } 42  43  private static <K, IK, IV> Map<IK, IV> getInnerMap(Map<K, Map<IK, IV>> outerMap, K outerKey) { 44  Map<IK, IV> innerMap = outerMap.get(outerKey); 45  if (innerMap == null) { 46  innerMap = new ConcurrentHashMap<>(); 47  outerMap.put(outerKey, innerMap); 48  } 49  return innerMap; 50  } 51  52  private static StandardByteDigester newStandardByteDigester(String algorithm, int iterations, int saltSizeBytes) { 53  final StandardByteDigester standardByteDigester = new StandardByteDigester(); 54  standardByteDigester.setAlgorithm(algorithm); 55  standardByteDigester.setIterations(iterations); 56  standardByteDigester.setSaltSizeBytes(saltSizeBytes); 57  standardByteDigester.initialize(); 58  return standardByteDigester; 59  } 60 }