Checksum Program in Java

This is a java program to implement checksum method and verify message. A checksum or hash sum is a small-size datum from an arbitrary block of digital data for the purpose of detecting errors which may have been introduced during its transmission or storage. It is usually applied to an installation file after it is received from the download server. By themselves checksums are often used to verify data integrity, but should not be relied upon to also verify data authenticity.

The actual procedure which yields the checksum, given a data input is called a checksum function or checksum algorithm. Depending on its design goals, a good checksum algorithm will usually output a significantly different value, even for small changes made to the input. This is especially true of cryptographic hash functions, which may be used to detect many data corruption errors and verify overall data integrity; if the computed checksum for the current data input matches the stored value of a previously computed checksum, there is a very high probability the data has not been accidentally altered or corrupted.

Here is the source code of the Java Program to Implement the Checksum Method for Small String Messages and Detect If the Received message is same as the Transmitted. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.*;
  5.  
  6. public class ChecksumMethod
  7. {
  8.     public static void main(String args[])
  9.     {
  10.         Scanner scan = new Scanner(System.in);
  11.         System.out.println("Enter the string input:");
  12.         String input = scan.next();
  13.         int checksum = generateChecksum(input);
  14.         // Call the method to create the checksum
  15.         System.out.println("The checksum generated is = "
  16.                 + Integer.toHexString(checksum));
  17.         System.out.println("Enter the data to be sent:");
  18.         input = scan.next();
  19.         System.out.println("Enter the checksum to be sent:");
  20.         checksum = Integer.parseInt((scan.next()), 16);
  21.         // User inputs data as hexadecimal value but it will be stored as a
  22.         // decimal value unless it is converted into hexadecimal first.
  23.         receive(input, checksum);
  24.         scan.close();
  25.     }
  26.  
  27.     static int generateChecksum(String s)
  28.     {
  29.         String hex_value = new String();
  30.         // 'hex_value' will be used to store various hex values as a string
  31.         int x, i, checksum = 0;
  32.         // 'x' will be used for general purpose storage of integer values
  33.         // 'i' is used for loops
  34.         // 'checksum' will store the final checksum
  35.         for (i = 0; i < s.length() - 2; i = i + 2)
  36.         {
  37.             x = (int) (s.charAt(i));
  38.             hex_value = Integer.toHexString(x);
  39.             x = (int) (s.charAt(i + 1));
  40.             hex_value = hex_value + Integer.toHexString(x);
  41.             // Extract two characters and get their hexadecimal ASCII values
  42.             System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
  43.                     + hex_value);
  44.             x = Integer.parseInt(hex_value, 16);
  45.             // Convert the hex_value into int and store it
  46.             checksum += x;
  47.             // Add 'x' into 'checksum'
  48.         }
  49.         if (s.length() % 2 == 0)
  50.         {
  51.             // If number of characters is even, then repeat above loop's steps
  52.             // one more time.
  53.             x = (int) (s.charAt(i));
  54.             hex_value = Integer.toHexString(x);
  55.             x = (int) (s.charAt(i + 1));
  56.             hex_value = hex_value + Integer.toHexString(x);
  57.             System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
  58.                     + hex_value);
  59.             x = Integer.parseInt(hex_value, 16);
  60.         }
  61.         else
  62.         {
  63.             // If number of characters is odd, last 2 digits will be 00.
  64.             x = (int) (s.charAt(i));
  65.             hex_value = "00" + Integer.toHexString(x);
  66.             x = Integer.parseInt(hex_value, 16);
  67.             System.out.println(s.charAt(i) + " : " + hex_value);
  68.         }
  69.         checksum += x;
  70.         // Add the generated value of 'x' from the if-else case into 'checksum'
  71.         hex_value = Integer.toHexString(checksum);
  72.         // Convert into hexadecimal string
  73.         if (hex_value.length() > 4)
  74.         {
  75.             // If a carry is generated, then we wrap the carry
  76.             int carry = Integer.parseInt(("" + hex_value.charAt(0)), 16);
  77.             // Get the value of the carry bit
  78.             hex_value = hex_value.substring(1, 5);
  79.             // Remove it from the string
  80.             checksum = Integer.parseInt(hex_value, 16);
  81.             // Convert it into an int
  82.             checksum += carry;
  83.             // Add it to the checksum
  84.         }
  85.         checksum = generateComplement(checksum);
  86.         // Get the complement
  87.         return checksum;
  88.     }
  89.  
  90.     static void receive(String s, int checksum)
  91.     {
  92.         int generated_checksum = generateChecksum(s);
  93.         // Calculate checksum of received data
  94.         generated_checksum = generateComplement(generated_checksum);
  95.         // Then get its complement, since generated checksum is complemented
  96.         int syndrome = generated_checksum + checksum;
  97.         // Syndrome is addition of the 2 checksums
  98.         syndrome = generateComplement(syndrome);
  99.         // It is complemented
  100.         System.out.println("Syndrome = " + Integer.toHexString(syndrome));
  101.         if (syndrome == 0)
  102.         {
  103.             System.out.println("Data is received without error.");
  104.         }
  105.         else
  106.         {
  107.             System.out.println("There is an error in the received data.");
  108.         }
  109.     }
  110.  
  111.     static int generateComplement(int checksum)
  112.     {
  113.         // Generates 15's complement of a hexadecimal value
  114.         checksum = Integer.parseInt("FFFF", 16) - checksum;
  115.         return checksum;
  116.     }
  117. }

Output:

$ javac ChecksumMethod.java
$ java ChecksumMethod
 
Enter the string input:
Sanfoundry
Sa : 5361
nf : 6e66
ou : 6f75
nd : 6e64
ry : 7279
The checksum generated is = ede4
Enter the data to be sent:
Sanfoundry
Enter the checksum to be sent:
ede4
Sa : 5361
nf : 6e66
ou : 6f75
nd : 6e64
ry : 7279
Syndrome = 0
Data is received without error.
 
 
Enter the string input:
Sanfoundry
Sa : 5361
nf : 6e66
ou : 6f75
nd : 6e64
ry : 7279
The checksum generated is = ede4
Enter the data to be sent:
sanfoundry
Enter the checksum to be sent:
ede4
sa : 7361
nf : 6e66
ou : 6f75
nd : 6e64
ry : 7279
Syndrome = ffffe000
There is an error in the received data.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.