2

Hello so for an assignment I have to implement a simple hash for a file. The parameters are as followed:

  1. If the input length measured in the number of bytes is not a multiple of 4, add 0s to the end until it is a multiple of 4.
  2. Divide the input array into 4-byte blocks.
  3. Calculate the exclusive-or of all blocks as the hash output.

I have completed points a and b but I am stuck on c. I am unsure as where to go now. Here is my code so far:

public class HomeWork {
    public static String file = "HW2P1input";

    public static void main(String[] args) throws IOException {
        Path path = Paths.get(file);
        byte[] source = Files.readAllBytes(path);
        byte newB[][] = divideArray(source,4);
    }

    public static byte[][] divideArray(byte[] source, int chunksize) {

        byte[][] ret = new byte[(int)Math.ceil(source.length / (double)chunksize)][chunksize];

        int start = 0;

        for(int i = 0; i < ret.length; i++) {
            if(start + chunksize > source.length) {
                System.arraycopy(source, start, ret[i], 0, source.length - start);
            } else {
                System.arraycopy(source, start, ret[i], 0, chunksize);
            }
            start += chunksize ;
        }
        return ret;
    }
}

I understand that ^ is the exclusive-or operator, however I do not understand how to use it when I only have one byte array.

Deviance
  • 21
  • 4
  • Usually when it is written "divide the array into x-byte blocks", they don't mean "literally make objects representing the blocks", they mean "treat it as though it is structured that way". – harold Oct 10 '16 at 20:27
  • 2
    `^` is a XOR operator. http://stackoverflow.com/questions/726652/creating-a-logical-exclusive-or-operator-in-java/726665#726665 – OneCricketeer Oct 10 '16 at 20:27
  • Actually the other XOR, but you write it the same. – harold Oct 10 '16 at 21:03
  • I understand but I do not understand where to use the ^ operator when there is only one byte array. – Deviance Oct 10 '16 at 21:08
  • @Deviance you calculate the xor of all the 32bit ints in the array. Obviously there are 4 bytes per int, which is what the "divide into blocks of 4" is saying. You don't even have to convert to ints (xor is bitwise so it's distributive with concatenation), but you could. – harold Oct 11 '16 at 10:06

0 Answers0