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