Home arrow static arrow Java Programming [Archive] - MD5 hash of file ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - MD5 hash of file ?
This topic has 7 replies on 1 page.

Posts:41
Registered: 6/13/04
MD5 hash of file ?  
Jun 26, 2004 5:55 AM



 
How to do an MD5 hash of file.. i have prepared SHA1 and MD5 hash using MessageDigest, but they accept only string inputs. Is there a built-in class where i can pass a filename, and get an MD5 hash for the same. Any ideas / pointers?

Also, what is actually an MD5 hash of a file. does the process take into consideration every bit of the file content.

Thanks in advance..
 

Posts:41
Registered: 6/13/04
Re: MD5 hash of file ?  
Jun 26, 2004 6:32 AM (reply 1 of 7)



 

I think i got an answer here.

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=34&t=004836
 

Posts:41
Registered: 6/13/04
Re: MD5 hash of file ?  
Jun 29, 2004 7:37 AM (reply 2 of 7)



 
Hi,

I have been looking for some help with generating an MD5 hash of a file.
I got it wrong, when i tried the below code ..i am getting wrong hash values. I think its with the way i update chunks.

any help would be useful.
Thanks

public String getMD5HashofFile(String fileFullname){try{MessageDigest digest1=MessageDigest.getInstance("MD5"); File oFile;FileInputStream oFS;BufferedInputStream inputBuff; oFile = new File(fileFullname);long iLengthofData = oFile.length();oFile = null; oFS = new FileInputStream(fileFullname);inputBuff = new BufferedInputStream(oFS); int iChunkSize = (iBuffLenForHashingFile * 1024);long iBytesRead = 0; //this will actually be bytesRead or Hashed, of file. byte[] buff1 = new byte[iChunkSize]; digest1.reset();String finalHash="";while (iBytesRead < iLengthofData){int iActualBytesRead;iActualBytesRead=inputBuff.read(buff1,0,iChunkSize); //bytes read. //digest1.reset();digest1.update(buff1,0, iActualBytesRead); //update the MessageDigest.//byte[] digestBuf = digest1.digest(); //get hash//finalHash += new String(digestBuf); iBytesRead += iChunkSize;}byte[] digestBuf = digest1.digest(); //get hashreturn new String(digestBuf);//return finalHash; }catch (Exception e){return null;}}} 
 

Posts:41
Registered: 6/13/04
Re: MD5 hash of file ?  
Jun 29, 2004 7:40 AM (reply 3 of 7)



 

Also.. is MD5 a standard.. becos.. my application is Java .. and a server application which
will verify the file hash after i send, is implementing microsoft cryptoAPI on windows.
Both the hashes are different.

As of now, i assume that MD5 is a standard. and only something is wrong with my code... and its not becos of the difference in provider of the cryptographic API... is this assumption right?
 

Posts:112
Registered: 4/13/98
Re: MD5 hash of file ?  
Jun 29, 2004 12:11 PM (reply 4 of 7)



 
One problem with your code is that it doesn't check for read() returning -1, meaning end of file. When the end of the file is reached, your code will pass -1 as the length parameter to update() which may cause a problem. Also, on final the read() before the end of the file is reached, your buffer will most likely be only partially filled with data.
 

Posts:112
Registered: 4/13/98
Re: MD5 hash of file ?  
Jun 29, 2004 2:16 PM (reply 5 of 7)



 
On second thought, the partially-filled buffer won't cause a problem, since the update() method takes the actual bytes read. The other place that you may be getting messed up is in making the return value from your method a String. String(byte[]) converts each byte in the array into its displayable character on your system, which is probably not what you want. Here is what I came up with. I hope it may be closer to perhaps what you were originally trying for.

import java.io.*;import java.security.*; public class CalcMD5 {   public static void main(String[] args)  {    CalcMD5 cmd5 = new CalcMD5();    byte[] ba = cmd5.getSum(args[0]);    String hexChar = null;    if(ba != null) {      for(int i=0; i<ba.length; i++) {	if(ba[i] > 0)                           // Convert hex string to two-character string for output	  hexChar = Integer.toHexString(ba[i]);	else if(ba[i] < 0)	  hexChar = Integer.toHexString(ba[i]).substring(6);	else	  hexChar = "00"; 	System.out.print(hexChar);      }    }    System.out.println();  }   public byte[] getSum(String fileName) {    byte[] digestBuf = null;    try {      MessageDigest digest1=MessageDigest.getInstance("MD5");       BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));       byte[] buff = new byte[1024];      int chunk = 1024;      int bytesRead = 0;      while((bytesRead=bis.read(buff, 0, chunk)) != -1)   // <-- Notice we don't go into loop at end-of-file	digest1.update(buff, 0, bytesRead);      digestBuf = digest1.digest(); //get hash    }    catch( Exception e ) {      e.printStackTrace();    }    return digestBuf;  }}
 

Posts:41
Registered: 6/13/04
Re: MD5 hash of file ?  
Jun 29, 2004 9:53 PM (reply 6 of 7)



 
i want a displayed string / ascii string only.. becos what i am looking for it to get a hash string for a file.
 

Posts:41
Registered: 6/13/04
Re: MD5 hash of file ?  
Jul 1, 2004 2:59 AM (reply 7 of 7)



 

Thanks schildre.. your code helped me analyze that the result of MD5 in hex form was ok.
And the problem was with the string conversion of the MD5 digest buffer..

the pb was with this line of the code

byte[] digestBuf = digest1.digest(); //get hash[b]return new String(digestBuf);[/b]
 
This topic has 7 replies on 1 page.