Two ways to get MD5 values using Java _java

Source: Internet
Author: User
Tags md5 stringbuffer
Message Digest algorithm MD5, a hash function widely used in the field of computer security, is a commonly used hashing algorithm, the fifth edition of the Chinese Word Digest algorithm.
Java can be implemented in two ways, we first say a little bit, code:
Copy Code code as follows:

public class Md5_test {
String Constants for MD5
Private final static string[] hexdigits = {"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F"};
public static void Main (string[] args) {
TODO auto-generated Method Stub
try {
MessageDigest messagedigest= messagedigest.getinstance ("MD5");
System.out.println (Bytearraytohexstring (Messagedigest.digest ("baidu.com". GetBytes ()));
catch (NoSuchAlgorithmException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
private static String bytearraytohexstring (byte[] b) {
StringBuffer RESULTSB = new StringBuffer ();
for (int i = 0; i < b.length; i++) {
Resultsb.append (bytetohexstring (b[i));
}
return resultsb.tostring ();
}
/** converts a byte into a 16-form string.
private static String bytetohexstring (Byte b) {
int n = b;
if (n < 0)
n = 256 + N;
int D1 = N/16;
int d2 = n% 16;
return HEXDIGITS[D1] + HEXDIGITS[D2];
}
}

The following is simple, but you need to import a jar package: Commons-codec,
Like the Commons-codec-1.4.jar code I used:

Copy Code code as follows:

Import Org.apache.commons.codec.digest.DigestUtils;
public class Tomain {
public static void Main (string[] args) {
System.out.println (Digestutils.md5hex ("baidu.com"));
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.