package com.utils.md5;
import java.io.*;
import java.security.*;
public class MD5 {
public static String hex(byte[] array) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
.toUpperCase().substring(1, 3));
}
return sb.toString();
}
public static String md5(String message) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");// 想用SHA加密的话就把MD5换成SHA吧
return hex(md.digest(message.getBytes("CP1252")));
} catch (NoSuchAlgorithmException e) {
} catch (UnsupportedEncodingException e) {
}
return null;
}
}
package com.utils.junit;
import com.utils.md5.MD5;
import junit.framework.TestCase;
public class MD5Test extends TestCase {
@Test
public void md5() {
String s = "123";
System.out.println(s);
String str = MD5.md5(s);
System.out.println(str);
String st = MD5.md5(s);
System.out.println(st);
}
}
/*
123
202CB962AC59075B964B07152D234B70
202CB962AC59075B964B07152D234B70
*/