import java.security.MessageDigest;
public class MD5Util {
private static char[] hexDigit = {'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
public static String encode(String str) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5"); // 换成SHA
byte[] digest = md.digest(str.getBytes());
return byteToHexString(digest);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String byteToHexString(byte[] digest) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < digest.length; ++i) {
sb.append(hexDigit[(digest[i] >> 4) & 0xF]);
sb.append(hexDigit[digest[i] & 0xF]);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(MD5Util.encode("Biao"));
}
}
在PHP中就一句代码: $str = md5("Biao")