|
2 | 2 |
|
3 | 3 | import android.text.TextUtils; |
4 | 4 |
|
| 5 | +import java.io.File; |
| 6 | +import java.io.FileInputStream; |
| 7 | +import java.math.BigInteger; |
5 | 8 | import java.nio.charset.Charset; |
6 | 9 | import java.security.MessageDigest; |
7 | 10 | import java.security.NoSuchAlgorithmException; |
@@ -94,28 +97,26 @@ private static String SHA(final String strText, final String strType) { |
94 | 97 | return strResult; |
95 | 98 | } |
96 | 99 |
|
97 | | - public static String EncoderByMd5(String buf) { |
| 100 | + public static String EncoderByMd5(File file) { |
| 101 | + if (!file.isFile()) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + String encodeMd5String; |
98 | 105 | try { |
99 | | - MessageDigest digist = MessageDigest.getInstance("MD5"); |
100 | | - byte[] rs = digist.digest(buf.getBytes("UTF-8")); |
101 | | - StringBuffer digestHexStr = new StringBuffer(); |
102 | | - for (int i = 0; i < 16; i++) { |
103 | | - digestHexStr.append(byteHEX(rs[i])); |
| 106 | + MessageDigest digest = MessageDigest.getInstance("MD5"); |
| 107 | + FileInputStream in = new FileInputStream(file); |
| 108 | + byte buffer[] = new byte[1024]; |
| 109 | + int len; |
| 110 | + while ((len = in.read(buffer, 0, 1024)) != -1) { |
| 111 | + digest.update(buffer, 0, len); |
104 | 112 | } |
105 | | - return digestHexStr.toString(); |
| 113 | + in.close(); |
| 114 | + BigInteger bigInt = new BigInteger(1, digest.digest()); |
| 115 | + encodeMd5String = bigInt.toString(16); |
106 | 116 | } catch (Exception e) { |
107 | 117 | e.printStackTrace(); |
| 118 | + encodeMd5String = null; |
108 | 119 | } |
109 | | - return null; |
110 | | - |
111 | | - } |
112 | | - |
113 | | - public static String byteHEX(byte ib) { |
114 | | - char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
115 | | - char[] ob = new char[2]; |
116 | | - ob[0] = Digit[(ib >>> 4) & 0X0F]; |
117 | | - ob[1] = Digit[ib & 0X0F]; |
118 | | - String s = new String(ob); |
119 | | - return s; |
| 120 | + return encodeMd5String; |
120 | 121 | } |
121 | 122 | } |
0 commit comments