MD5.java
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.sunyo.energy.location.demopay;
import java.io.*;
import java.security.*;
public class MD5 {
public static String md5Str(String str) {
if (str == null) return "";
return md5Str(str, 0);
}
/**
* ������ϢժҪ��
*
* @param data ����ժҪ�����ݡ�
* @param offset ����ƫ�Ƶ�ַ��
* @param length ���ݳ��ȡ�
* @return ժҪ�����(16�ֽ�)
*/
public static String md5Str(String str, int offset) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] b = str.getBytes("UTF8");
md5.update(b, offset, b.length);
return byteArrayToHexString(md5.digest());
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
return null;
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return null;
}
}
/**
* @param b byte[]
* @return String
*/
public static String byteArrayToHexString(byte[] b) {
String result = "";
for (int i = 0; i < b.length; i++) {
result += byteToHexString(b[i]);
}
return result;
}
private static String[] hexDigits =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b",
"c", "d", "e", "f"};
/**
* ���ֽ�ת��Ϊ��Ӧ��16��������
*
* @param b byte
* @return String
*/
public static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static void main(String[] args) {
System.out.println(byteToHexString((byte) -99));
String str =
"eeeeeeeeeeeeeewrw213123122222222222222222222222213123213213213erwer";
String ened = MD5.md5Str(str);
System.out.println(ened.length());
System.out.println(ened);
}
}