[FSF Associate Member] View Annang Sowah's profile on LinkedIn

Wednesday, 17 July 2013

Performing a Cyclic Redundancy Check Checksum on a file using Java

I want to delve on how to generate a CRC checksum (acronym for SUMmation CHECK) for  textual/binary data using the Java programming language.

This is achieved using the Checksum class and its derivative/child class CRC32. As a programming best practice, a subroutine/function (called method in Java) is created and invoked at a point real data is passed unto it as a parameter. 

Purpose: This enables one to use a string token to verify if a file has been compromised or not. This is done by a sending point generating a token (programmatically or other) on the file in transit and the receiving point regenerating a token for comparison to ascertain the authenticity of a file.

a. Generating CRC checksum programmatically 
snippet 1.0: Java code snippet to generate checksum on a file labelled data.avi.


public class TestChecksum {
                
                 //catch exceptions if neccessary
public static long performChecksum(Object data) {
Checksum checksum = new CRC32();
//build checksum
checksum.update(data.getBytes(), 0,
data.getBytes().length);
//produce and return checksum token.
return checksum.getValue();
}

             //catch exceptions if neccessary
public static void main(String[] args){
//input data here: txt, avi, doc etc
Object data = new File (‘data.avi);
//Invoke instance method performChecksum here
new TestChecksum().performChecksum(data);
}

}


b. Generating CRC checksum from Linux to ascertain the authenticity of a file 
This is achieved by using the cksum command on Linux to generate a checksum to compare to a previous token.

fig. 1.0: command line code to generate the checksum and data byte size.