The Message digest algorithm is also a hashing algorithm, the core of which is the unidirectional nature of the hash function. That is, the hash function is used to obtain the corresponding hash value, but it is not possible to reverse its original information by hashing the value. The Message digest algorithm is divided into three main categories: MD (Message Digest, MSG Digest), SHA (Secure Hash algorithm, secure Hash Algorithm) MAC (message authentication Code , message authentication code) This article only shows that the MD5 algorithm MD series algorithm including the MD2, MD4, MD5 three kinds of algorithmsthe MD system's Message digest algorithm generates a 128-bit message digest that is converted to 16 and is 32-bit 16 decimal. The 16-binary MD5 message digest is 32-bit.
Java comes with the MessageDigest class (if you need to process the stream, use
Digestinputstream,
Digestoutputstream
implement MD5 summary information algorithmNote: Java automatically supports only MD2 and MD5
/*** MD5 Message digest* @throws Exception */Public static void Md5hex () throws exception{ //Received MD5 message digest messagedigest MD5 = messagedigest. getinstance ("MD5");md5.update ("China". GetBytes ("Utf-8")); byte[] result = Md5.digest (); //Convert to 16 binary string, note: conversion to 16 is not a step in the MD5 message digest, so it is not required String hexstring = tohexstring(result);System. out. println (hexstring); } /*** Converts a byte array into a 16-binary string* @param bytes* @return */Public static String tohexstring (byte[] bytes) {stringbuffer sb = new stringbuffer ();For (byte b:bytes) {String hex = Integer. tohexstring (b & 0x0ff); If(Hex.length () ==1) hex = "0" + hex;sb.append (hex); } return sb.tostring (); } |
implemented using Apache's Message digest tool class org.apache.commons.codec.digest.DigestUtils The Apache Message Digest tool class is actually an encapsulation of Java's MessageDigest class use steps, which simplifies our development Digestutils.md5hex ("China". GetBytes ("UTF-8"))
Implementing a MD5 message digest calculation for a fileScenario one, as with the MD5 value of the normal string, use theMessageDigest class public static void md5file2 () throws exception{ FileInputStream fis = new fileinputstream ("F:\\t1-1_20140910_80070000.dat"); byte[]buffer = new byte[1024x768]; int count =-1;Bytearrayoutputstream bout = new bytearrayoutputstream ();While ((count=fis.read (buffer)) >-1) {bout.write (buffer, 0, count); }messagedigest MD5 = messagedigest. getinstance ("MD5"); byte[] result = Md5.digest (Bout.tobytearray ());//Print out a hexadecimal stringSystem. Out. println (tohexstring(Result)); }
Scenario two, using the Digestinputstream class public static void md5file1 () throws exception{ digestinputstream dis = new digestinputstream (new FileInputStream ("f:\\t1-1_20140910_ 80070000.dat "), MessageDigest. getinstance ("MD5")); byte[]buffer = new byte[1024x768];While (dis.read (buffer) >-1) { }messagedigest MD5 = Dis.getmessagedigest (); byte[] result = Md5.digest ();System. Out. println (tohexstring(Result)); } throughthe Read method of the Digestinputstream class can be known,actually useThe Digestinputstream class implements the same principle as scheme one. It also reads the bytes from the file stream, and then calls thethe Update method for the MessageDigest class is updated. Below isthe Read method of the Digestinputstream class public int read (byte[] b, int off, int len) throws IOException { int result = In.read (b, off, Len); if (on && result! =-1) {Digest.update (b, off, result); } return result; }
From for notes (Wiz)
MD5 Summary (Java Implementation)