Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.security.MessageDigest; public class Main { private final static String SHA1 = "SHA-1"; public static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String getFileSHA1(File file) { return toHexString(getFileDigest(file, SHA1)); } public static String toHexString(byte[] b) { StringBuilder sb = new StringBuilder(b.length * 2); for (int i = 0; i < b.length; i++) { sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]); sb.append(HEX_DIGITS[b[i] & 0x0f]); } return sb.toString(); } public static byte[] getFileDigest(File file, String algorithm) { InputStream fis = null; byte[] buffer = new byte[1024]; int numRead; MessageDigest md; try { fis = new FileInputStream(file); md = MessageDigest.getInstance(algorithm); while ((numRead = fis.read(buffer)) > 0) { md.update(buffer, 0, numRead); } fis.close(); return md.digest(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return null; } }