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

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

		try{
		// Create MessageDigest object
		MessageDigest sha1 = MessageDigest.getInstance("SHA-1");

		// 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);
			sha1.update(buffer,0,length);
		}
		bis.close();

		// Find the hash
		byte[] SHA1hash = sha1.digest();

		// Print/Store the hash
		FileOutputStream ofs = new FileOutputStream(args[0]+".sha1");
		ofs.write(SHA1hash);
		ofs.close();
		System.out.println("SHA-1 Hash of input file saved as "+args[0]+".sha1");

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

	}
}
