import java.io.*; 
import java.security.*; 
import sun.misc.*;
import javax.swing.*;
import java.awt.*;



public class JCE_SHA1
	{	
		
		public static void main(String args[]) throws Exception
			{ 	
										
				String filename=null;
				FileDialog fileDialog=new FileDialog(new Frame(),"Select file for digest");
				while(true)
					{	fileDialog.setVisible(true);
						filename=fileDialog.getDirectory()+fileDialog.getFile();
						if (filename!=null) break;
					};
				
				File dataFile=new File(filename);
				BufferedInputStream fileInput=new BufferedInputStream(new FileInputStream(dataFile));
				long fileSize=dataFile.length();
				long dataCounter=0;
				byte[] data;
				
				
				Security.addProvider(new com.sun.crypto.provider.SunJCE());			
				MessageDigest sha = MessageDigest.getInstance("SHA-1");
							
				
				while (dataCounter<fileSize)
					{	data=new byte[fileInput.available()];
						dataCounter=dataCounter+fileInput.read(data,0,fileInput.available());
						sha.update(data);
						
					};
				
				byte[] hash = sha.digest();
				String hashString=new String();
				for (int i=0;i<hash.length;i++) hashString=hashString+" "+hash[i];
				
				new JOptionPane().showMessageDialog(null,"DIGEST OK!\n"+"DIGEST LENGTH: "+hash.length+" byte\n"+"DIGEST: "+hashString);
				System.exit(0);				
			} 
						
	} 
