How to get the Checksum of a Byte Array in Java?

Java 8Object Oriented ProgrammingProgramming

Create a Byte Array for which you want the Checksum −

byte[] arr = "This is it!".getBytes();

Now, create a Checksum object −

Checksum checksum = new Adler32();
checksum.update(arr, 0, arr.length);

The update() above updates the current checksum with the specified array of bytes.

Now, get the checksum with getValue() method, which gives the current checksum value.

Example

 Live Demo

import java.util.zip.Adler32;
import java.util.zip.Checksum;
public class Demo {
   public static void main(String[] argv) throws Exception {
      byte[] arr = "This is it!".getBytes();
      Checksum checksum = new Adler32();
      checksum.update(arr, 0, arr.length);
      long res = checksum.getValue();
      System.out.println("Checksum of a Byte array = "+res);
   }
}

Output

Checksum of a Byte array = 391709619
raja
Published on 24-Apr-2019 08:57:20
Advertisements