1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.util; |
21 |
| |
22 |
| import java.security.MessageDigest; |
23 |
| import java.security.NoSuchAlgorithmException; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| public class MD5 { |
30 |
| private static String[] hex = {"0", "1", "2", "3", "4", "5", "6", "7", |
31 |
| "8", "9", "a", "b", "c", "d", "e", "f"}; |
32 |
| |
33 |
0
| public static String md(String pswd) {
|
34 |
0
| MessageDigest md;
|
35 |
0
| String digestString = "";
|
36 |
| |
37 |
0
| try {
|
38 |
0
| md = MessageDigest.getInstance("MD5");
|
39 |
0
| md.update(pswd.getBytes("utf-8"));
|
40 |
| |
41 |
0
| byte[] digest = md.digest();
|
42 |
| |
43 |
0
| digestString = byteArrayToHexString(digest);
|
44 |
| } catch (NoSuchAlgorithmException e) { |
45 |
0
| System.out.println("MD5 not supported.");
|
46 |
0
| e.printStackTrace(System.err);
|
47 |
| } catch (Exception e) { |
48 |
0
| e.printStackTrace(System.err);
|
49 |
| } |
50 |
| |
51 |
0
| return digestString;
|
52 |
| } |
53 |
| |
54 |
0
| static String byteToHexString(byte b) {
|
55 |
0
| int n = b;
|
56 |
| |
57 |
0
| if (n < 0) {
|
58 |
0
| n = 256 + n;
|
59 |
| } |
60 |
| |
61 |
0
| int d1 = n / 16;
|
62 |
0
| int d2 = n % 16;
|
63 |
| |
64 |
0
| return hex[d1] + hex[d2];
|
65 |
| } |
66 |
| |
67 |
0
| static String byteArrayToHexString(byte[] b) {
|
68 |
0
| StringBuffer result = new StringBuffer(b.length * 2);
|
69 |
| |
70 |
0
| for (int i = 0; i < b.length; ++i) {
|
71 |
0
| result.append(byteToHexString(b[i]));
|
72 |
| } |
73 |
| |
74 |
0
| return result.toString();
|
75 |
| } |
76 |
| } |