Hmac MD5 of a large file

641 Views Asked by At

I'm using FileInputStream wrapped with BufferedInputStream for reading in a large file in byte chunks.

public void MD5(BufferedInputStream in, Key pubKey) throws Exception{

        Mac md = Mac.getInstance("HmacMD5");
            md.init(pubKey);
            byte[] contents = new byte[1024];
            int readSize;
            while ((readSize = in.read(contents)) != -1) {
                {
                    md.update(contents,0,readSize);
                }
                byte[] hashValue = md.doFinal();

            }
}

It works perfectly fine for a small file, but takes insane amounts of time for a file 200MB file.

The same approach works perfectly fine while I try to sign the 200MB file using SHA256withRSA.

Is there any specific reason for this?? I have a feeling it is something to do with md.update().

But I've used the same function while using 'Signature' as well.

Any help would be appreciated.

1

There are 1 best solutions below

1
On BEST ANSWER

You're calling doFinal within the while-loop. That doesn't look right. Try the following:

public void MD5(BufferedInputStream in, Key pubKey) throws Exception{
    Mac md = Mac.getInstance("HmacMD5");
    md.init(pubKey);
    byte[] contents = new byte[1024];
    int readSize;
    while ((readSize = in.read(contents)) != -1) {
        md.update(contents, 0, readSize);
    }
    byte[] hashValue = md.doFinal();
}