Home  >  Article  >  Java  >  Using JDK to implement a simple MD5 tool class in Java

Using JDK to implement a simple MD5 tool class in Java

php是最好的语言
php是最好的语言Original
2018-08-09 17:43:242197browse

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

Using JDK to implement a simple MD5 tool class in Java

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn