Baran Topal

Baran Topal


April 2024
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
2930  

Categories


CryptoServices in Java: CRCPro

baranbaran

This is the Java Realm implementation. Note there are 4 other parts to fulfill this cryptoservices completely.  Check the other posts to have a complete picture, yet, this is also a standalone module and a java with main is at the end of the page to test the functionality.

This is the implementation of Cyclic Redundancy. This module also works standalone.

CRCPro Project is pretty straight forward:

It has 2 classes CheckSum.java and CRCEx.java. Note that, there is no bouncycastle utilization or so but mere Java packages .

Careful about this line, this is new Java-7 feature. We should be grateful to Oracle for this:

try(FileInputStream fis = new FileInputStream(file); CheckedInputStream cis = new CheckedInputStream(fis, crc);){

I won’t be explaining any further, read the comments if needed. I also attached the input file:

loremipsum_inp


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 * 
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: CRCPro           			 		 *
 * Package name: com.baran.crc				 		 *
 * File name: CheckSum.java                  				 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.baran.crc;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.CheckedInputStream;
import java.util.zip.CRC32;

// class for computing checksum
public class CheckSum {

	public long checkSum(File file){
		long checksum = 0L;
		try{
			CRC32 crc = new CRC32();
			long sizeOfFile = 0;
			// java-7 style
			try(FileInputStream fis = new FileInputStream(file); CheckedInputStream cis = new CheckedInputStream(fis, crc);){								
				sizeOfFile = file.length();

				byte[] buffer = new byte[4096];
				while(cis.read(buffer)>=0)
				{
					checksum = cis.getChecksum().getValue();				
				}
				System.out.println("The checksum of the file" + file + " is: " + checksum); 
				System.out.println("The file size is: " + sizeOfFile + " bytes");
			}
		}
		catch(IOException e){
			System.out.println("IO Exception thrown");
			e.printStackTrace();
			System.exit(1);
		}
		return checksum;
	}		 
}


/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Company: -								 *            
 * Assignment: Cyclic Redundancy Check + DES + Hibernate + JAX-WSRPC	 *
 * Programmer: Baran Topal                   				 *
 * WorkspaceName: Crypto					 	 *
 * Project Name: CRCPro           			 		 *
 * Package name: com.baran.crc				 		 *
 * File name: CRCEx.java                  	 			 *
 *                                           				 *      
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *	                                                                                         *
 *  LICENSE: This source file is subject to have the protection of GNU General                   *
 *	Public License. You can distribute the code freely but storing this license information. *
 *	Contact Baran Topal if you have any questions. barantopal@barantopal.com                 *
 *	                                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

package com.baran.crc;

import java.io.File;

// checksum test
public class CRCEx {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CheckSum cs = new CheckSum();
		cs.checkSum(new File("loremipsum_inp.txt"));
	}
}

Comments 0
There are currently no comments.