Skip to content

Commit

Permalink
增加文件取MD5值方法
Browse files Browse the repository at this point in the history
  • Loading branch information
panxw committed Jan 21, 2015
1 parent 3c9436f commit f505c10
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.java.alogrithm.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
Expand Down Expand Up @@ -42,6 +46,35 @@ public class SignatureHelper {
public static String encryptMD5(String strInput) {
return encyptByAlogrithm(strInput, ALGORITHM_MD5);
}

/**
* 取文件MD5值
* @param file
* @return
*/
public static String encryptFileMD5(File file) {
FileInputStream fis = null;
MessageDigest md;
try {
md = MessageDigest.getInstance(ALGORITHM_MD5);
fis = new FileInputStream(file);
byte[] buffer = new byte[102400];
int length;
while ((length = fis.read(buffer)) != -1) {
md.update(buffer, 0, length);
}
return Hex.encode(md.digest());
} catch (Exception e) {
return null;
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* 对字符串取SHA-1值
Expand Down

0 comments on commit f505c10

Please sign in to comment.