Home > Java > javaTutorial > body text

Using JDK to implement a simple MD5 tool class in Java

php是最好的语言
Release: 2018-08-09 17:43:24
Original
2267 people have browsed it

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"));
}
Copy after login

The test output is:

5eb63bbbe01eeed093cb22bb8f5acdc3
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template