import java.io.*;
import java.security.*;
import java.util.*;
import java.sql.*;

class DSASignVeri
{
	public static void main(String args[])
	{
		if(args.length!=3){
			System.out.println("Usage: DSASignVeri <public-key-file> <signature_file> <message-file>");
			System.exit(0);
		}
		try{
			/************************* BEGIN Get Public Key ***************************/
			
			FileInputStream fis = new FileInputStream(args[0]);
			ObjectInputStream ois = new ObjectInputStream(fis);
			PublicKey publickey = (PublicKey) ois.readObject();
			ois.close();
		
			long start = getCurrentTime(); //Start the watch
	
			/************* Create a Signature object and initialize it with public key **************/
			Signature dsa = Signature.getInstance("SHA1withDSA","SUN");
			dsa.initVerify(publickey);

			/************************* Update and verify the data ***************************/
			fis = new FileInputStream(args[2]);
			BufferedInputStream bis = new BufferedInputStream(fis);
			byte buffer[] = new byte[1024];
			int length;
			int size = 0; //Size of file (message);	
			while(bis.available() != 0){
				length = bis.read(buffer);
				size += length;
				dsa.update(buffer,0,length);
			}
			bis.close();

			/*
			 * Now, that all the data to be verified is read in,
			 * and hash generated ( using update method )
			 * verify the  signature for it
			 */
			
			/************************  First read the Signature ****************************/	

			fis = new FileInputStream(args[1]);
			bis = new BufferedInputStream(fis);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int ch;
			while((ch = bis.read())!=-1)
			{
				baos.write(ch);
			}		
			byte [] buf = baos.toByteArray();

			/*********************** Verify the signature ********************************/

			if(dsa.verify(buf))
			{
			//	System.out.println("Valid Signature");
			}
			else
			{
			//	System.out.println("In-valid Signature");
			}
			long end = getCurrentTime();
			long time_sign_veri = end - start;
			System.out.println("  SHA1withDSA\t\t" + size + "\t\t" + time_sign_veri);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

/***************************Function to get current time (in milliseconds) ******************************/

        public static long getCurrentTime()
        {
              	java.util.Date date=Calendar.getInstance().getTime();
	        Timestamp a=new Timestamp(date.getTime()); 
        	long currentTime=Calendar.getInstance().getTimeInMillis();
                //System.out.println("Time: "+currentTime);
                return currentTime;
        }
}
