1 package gboat2.base.bridge.util.security;
2
3 import java.io.File;
4 import java.io.IOException;
5
6
7
8
9
10
11
12 public final class MD5Util extends Encryptor{
13
14 private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7',
15 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
16
17
18
19
20
21
22
23
24 public static String getFileMD5String(File file) {
25 byte[] bytes = digest(file, MD5);
26 return bufferToHex(bytes);
27 }
28
29
30
31
32
33
34
35 public static String getMD5String(String str) {
36 byte[] bytes = digest(str, MD5);
37 return bufferToHex(bytes);
38 }
39
40 private static String bufferToHex(byte[] bytes) {
41 return bufferToHex(bytes, 0, bytes.length);
42 }
43
44 private static String bufferToHex(byte[] bytes, int m, int n) {
45 StringBuffer stringbuffer = new StringBuffer(2 * n);
46 int k = m + n;
47 for (int l = m; l < k; l++) {
48 appendHexPair(bytes[l], stringbuffer);
49 }
50 return stringbuffer.toString();
51 }
52
53 private static void appendHexPair(byte bt, StringBuffer stringbuffer) {
54
55 char c0 = hexDigits[(bt & 0xf0) >> 4];
56 char c1 = hexDigits[bt & 0xf];
57 stringbuffer.append(c0);
58 stringbuffer.append(c1);
59 }
60 }