Java - Adler32 and CRC32 Checksums


Java Adler32 class in the package computes the Adler-32 checksum for bytes of data.

getValue() method returns the checksum.

CRC32 in the same package computes a checksum using the CRC32 algorithm.

Adler32 is faster than CRC32. CRC32 gives a more robust checksum.

Checksum is an interface, and the Adler32 and CRC32 classes implement the interface.

CheckedInputStream and CheckedOutputStream are two concrete decorator classes in the InputStream/OutputStream class family.

The following code illustrates how to use the Adler32 and CRC32 classes to compute checksums.



public class Main {
  public static void main(String[] args) throws Exception {
    String str = "HELLO";
    byte[] data = str.getBytes("UTF-8");
    System.out.println("Adler32 and CRC32 checksums for " + str);

    // Compute Adler32 checksum
    Adler32 ad = new Adler32();
    ad.update(data);//from w w  w  .  j  a va 2 s . c o m
    long adler32Checksum = ad.getValue();
    System.out.println("Adler32: " + adler32Checksum);

    // Compute CRC32 checksum
    CRC32 crc = new CRC32();
    long crc32Checksum = crc.getValue();
    System.out.println("CRC32: " + crc32Checksum);