A Checksum is used for error checking while transferring data from server to server or client to server. Data is flown across networks in the form of packets. So, checksum is a computed value that is dependent on the contents transferred over the network. For each packet the computed checksum will be different. This computed value is transmitted with the packet when it is transmitted. The receiving system checks the checksum and on the basis of checksum it receives and rejects the packet. It is mainly used where it becomes necessary to check the packets before accepting it.We used the same in our project when the payment is accepted by third party and sends the response string back with checksum appnded,we decode to compute the check sum again.In this example we are calculating a value of a Byte Array using CRC32 it can also be done using Adler32, both the classes are the implementation of Checksum and are available in java.util package.
//Output
Actual String : ETRADE|0|MCITVR0000000088|NA|00000001.00|CIT|22275790|NA|INR|NA|NA|NA|NA|25-06-2008 13:24:50|0300|NA|MCITVR0000000088|1|NA|NA|NA|NA|NA|NA|NA|383801148421
Check Sum String : 383801148421
String to be tested : ETRADE|0|MCITVR0000000088|NA|00000001.00|CIT|22275790|NA|INR|NA|NA|NA|NA|25-06-2008 13:24:50|0300|NA|MCITVR0000000088|1|NA|NA|NA|NA|NA|NA|NA
Value of Check Sum after Computing : 383801148421
Check Sum Validated : true
import java.io.ByteArrayInputStream; import java.util.zip.Adler32; import java.util.zip.CRC32; import java.util.zip.CheckedInputStream; public class CheckSum { public static void main(String args[]) { //The last string appended to the last is the checksum that is sent to client from server, need to check again try{ String string = "ETRADE|0|MCITVR0000000088|NA|00000001.00|CIT|22275790|NA|INR|NA|NA|NA|NA|25-06-2008 13:24:50|0300|NA|MCITVR0000000088|1|NA|NA|NA|NA|NA|NA|NA|383801148421"; System.out.println("Actual String : "+ string); String input = string.substring(string.lastIndexOf("|")+1); System.out.println("Check Sum String : "+ input); String test = string.substring(0,string.lastIndexOf("|")); System.out.println("String to be tested : "+test); String[] chec = test.split("|"); long value=-1; for(int i = 0; i<chec.length ; i++ ) { // Main login start byte buffer[] = chec[i].getBytes(); ByteArrayInputStream bais = new ByteArrayInputStream(buffer); CheckedInputStream cis = new CheckedInputStream(bais, new CRC32()); //CheckedInputStream cis = new CheckedInputStream(bais, new Adler32()); byte readBuffer[] = new byte[chec[i].length()]; while (cis.read(readBuffer) >= 0){ value += cis.getChecksum().getValue(); } // Main login end } System.out.println("Value of Check Sum after Computing : "+value); System.out.println("Check Sum Validated : " + input.equalsIgnoreCase(new Long(value).toString())); } catch(Exception e){ System.out.println("Exception has been caught" + e); } } }
//Output
Actual String : ETRADE|0|MCITVR0000000088|NA|00000001.00|CIT|22275790|NA|INR|NA|NA|NA|NA|25-06-2008 13:24:50|0300|NA|MCITVR0000000088|1|NA|NA|NA|NA|NA|NA|NA|383801148421
Check Sum String : 383801148421
String to be tested : ETRADE|0|MCITVR0000000088|NA|00000001.00|CIT|22275790|NA|INR|NA|NA|NA|NA|25-06-2008 13:24:50|0300|NA|MCITVR0000000088|1|NA|NA|NA|NA|NA|NA|NA
Value of Check Sum after Computing : 383801148421
Check Sum Validated : true