I. MD5工具类
利用JDK封装一个简易的MD5工具类,逻辑比较简单,直接贴下具体实现
public static String getMD5(String content) { String result = ""; try { MessageDigest md = MessageDigest.getInstance("md5"); md.update(content.getBytes()); byte[] bytes = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : bytes) { String str = Integer.toHexString(b & 0xFF); if (str.length() == 1) { sb.append("0"); } sb.append(str); } result = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return result; } @Test public void testMd5() { System.out.println(getMD5("hello world")); } 复制代码
测试输出为:
5eb63bbbe01eeed093cb22bb8f5acdc3 复制代码
顺手使用shell验证一下