DigestUtils.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.util.HashMap;
  23. import java.util.Map;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.io.Base64Utilities;
  26. import org.openspcoop2.utils.io.HexBinaryUtilities;

  27. /**
  28.  * DigestUtils
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class DigestUtils {
  35.    
  36.     private DigestUtils() {}

  37.     private static UtilsException newUtilsExceptionDigestEncodingUndefined() {
  38.         return new UtilsException("Digest encoding undefined");
  39.     }
  40.    
  41.     public static byte[] getDigestValue(byte[] content, String algorithm) throws UtilsException{
  42.         if(content==null) {
  43.             throw new UtilsException("Digest content undefined");
  44.         }
  45.         if(algorithm==null) {
  46.             throw new UtilsException("Digest algorithm undefined");
  47.         }
  48.        
  49.         MessageDigest digest = null;
  50.         try {
  51.             digest = MessageDigestFactory.getMessageDigest(algorithm);
  52.         }catch(Exception e) {
  53.             throw new UtilsException("Message digest (algorithm: '"+algorithm+"') initialization failed: "+e.getMessage(),e);
  54.         }
  55.         try {
  56.             return digest.digest(content); // md5Data
  57.         }catch(Exception e) {
  58.             throw new UtilsException(e.getMessage(),e);
  59.         }
  60.     }
  61.    
  62.     public static String getDigestValue(byte[] content, String algorithm, DigestEncoding digestEncoding) throws UtilsException{
  63.         return getDigestValue(content, algorithm, digestEncoding, false);
  64.     }
  65.     public static String getDigestValue(byte[] content, String algorithm, DigestEncoding digestEncoding, boolean rfc3230) throws UtilsException{
  66.         byte[]md5Data = getDigestValue(content, algorithm);
  67.         if(digestEncoding==null) {
  68.             throw newUtilsExceptionDigestEncodingUndefined();
  69.         }
  70.         return encode(md5Data, digestEncoding, rfc3230, algorithm);
  71.     }
  72.    
  73.     public static Map<DigestEncoding, String> getDigestValues(byte[] content, String algorithm, DigestEncoding ... digestEncoding) throws UtilsException{
  74.         return getDigestValues(content, algorithm, false, digestEncoding);
  75.     }
  76.     public static Map<DigestEncoding, String> getDigestValues(byte[] content, String algorithm, boolean rfc3230, DigestEncoding ... digestEncoding) throws UtilsException{

  77.         byte[]md5Data = getDigestValue(content, algorithm);
  78.         if(digestEncoding==null || digestEncoding.length<=0) {
  79.             throw newUtilsExceptionDigestEncodingUndefined();
  80.         }
  81.        
  82.         Map<DigestEncoding, String> map = new HashMap<>();
  83.         for (DigestEncoding de : digestEncoding) {
  84.             try {
  85.                 String digestValue = encode(md5Data, de, rfc3230, algorithm);
  86.                 map.put(de, digestValue);
  87.             }catch(Exception e) {
  88.                 throw new UtilsException(e.getMessage(),e);
  89.             }
  90.         }
  91.         return map;
  92.     }
  93.    
  94.     private static String encode(byte[] md5Data, DigestEncoding digestEncoding, boolean rfc3230, String algorithm) throws UtilsException{

  95.         if(digestEncoding==null) {
  96.             throw newUtilsExceptionDigestEncodingUndefined();
  97.         }
  98.        
  99.         try {
  100.             String digestValue = null;
  101.             if(DigestEncoding.HEX.equals(digestEncoding)) {
  102.                 digestValue = HexBinaryUtilities.encodeAsString(md5Data);
  103.             }
  104.             else if(DigestEncoding.BASE64.equals(digestEncoding)) {
  105.                 digestValue = Base64Utilities.encodeAsString(md5Data);
  106.             }
  107.             else {
  108.                 throw new UtilsException("DigestEncoding '"+digestEncoding+"' unsupported");
  109.             }
  110.             if(rfc3230) {
  111.                 return algorithm+"="+digestValue;
  112.             }
  113.             else {
  114.                 return digestValue;
  115.             }
  116.         }catch(Exception e) {
  117.             throw new UtilsException(e.getMessage(),e);
  118.         }
  119.     }
  120. }