|
|
@@ -3,6 +3,10 @@ package com.atmob.common.crypto;
|
|
|
import android.util.Base64;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.security.MessageDigest;
|
|
|
@@ -480,15 +484,34 @@ public final class CryptoUtils {
|
|
|
|
|
|
public static String md5(InputStream is) {
|
|
|
try {
|
|
|
- return md5(IOUtils.readBytes(is));
|
|
|
+ return new String(encodeHex(getMd5(is)));
|
|
|
} catch (Exception e) {
|
|
|
- Log.w("exception", e);
|
|
|
return "";
|
|
|
} finally {
|
|
|
IOUtils.closeQuietly(is);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public static String md5(File file) {
|
|
|
+ try {
|
|
|
+ return md5(new FileInputStream(file));
|
|
|
+ } catch (FileNotFoundException ignore) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static byte[] getMd5(InputStream in) throws NoSuchAlgorithmException, IOException {
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len;
|
|
|
+ MessageDigest digest = MessageDigest.getInstance("MD5");
|
|
|
+ while ((len = in.read(buffer, 0, 1024)) != -1) {
|
|
|
+ digest.update(buffer, 0, len);
|
|
|
+ }
|
|
|
+
|
|
|
+ return digest.digest();
|
|
|
+ }
|
|
|
+
|
|
|
public static byte[] md5Bytes(byte[] data) {
|
|
|
return getDigest(MD5).digest(data);
|
|
|
}
|