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