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;
}
digest1.reset();
byte[] digestBuf = digest1.digest(); //get hash
return new String(digestBuf);
//return finalHash;
}catch (Exception e){
return null;
}
}
}