bhavs
bhavs

Reputation: 2291

clarification regarding MD5 checksum calculation in java

I have used the code in this stackoverflow discussion in order to calculate the checksum of a file in java.

I am a little confused about this working I am applying this in my problem as follows :

I have a file with some data. I have calculated the size of the text in the file using

  System.out.println(file1content.toString().getBytes().length);  the o/p is 4096 bytes

When i try to execute the checksum code I realize that the number of bytes being read is 4096+12 bytes, is this 12 bytes equal to the filename ?

I have another file2 with the same content as file1 ( i know this for sure because I extract the text to a String and compare it with String.equals) but the checksum generated is different. I am wondering why this is happening ?

Am I missing something here ?

Edit 1:

I am reading data from the file using the following loop :

 InputStream fis =  new FileInputStream(filename);
 byte[] buffer = new byte[1024];
  do {
       numRead = fis.read(buffer);
       System.out.println(" "+ numRead);
       if (numRead > 0) {
           complete.update(buffer, 0, numRead);
       }
   } while (numRead != -1);

   fis.close();

The output of numread is :

 1024
 1024
 1024
 1024
 12
 -1

Regards, Bhavya

Upvotes: 2

Views: 240

Answers (1)

bhavs
bhavs

Reputation: 2291

Well I found out what the bug was, I am not sure if this I introduced the bug or if it was already there.

I realised that the data being read from the file was not correct, some portions of the file were read multiple times, so I modified the code so that I could obtain data from the file by specifying the start and end positions.

In case anyone is facing this issue please let me know I can post the solution for this.

Regards,

Upvotes: 1

Related Questions