How to Calculate CRC32 Checksum in Java

Cyclic Redundancy Check (CRC) is an error detection technique commonly used to detect any changes to raw data. CRC checksum is a short fixed length data derived from a larger block of data. If there is a change in original raw data, the computed CRC checksum will differ from the CRC checksum received from the source. This technique is used to detect data errors when a file is read from a storage system. Each file stored in the file system also has the checksum stored along with the content. If the checksum is different when calculated on the file content, we know that the file on the disk is corrupted.

There are differences between CRC checksums and common hash functions such as MD5 and SHA1. CRC checksums are simpler and faster to compute. However they are not cryptographically secure. Hence CRC checksums are used in data error detection while hash functions are used in encryption algorithms.

CRC32 algorithm returns a 32-bit checksum value from the input data. It is very easy to calculate CRC32 checksum of a given string in Java. The following example program generates CRC32 checksum using the built-in class

Java Source Code for CRC32 Checksum Calculation


// Calculates CRC32 checksum for a string
public class CRC32Generator {

    public static void main(String[] args) {
        String input = "Hello World!";
        CRC32 crc = new CRC32();

Here is the output of the program,

input:Hello World!