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

class MD5
{
	public static void main(String args[])
	{
		if(args.length!=1){
			System.out.println("Usage: MD5 <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 md5 = MessageDigest.getInstance("MD5");
			
			/*********************** Initialize the process ********************************/

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

			md5.update(buffer);

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

			byte[] digest = md5.digest();
			long end = getCurrentTime();
			System.out.println("  MD5\t\t\t" + buffer.length + "\t\t" + (end-start));	
			FileOutputStream fos = new FileOutputStream(args[0]+".md5");
			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;
        }
}
