5

I'm a bit of a newbie in Java and I trying to perform a MAC calculation on a file. Now since the size of the file is not known at runtime, I can't just load all of the file in to memory. So I wrote the code so it would read in bits (4k in this case). The issue I'm having is I tried loading the entire file into memory to see if both methods produce the same hash. However they seem to be producing different hashes

Here's the bit by bit code:

FileInputStream fis = new FileInputStream("sbs.dat");
byte[] file = new byte[4096];
m = Mac.getInstance("HmacSHA1");
int i=fis.read(file);
m.init(key);
while (i != -1)
{
    m.update(file);
    i=fis.read(file);
}
mac = m.doFinal();

And here's the all at once approach:

File f = new File("sbs.dat");
long size = f.length();
byte[] file = new byte[(int) size];
fis.read(file);
m = Mac.getInstance("HmacSHA1");
m.init(key);
m.update(file);
mac = m.doFinal();

Shouldn't they both produce the same hash?

The question however is more generic. Is the 1st code the correct way of loading a file into memory into pieces and perform whatever we want to do inside the while cycle? (socket send, cipher a file, etc...). This question is useful because every tutorial I've seen just loads everything at once...

Update: Working :-D. Will this approach work properly sending a file in pieces through a socket?

3 Answers 3

5

No. You have no guarantee that in fis.read(file) will read file.length bytes. This is why read() is returning an int to tell you how many bytes it has actually read.

You should instead do this:

m.init(key);
int i=fis.read(file);

while (i != -1)
{
    m.update(file, 0, i);
    i=fis.read(file);
}

taking advantage of Mac.update(byte[] data, int offset, int len) method that allows you to specify length of actual data in in byte[] array.

1
  • Doh. I feel like an idiot, I completely forgot that there's 3 update methods in the Mac classes .Nevertheless, I still would have lost hours around something simple. Gonna try it out, but it sounds right.
    – ptguy
    Apr 15, 2011 at 20:07
4

The read function will not necessarily fill up your entire array. So, you need to check how many bytes were returning from the read function, and only use that many bytes of your buffer.

1

Just like Jason LeBrun says - The read method will not always read the specified amount of bytes. For example: What do you think will happen if the file does not contain a multiple of 4096 bytes?

I would go for something like this:

        FileInputStream fis = new FileInputStream(filename);
        byte[] buffer = new byte[buffersize];
        Mac m = Mac.getInstance("HmacSHA1");
        m.init(key);

        int n;
        while ((n = fis.read(buffer)) != -1)
        {
            m.update(buffer, 0, n);
        }
        byte[] mac = m.doFinal();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.