Java examples for Security:SHA
get SHA1 hash for File
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { public static void main(String[] argv) throws Exception { File file = new File("Main.java"); System.out.println(getSHA1(file)); }//from www.jav a 2s . c o m public static String getSHA1(File file) throws IOException { try { MessageDigest md = MessageDigest.getInstance("SHA1"); FileInputStream fis = new FileInputStream(file); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } ; byte[] mdbytes = md.digest(); //convert the byte to hex format StringBuffer sb = new StringBuffer(""); for (int i = 0; i < mdbytes.length; i++) { sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16) .substring(1)); } return sb.toString(); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } } }