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

class SHA
{
	public static void main(String args[])
	{
		if(args.length!=1){
			System.out.println("Usage: SHA <message-file>");
			System.exit(0);
		}
		try{

			/************************* Read the data in to a byte-array***************************/

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

			long start = getCurrentTime();

			/*********************** Get message-digest instance ********************************/
			
			MessageDigest sha = MessageDigest.getInstance("SHA-1");
			
			/*********************** Initialize the process ********************************/

			sha.reset();
			
			/*********************** Calculate the digest  ********************************/

			sha.update(buffer);

			/*********************** Fetch the digest, and output to a file  ********************************/

			byte[] digest = sha.digest();

			long end = getCurrentTime();

			System.out.println("  SHA1\t\t\t" + buffer.length+ "\t\t" + (end-start));	
			FileOutputStream fos = new FileOutputStream(args[0]+".sha");
			fos.write(digest);
			fos.close();

		}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;
        }
}
