Generate CRC32 Checksum in Java
Published on September 26, 2015 by Bo Andersen
Generating a CRC32 checksum in Java is – as you would expect – extremely simple. All it takes is a few lines of code. Below is an example on how to generate a CRC32 checksum from a string.
public final class AppUtils {
private AppUtils() { }
public static long crc32(String input) {
byte[] bytes = input.getBytes();
Checksum checksum = new CRC32(); // java.util.zip.CRC32
checksum.update(bytes, 0, bytes.length);
return checksum.getValue();
}
}
In the example above, the code required to generate the CRC32 checksum has been wrapped in a convenient helper class, such that you can use the static method as follows.
long checksum = AppUtils.crc32("Turn this into a CRC32 checksum!");
You can of course just take the code within the crc32 method and use it in whichever context you need.
As you can see, generating a CRC32 checksum in Java is very, very easy.