package model;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import sun.misc.BASE64Decoder;

public class MySignature {
	
	public boolean verify(String data, String signature, String cerFile) throws Exception{
		byte[] signatureB = new BASE64Decoder().decodeBuffer(signature);
		
		Signature s = Signature.getInstance("MD5WithRSA");
		
		// Load File CertUser
		FileInputStream cerIn1 = new FileInputStream(cerFile);
		BouncyCastleProvider provider1 = new BouncyCastleProvider();
		CertificateFactory cf1 = CertificateFactory.getInstance("X.509" , provider1);
		X509Certificate cert1= (X509Certificate) cf1.generateCertificate(cerIn1);
		// End
		
		s.initVerify(cert1.getPublicKey());
		byte[] dataB = data.getBytes("UTF-8");
		s.update(dataB);

		return s.verify(signatureB);
	}
	
}
