0

I get the data in the form of byte buffer of 32KB, and want to calculate the checksum of the whole data. So using the MessageDigest I keep updating the bytes into it and at the end I use the digest method to calculate the bytes read and calculating the checksum out of it. Checksum calculated is wrong by the above method. Below is the code. Any idea how to get it right?

private MessageDigest messageDigest;

//Keep getting bytebuffer of 32kb till eof is read
public int write(ByteBuffer src) throws IOException {
        try {
            ByteBuffer copiedByteBUffer = src.duplicate();
            try{
                messageDigest = MessageDigest.getInstance(MD5_CHECKSUM);
                while(copiedByteBUffer.hasRemaining()){
                    messageDigest.update(copiedByteBUffer.get());
                }
            }catch(Exception e){
                throw new IOException(e);
            }
            copiedByteBUffer = null;
        }catch(Exception e){
    }
}

//called after whole file is read in write function
public void calculateDigest(){
    if(messageDigest != null){
        byte[] digest = messageDigest.digest();
        checkSumMultiPartFile = toHex(digest);  // converting bytes into hexadecimal
    }
}

Updated try #2

//Will Keep getting bytebuffer of 32kb till eof is read
    public int write(ByteBuffer original) throws IOException {
            try {
                ByteBuffer copiedByteBuffer = cloneByteBuffer(original);
                messageDigest = MessageDigest.getInstance(MD5_CHECKSUM);
                messageDigest.update(copiedByteBuffer);
                copiedByteBUffer = null;
            }catch(Exception e){
        }
    }
    
    public static ByteBuffer cloneByteBuffer(ByteBuffer original) {
        final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity()):ByteBuffer.allocate(original.capacity());
        final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();
        readOnlyCopy.flip();
        clone.put(readOnlyCopy);
        clone.position(original.position());
        clone.limit(original.limit());
        clone.order(original.order());
        return clone;
    }

After trying the above code i was able to see that the message digest was getting updated with all the bytes read for example: if the file size is 52,42,892 bytes then it was updated with 52,42,892 bytes. But when the checksum of file calculated using certutil -hashfile MD5 using CMD and the one calculated using the above method does not match.

SSharma2203
  • 217
  • 4
  • 18
  • Have you tried calling `copiedByteBUffer.rewind()` after `duplicate()`? – Jeroen Steenbeeke May 21 '21 at 07:04
  • 1
    Too many unknowns here. What checksum are you getting, and what are you expecting, and why are you expecting that? Has the `src` buffer been flipped? And why aren't you using `MessageDigest.update(ByteBuffer buffer)`? – user207421 May 21 '21 at 07:30
  • I have updated the way of getting the byte buffer. Please check the updated question. – SSharma2203 May 24 '21 at 09:57
  • @JeroenSteenbeeke - I do not see the use of `copiedByteBUffer.rewind()` . i have used `flip()` in order to read the whole original bytebuffer. However, i did try using `rewind()` and it did not work. – SSharma2203 May 24 '21 at 10:08
  • @SSharma2203 my theory was that the original ByteBuffer already had a mark beyond the point you wanted to start reading from. In other words: you were only calculating a checksum for a part of the bytes you wanted, not the whole of it. – Jeroen Steenbeeke May 25 '21 at 06:43
  • Actually I figured out the error was with the message digest instance was getting created every time when it was filled with bytes and that was the error. So when instance was created at class level, I received the correct checksum. – SSharma2203 Jun 01 '21 at 05:38

0 Answers0