SpringCrypt.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.openspcoop2.utils.UtilsException;
  22. import org.slf4j.Logger;
  23. import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;

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

  32.     private Logger log;
  33.     private SpringType type;
  34.    
  35.     public SpringCrypt(SpringType type){
  36.         this.type = type;
  37.         if(this.type == null) {
  38.             this.type = SpringType.B_CRYPT;
  39.         }
  40.     }
  41.    
  42.     @Override
  43.     public void init(Logger log, CryptConfig config) {
  44.         this.log = log;
  45.     }
  46.    
  47.     @Override
  48.     public String crypt(String password) throws UtilsException {
  49.        
  50.         switch (this.type) {
  51.         case B_CRYPT:
  52.             org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder b = new org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder();
  53.             return b.encode(password);
  54.         case S_CRYPT:
  55.             org.springframework.security.crypto.scrypt.SCryptPasswordEncoder s = SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8();
  56.             return s.encode(password);
  57.         }
  58.         throw new UtilsException("Unsupported type '"+this.type+"'");
  59.        
  60.     }
  61.    
  62.     @Override
  63.     public boolean check(String password, String pwcrypt) {
  64.         try {
  65.            
  66.             switch (this.type) {
  67.             case B_CRYPT:
  68.                 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder b = new org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder();
  69.                 return b.matches(password, pwcrypt);
  70.             case S_CRYPT:
  71.                 org.springframework.security.crypto.scrypt.SCryptPasswordEncoder s = SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8();
  72.                 return s.matches(password, pwcrypt);
  73.             }
  74.             throw new UtilsException("Unsupported type '"+this.type+"'");
  75.            
  76.         }catch(Throwable e){
  77.             if(this.log!=null) {
  78.                 this.log.error("Verifica password '"+pwcrypt+"' fallita: "+e.getMessage(),e);
  79.             }
  80.             return false;
  81.         }
  82.     }

  83. }