public class MD5Test {
public static String md5(String source) {
if(source == null || source.length() == 0 ) {
throw new RuntimeException("不是有效字符串");
}
try {
String algorithm = "md5";
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
byte[] input = source.getBytes();
byte[] output = messageDigest.digest(input);
int signum = 1;
BigInteger bigInteger = new BigInteger(signum,output);
int radix = 16;
String encoded = bigInteger.toString(radix).toUpperCase();
return encoded;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String sourceString = "123456";
System.out.println(MD5Test.md5(sourceString));
}
}