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

class DSASignGen
{
	public static void main(String args[])
	{
		if(args.length!=2){
			System.out.println("Usage: DSASignGen <key_size> <file_to_sign>");
			System.exit(0);
		}
		try{

			/************************* BEGIN Generate a key pair ***************************/
			KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA","SUN");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");
			int keySize = Integer.parseInt( args[0] ) ;

			keygen.initialize(keySize,random);

			KeyPair keypair = keygen.generateKeyPair();
			PrivateKey privatekey = keypair.getPrivate();
			PublicKey publickey = keypair.getPublic();
			/************************* END Generate a key pair ***************************/

			long start = getCurrentTime(); //Start the watch

			/************* Create a Signature object and initialize it with private key **************/
			Signature dsa = Signature.getInstance("SHA1withDSA","SUN");
			dsa.initSign(privatekey);

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

			/*
			 * Now, that all the data t obe signed is read in,
			 * and hash generated ( using update method )
			 * generate a signature for it
			 */
			byte[] realsign = dsa.sign();
			
			long end = getCurrentTime(); //End of signature calculation
			long time_sign = end - start; //Calculate total time

			/*********** Save signature in a file *************/
			FileOutputStream sigfos = new FileOutputStream(args[1]+".dsa_sign");
			sigfos.write(realsign);
			sigfos.close();

			/*********** Save Public key in a file *************/
			FileOutputStream pubkeyfos = new FileOutputStream(args[1]+".dsa_public_key");
			ObjectOutputStream oos = new ObjectOutputStream(pubkeyfos);
			oos.writeObject(publickey);
			oos.close();

			/*********** Save Private key in a file *************/
			FileOutputStream prikeyfos = new FileOutputStream(args[1]+".dsa_private_key");
			prikeyfos.write(privatekey.getEncoded());
			prikeyfos.close();
			System.out.println("  DSA\t\t\t" + size + "\t\t" + time_sign + "\t\t" + keySize); 
		}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;
        }

}
