Java I/O How to - Calculate the Checksum of a Byte Array (Compute Adler-32 checksum)








Question

We would like to know how to calculate the Checksum of a Byte Array (Compute Adler-32 checksum).

Answer

/*from  w w  w.  ja  va 2 s  .c o m*/
import java.util.zip.Adler32;
import java.util.zip.Checksum;

public class Main {
  public static void main(String[] argv) throws Exception {
    byte[] bytes = "some data".getBytes();

    
    Checksum checksumEngine = new Adler32();
    checksumEngine.update(bytes, 0, bytes.length);
    long checksum = checksumEngine.getValue();
    System.out.println(checksum);
  }
}

The code above generates the following result.