import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.DigestInputStream; class jsha256sum{ private File myfile; jsha256sum(String name){ myfile = new File(name); } public String gethash(){ String retstr = ""; MessageDigest md; FileInputStream fis = null; DigestInputStream dis = null; try{ md = MessageDigest.getInstance("SHA-256"); fis = new FileInputStream(myfile); dis = new DigestInputStream(fis, md); dis.on(true); byte buff[] = new byte[8192]; int bp; while((bp = dis.read(buff)) > 0){ } retstr = bytesToString(md.digest()); }catch(NoSuchAlgorithmException e){ retstr = "NoSuchAlgorithmException"; }catch(FileNotFoundException e){ retstr = "FileNotFoundException"; }catch(IOException e){ retstr = "IOException"; }finally{ try{ dis.close(); }catch(IOException e){} try{ fis.close(); }catch(IOException e){} } return retstr; } public String bytesToString(byte b[]){ String hexs[] = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; int len = b.length; String str = ""; int p, q; for(int i=0; i> 4) & 0x0f; str += hexs[p] + hexs[q]; } return str; } public static void main(String args[]){ if(args.length < 1){ System.out.println("Usage: jsha256sum filename"); System.exit(1); } jsha256sum sha256 = new jsha256sum(args[0]); System.out.println(sha256.gethash()); } }