VerifySignature.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.security;

  21. import java.security.cert.Certificate;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.certificate.KeyStore;

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

  32.     private KeyStore keystore;
  33.     private Certificate certificate;
  34.    
  35.     public VerifySignature(KeyStore keystore) throws UtilsException{
  36.         this.keystore = keystore;
  37.         this.certificate = this.keystore.getCertificate();
  38.     }
  39.     public VerifySignature(KeyStore keystore, String alias) throws UtilsException{
  40.         this.keystore = keystore;
  41.         this.certificate = this.keystore.getCertificate(alias);
  42.     }
  43.    
  44.     public boolean verify(String data, String charsetName, byte[] signatureData, String algorithm) throws UtilsException{
  45.         try{
  46.             return this.verify(data.getBytes(charsetName), signatureData, algorithm);
  47.         }catch(Exception e){
  48.             throw new UtilsException(e.getMessage(),e);
  49.         }
  50.     }
  51.    
  52.     public boolean verify(byte[] data, byte[] signatureData, String algorithm) throws UtilsException{
  53.         try{
  54.             java.security.Signature sig = java.security.Signature.getInstance(algorithm);
  55.             sig.initVerify(this.certificate);
  56.             sig.update(data);
  57.             return sig.verify(signatureData);  
  58.         }catch(Exception e){
  59.             throw new UtilsException(e.getMessage(),e);
  60.         }
  61.     }

  62. }