CryptFactory.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.crypt;

  21. import org.apache.commons.lang.StringUtils;
  22. import org.openspcoop2.utils.Utilities;
  23. import org.openspcoop2.utils.UtilsException;
  24. import org.slf4j.Logger;

  25. /**
  26.  * CryptFactory
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class CryptFactory {

  33.     public static ICrypt getCrypt(Logger log, CryptConfig config) throws UtilsException {
  34.        
  35.         if(config.getCryptType()!=null) {
  36.             return getCrypt(log, config.getCryptType(), config);
  37.         }
  38.         else {
  39.             String className = config.getCryptCustomType();
  40.             if(StringUtils.isEmpty(className)) {
  41.                 throw new UtilsException("Property '"+CryptConfig.CRYPT_TYPE+"' or '"+CryptConfig.CRYPT_CUSTOM_TYPE+"' are required");
  42.             }
  43.             return getCrypt(log, className, config);
  44.         }

  45.     }
  46.    
  47.     public static ICrypt getCrypt(Logger log, CryptType type, CryptConfig config) {
  48.         return _getCrypt(log, type, config);
  49.     }
  50.     public static ICrypt getCrypt(CryptType type, CryptConfig config) {
  51.         return _getCrypt(null, type, config);
  52.     }
  53.     public static ICrypt getCrypt(Logger log, CryptType type) {
  54.         return _getCrypt(log, type, null);
  55.     }
  56.     public static ICrypt getCrypt(CryptType type) {
  57.         return _getCrypt(null, type, null);
  58.     }
  59.     private static ICrypt _getCrypt(Logger log, CryptType type, CryptConfig config) {
  60.         switch (type) {
  61.        
  62.         case LIBC_CRYPT_MD5:
  63.             return getCodecCrypt(log, CodecType.LIBC_CRYPT_MD5, config);
  64.         case LIBC_CRYPT_MD5_APACHE:
  65.             return getCodecCrypt(log, CodecType.LIBC_CRYPT_MD5_APACHE, config);
  66.         case SHA2_BASED_UNIX_CRYPT_SHA256:
  67.             return getCodecCrypt(log, CodecType.SHA2_BASED_UNIX_CRYPT_SHA256, config);
  68.         case SHA2_BASED_UNIX_CRYPT_SHA512:
  69.             return getCodecCrypt(log, CodecType.SHA2_BASED_UNIX_CRYPT_SHA512, config);
  70.         case DES_UNIX_CRYPT:
  71.             return getCodecCrypt(log, CodecType.DES_UNIX_CRYPT, config);
  72.            
  73.         case RFC2307_MD5:
  74.             return getJasyptCrypt(log, JasyptType.RFC2307_MD5);
  75.         case RFC2307_SMD5:
  76.             return getJasyptCrypt(log, JasyptType.RFC2307_SMD5);
  77.         case RFC2307_SHA:
  78.             return getJasyptCrypt(log, JasyptType.RFC2307_SHA);
  79.         case RFC2307_SSHA:
  80.             return getJasyptCrypt(log, JasyptType.RFC2307_SSHA);
  81.            
  82.         case JASYPT_BASIC_PASSWORD:
  83.             return getJasyptCrypt(log, JasyptType.JASYPT_BASIC_PASSWORD);
  84.         case JASYPT_STRONG_PASSWORD:
  85.             return getJasyptCrypt(log, JasyptType.JASYPT_STRONG_PASSWORD);
  86.         case JASYPT_CUSTOM_PASSWORD:
  87.             return getJasyptCrypt(log, config);
  88.            
  89.         case PBE_KEY_SPEC:{
  90.             PBEKeySpecCrypt pbe = new PBEKeySpecCrypt();
  91.             pbe.init(log, config);
  92.             return pbe;
  93.         }
  94.        
  95.         case B_CRYPT:
  96.             return getSpringCrypt(log, SpringType.B_CRYPT);
  97.         case S_CRYPT:
  98.             return getSpringCrypt(log, SpringType.S_CRYPT);
  99.                
  100.         case PLAIN:{
  101.             PlainCrypt pbe = new PlainCrypt();
  102.             pbe.init(log, config);
  103.             return pbe;
  104.         }
  105.        
  106.         }
  107.        
  108.         return null;
  109.     }
  110.    
  111.     public static ICrypt getCrypt(Logger log, String className, CryptConfig config) throws UtilsException {
  112.         return _getCrypt(log, className, config);
  113.     }
  114.     public static ICrypt getCrypt( String className, CryptConfig config) throws UtilsException {
  115.         return _getCrypt(null, className, config);
  116.     }
  117.     public static ICrypt getCrypt(Logger log, String className) throws UtilsException {
  118.         return _getCrypt(log, className, null);
  119.     }
  120.     public static ICrypt getCrypt(String className) throws UtilsException {
  121.         return _getCrypt(null, className, null);
  122.     }
  123.     @SuppressWarnings("unchecked")
  124.     private static ICrypt _getCrypt(Logger log, String className, CryptConfig config) throws UtilsException {
  125.         Class<ICrypt> c = null;
  126.         try {
  127.             c = (Class<ICrypt>) Class.forName(className);
  128.         }catch(Throwable e) {
  129.             throw new UtilsException(e.getMessage(), e);
  130.         }
  131.         return _getCrypt(log, c, config);
  132.     }
  133.    
  134.     public static ICrypt getCrypt(Logger log, Class<ICrypt> c, CryptConfig config) throws UtilsException {
  135.         return _getCrypt(log, c, config);
  136.     }
  137.     public static ICrypt getCrypt( Class<ICrypt> c, CryptConfig config) throws UtilsException {
  138.         return _getCrypt(null, c, config);
  139.     }
  140.     public static ICrypt getCrypt(Logger log, Class<ICrypt> c) throws UtilsException {
  141.         return _getCrypt(log, c, null);
  142.     }
  143.     public static ICrypt getCrypt(Class<ICrypt> c) throws UtilsException {
  144.         return _getCrypt(null, c, null);
  145.     }
  146.     private static ICrypt _getCrypt(Logger log, Class<ICrypt> c, CryptConfig config) throws UtilsException {
  147.         try {
  148.             ICrypt crypt = Utilities.newInstance(c);
  149.             crypt.init(log, config);
  150.             return crypt;
  151.         }catch(Exception e) {
  152.             throw new UtilsException(e.getMessage(), e);
  153.         }
  154.     }
  155.    
  156.    
  157.     // Sottoclassi specifiche
  158.    
  159.     @SuppressWarnings("deprecation")
  160.     public static OldMD5Crypt getOldMD5Crypt(Logger log) {
  161.         OldMD5Crypt old = new OldMD5Crypt();
  162.         old.init(log, null);
  163.         return old;
  164.     }
  165.     @SuppressWarnings("deprecation")
  166.     public static OldMD5Crypt getOldMD5Crypt() {
  167.         return getOldMD5Crypt(null);
  168.     }
  169.    
  170.     public static CodecCrypt getCodecCrypt(Logger log, CodecType type, CryptConfig config) {
  171.         CodecCrypt c = new CodecCrypt(type);
  172.         c.init(log, config);
  173.         return c;
  174.     }
  175.     public static CodecCrypt getCodecCrypt(CodecType type, CryptConfig config) {
  176.         return getCodecCrypt(null, type, config);
  177.     }
  178.     public static CodecCrypt getCodecCrypt(Logger log, CodecType type) {
  179.         return getCodecCrypt(log, type, null);
  180.     }
  181.     public static CodecCrypt getCodecCrypt(CodecType type) {
  182.         return getCodecCrypt(null, type, null);
  183.     }
  184.    
  185.     public static JasyptCrypt getJasyptCrypt(Logger log, CryptConfig config) {
  186.         JasyptCrypt j = new JasyptCrypt();
  187.         j.init(log, config);
  188.         return j;
  189.     }
  190.     public static JasyptCrypt getJasyptCrypt(CryptConfig config) {
  191.         return getJasyptCrypt(null, config);
  192.     }
  193.     public static JasyptCrypt getJasyptCrypt(Logger log, JasyptType type) {
  194.         JasyptCrypt j = new JasyptCrypt(type);
  195.         j.init(log, null);
  196.         return j;
  197.     }
  198.     public static JasyptCrypt getJasyptCrypt(JasyptType type) {
  199.         return getJasyptCrypt(null, type);
  200.     }
  201.    
  202.     public static SpringCrypt getSpringCrypt(Logger log, SpringType type) {
  203.         SpringCrypt s = new SpringCrypt(type);
  204.         s.init(log, null);
  205.         return s;
  206.     }
  207.     public static SpringCrypt getSpringCrypt(SpringType type) {
  208.         return getSpringCrypt(null, type);
  209.     }
  210. }