直接上代码
| |
| |
| |
| public class CrowdUtil { |
| |
| |
| |
| |
| |
| |
| |
| public static String md5(String source){ |
| |
| |
| if (source == null || source.length() == 0){ |
| |
| throw new RuntimeException(CorwdConstant.MESSAGE_STRING_INVALIDATE); |
| } |
| try { |
| |
| String algorithm = "md5"; |
| MessageDigest messageDigest = MessageDigest.getInstance(algorithm); |
| |
| |
| byte[] bytes = source.getBytes(); |
| |
| |
| |
| byte[] output = messageDigest.digest(bytes); |
| |
| 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 class StringTest { |
| @Test |
| public void testMD5(){ |
| String source = "123456789"; |
| |
| String md5 = CrowdUtil.md5(source); |
| |
| System.out.println("md5 = " + md5); |
| } |
| } |
| |