Two methods for obtaining md5 values using java

Source: Internet
Author: User

Message Digest Algorithm MD5 (the fifth version of the Message Digest Algorithm in Chinese) is a hash function widely used in the computer security field. It is a common hashing Algorithm.
Java can be implemented in two ways. The Code is as follows:
Copy codeThe Code is as follows: public class md5_test {
// MD5 String constant
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 ();
}
/** Convert a byte into a hexadecimal 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,
For example, I used this commons-codec-1.4.jar code:
Copy codeThe Code is 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 "));
}
}

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.