MD5 tool class
Use JDK to encapsulate a simple MD5 tool class. The logic is relatively simple. I will directly post the specific implementation
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")); }
The test output is:
5eb63bbbe01eeed093cb22bb8f5acdc3
Use the shell to verify it
Related recommendations:
JAVA development tools JDK (Java Development Kit)
5 JDK Tools Every Java Developer Should Know
The above is the detailed content of Using JDK to implement a simple MD5 tool class in Java. For more information, please follow other related articles on the PHP Chinese website!