import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
private static MessageDigest messageDigest = null;
public static synchronized String str2MD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
if(str == null || "".equals(str)) return "";
if(messageDigest == null) getInstance();
byte[] secretBytes = null;
secretBytes = messageDigest.digest(str.getBytes());
String md5code = new BigInteger(1, secretBytes).toString(16);
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
private static synchronized void getInstance() throws NoSuchAlgorithmException{
messageDigest = MessageDigest.getInstance("md5");
}
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
System.out.println(str2MD5("admin"));
}
}
转载于:https://my.oschina.net/Sheamus/blog/534779