import java.io.*;
import java.security.*;

class MD5
{
	public static void main(String args[])
	{
		if(args.length!=1){
			System.out.println("Usage: java MD5 <input_file>");
			System.exit(0);
		}

		try{
		// Create MessageDigest object
		MessageDigest md5 = MessageDigest.getInstance("MD5");

		// Read data from input file
		FileInputStream fis = new FileInputStream(args[0]);
		BufferedInputStream bis = new BufferedInputStream(fis);
		byte buffer[] = new byte[1024];
		int length;
		while(bis.available() != 0){
			length = bis.read(buffer);
			md5.update(buffer,0,length);
		}
		bis.close();

		// Find the hash
		byte[] MD5hash = md5.digest();

		// Print/Store the hash
		//System.out.println("MD-5 Hash of input file is: " + MD5hash);
		FileOutputStream ofs = new FileOutputStream(args[0]+".md5");
		ofs.write(MD5hash);
		ofs.close();
		System.out.println("MD5 Hash of input file saved as "+args[0]+".md5");

		}catch(Exception e){
			e.printStackTrace();
		}

	}
}
