DigestImpl.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.digest;

  21. import java.security.MessageDigest;
  22. import java.security.NoSuchAlgorithmException;
  23. import java.util.Base64;

  24. import org.bouncycastle.jcajce.provider.digest.SHA3;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.slf4j.Logger;

  27. /**
  28.  * DigestImpl
  29.  *
  30.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class DigestImpl implements IDigest {

  35.     protected DigestConfig config;
  36.     protected Logger log;
  37.    
  38.     @Override
  39.     public void init(Logger log, DigestConfig config) {
  40.         this.config = config;
  41.         this.log = log;
  42.     }

  43.    
  44.     private byte[] standardDigest(byte[] msg) throws NoSuchAlgorithmException {
  45.         MessageDigest digest = MessageDigest.getInstance(this.config.getDigestType().getAlgorithmName());
  46.         return digest.digest(msg);
  47.     }
  48.    
  49.     private byte[] shakeDigest(byte[] msg) throws NoSuchAlgorithmException {
  50.         MessageDigest digest = null;
  51.        
  52.         if (this.config.getDigestType().equals(DigestType.SHAKE128)) {
  53.             digest = new SHA3.DigestShake128_256();
  54.         } else if (this.config.getDigestType().equals(DigestType.SHAKE256)) {
  55.             digest = new SHA3.DigestShake256_512();
  56.         } else {
  57.             throw new NoSuchAlgorithmException("digest algorithm : " + this.config.getDigestType().getAlgorithmName() + " not found ");
  58.         }
  59.         return digest.digest(msg);
  60.     }
  61.    
  62.    
  63.    
  64.     @Override
  65.     public byte[] digest(byte[] input, byte[] salt) throws UtilsException {
  66.         byte[] output = null;
  67.        
  68.         if (salt.length != this.config.getSaltLength())
  69.             throw new UtilsException("lunghezza salt fornito: " + salt.length + ", lunghezza attesa: " + this.config.getSaltLength());
  70.        
  71.         byte[] msg = this.config.composeMessage(input, salt);
  72.        
  73.         try {
  74.             switch (this.config.getDigestType()) {
  75.             case SHAKE128:
  76.             case SHAKE256:
  77.                 output = shakeDigest(msg);
  78.                 break;
  79.             case SHA256:
  80.             case SHA384:
  81.             case SHA3_256:
  82.             case SHA3_384:
  83.             case SHA3_512:
  84.             case SHA512:
  85.             case SHA512_256:
  86.                 output = standardDigest(msg);
  87.                 break;
  88.             }
  89.         } catch (NoSuchAlgorithmException e) {
  90.             throw new UtilsException("Algorithm not found: " + this.config.getDigestType().getAlgorithmName(), e);
  91.         }
  92.        
  93.         if (this.config.isBase64Encode()) {
  94.             output = Base64.getEncoder().encode(output);
  95.         }
  96.        
  97.         return output;
  98.     }

  99.     @Override
  100.     public boolean check(byte[] input, byte[] salt, byte[] digest) throws UtilsException {
  101.        
  102.         byte[] generated = this.digest(input, salt);
  103.         if (generated.length != digest.length)
  104.             return false;
  105.        
  106.         boolean isEqual = true;
  107.         for (int i = 0; i < generated.length; i++) {
  108.             isEqual &= (generated[i] == digest[i]);
  109.         }
  110.         return isEqual;
  111.     }

  112. }