Here you can find the source of getHashMD5(File file)
Parameter | Description |
---|---|
file | a parameter |
public static String getHashMD5(File file)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class Main { /**// ww w . j a v a 2s . c o m * Generates MD5 hash for a given file. * * @param file * @return */ public static String getHashMD5(File file) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(file); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); } fis.close(); byte[] mdbytes = md.digest(); // convert the byte to hex format method 1 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 | IOException e) { e.printStackTrace(); } return null; } }