Generate CRC32 Checksum in Java

Published on September 26, 2015 by

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.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.