public class Md5check {/** * default password string combination, used to convert bytes to 16 binary representation of the character, Apache verify the correctness of the downloaded file is the default combination of this */protected char hexdigits[ ] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; protected MessageDigest messagedigest = null;
{ try { messagedigest = messagedigest.getinstance ("MD5"); catch (NoSuchAlgorithmException e) { E.printstacktrace (); } } public String getfilemd5string ( File file) throws IOException { InputStream fis; & nbsp; fis = new FileInputStream (file); byte[] buffer = new byte[1024]; int numread = 0; while ((Numread = fis.read (buffer)) > 0) { messagedigest.update (buffer, 0, numread); } fis.close (); return Buffertohex (Messagedigest.digest ()); }
public String getfilemd5string (InputStream in) throws IOException { & nbsp; byte[] buffer = new byte[1024]; int numread = 0; while ((Numread = in.read (buffer)) > 0) { messagedigest.update (buffer, 0, numread); } in.close (); return Buffertohex (Messagedigest.digest ()); } Private String Buffertohex (byte bytes[]) { return Buffertohex (bytes, 0, bytes.length); } Private String Buffertohex (byte bytes[], int m, int n) { & nbsp; StringBuffer stringbuffer = new StringBuffer (2 * n); int k = m + N; for (int l = m; l < K; l++) {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB sp; Appendhexpair (Bytes[l], stringbuffer); } return Stringbuffer.tostring (); } private void Appendhexpair (Byte bt, StringBuffer StringBuffer) { & nbsp; char C0 = hexdigits[(BT & 0xf0) >> 4];//take byte high 4-bit digital conversion &NBSP;&NBSP;&NBSP;&NBSP;&N bsp; //For logical right SHIFT, move the sign bit right together, there is no difference between the two symbols found here char C1 = HEXDIGITS[BT & 0xf];//4-bit digital conversion stringbuffer.append (C0) in bytes; Stringbuffer.append (C1); }}
Java COMPUTE file MD5