Search code examples
javanetwork-programmingchecksum

How many bytes of checksum for byte array length 1000


I am implementing a reliable data transfer protocol. The maximum packet length is 1000 bytes. How many bytes do I need to store the checksum for the maximum packet size? I tried using one byte to store the checksum, but but the receiver is unable to obtain the same checksum as the stored one.

Checksum checksum = new CRC32();
checksum.update(out_data, 0, out_data.length-1);

Long checksumValue = checksum.getValue();
out_data[out_data.length-1] =  checksumValue.byteValue();

Here's my code for checksum implementation. I used the last slot of the byte array for the checksum. Note that this is not a case of corrupted packet as the simulation of the unreliable network that I'm running can be made to be reliable.


Solution

  • You can checksum things of any size. Ethernet frames are typically 1500+ bytes and only use 4 bytes for checksums. If you want to be on the safe side, use sha1 (but it takes up 20 bytes and can be slow to calculate).

    To put the result of CRC32 into the array, you need 4 bytes (32 bits / 8 bits per byte).