MessageDigest with byte arrays or one byte at a time

Hi,

I am trying below examples.

http://www.avajava.com/tutorials/lessons/should-i-update-my-messagedigest-with-byte-arrays-or-one-byte-at-a-time.html
http://www.avajava.com/tutorials/lessons/how-do-i-generate-an-md5-digest-for-a-web-page.html

what is messagedigest. why and where we use it.

When i try to run this example i am getting compilation error at below import saying
import import org.apache.commons.codec.binary.Hex;
 cannot be resolved.


import org.apache.commons.codec.binary.Hex;

how is md5 digest is different from regular digest.

I am getting compilation error saying below import cannot be resolved
import org.apache.commons.codec.binary.Hex;

please advise on how to fix it.
Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Ok, I didn't really study the tutorial example to see what they were trying to demonstrate, but now that I've looked, the aim is to compare the performance of the MD5 digest calculation with different sized buffers.

For you to do this though, you will need a much larger file. Rather than creating a text file and zipping it, just browse around in you file system for a file of decent size. The example file that the tutorial refers to is about 9MB in size, so I would suggest something around that size (or a bit bigger, but the exact size doesn't matter too much). You just need something big enough that it takes a bit of time to do the MD5 calculations on your computer. Your file above is so small that it doesn't matter what the size of the buffer is, it is finishing very quickly.

Once you do that, you should get output that shows the time taken to do the MD5 calc for the different buffer sizes, and those timings should point you towards the most efficient buffer size to use (in general).
0
 
girionisCommented:
Can you make sure that you have the commons-codec in your classpath?
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Firstly, as an alternative to using an additional library to convert the byte array to a hex string, the below uses functions that are already provided with Java 6 and above. Try changing the following lines...
import org.apache.commons.codec.binary.Hex;

...

String result = new String(Hex.encodeHex(digest));

Open in new window

... to this ...

import javax.xml.bind.DatatypeConverter;

...

String result = DatatypeConverter.printHexBinary(digest);

Open in new window

what is messagedigest. why and where we use it.
A message digest is a value that represents the message but is generally smaller and therefore easier to manage. As an example, say you have a large file for download on a website, you might calculate the digest (using say MD5) of the file to give you a much smaller string of characters that represents that file, now by itself this digest is not of any use, but if someone downloads that file they can also calculate the digest of the content of the file they download and then compare this with what is printed on the website. If they match, then it is extremely unlikely that the file has been corrupted while downloading. So it is sort of like a checksum.

how is md5 digest is different from regular digest.
Well there isn't anything really that is a "regular" digest. There are different types of digest, MD5 being one and others such as SHA1, etc but there's no real "regular" digest.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
gudii9Author Commented:
I changed above two lines and when i try to run i see below error

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
      at java.net.PlainSocketImpl.socketConnect(Native Method)
      at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
      at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)

please advise
0
 
gudii9Author Commented:
when i run second example in other link
http://www.avajava.com/tutorials/lessons/should-i-update-my-messagedigest-with-byte-arrays-or-one-byte-at-a-time.html


I see below error

java.io.FileNotFoundException: httpd-2.2.6-win32-src-r2.zip (The system cannot find the file specified)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.<init>(FileInputStream.java:120)
      at java.io.FileInputStream.<init>(FileInputStream.java:79)
      at MessageDigestTest.main(MessageDigestTest.java:19)

Please advise on how to fix it.
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
That file is just an example that the author of that tutorial downloaded to test calculating the digest on. You would either need to download that very same file or probably easier to just point the code to a file that is already on your computer.
0
 
gudii9Author Commented:
I created test folder under

C:\gptest

i put one text file named aaa.txt under C:\gptest\test

Now I zipped the test folder and gave same zip file path in the program as below


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

//import org.apache.commons.codec.binary.Hex;
import javax.xml.bind.DatatypeConverter;

public class MessageDigestTest {

	public static void main(String[] args) {

		try {
			String file = "C:\gptest\test.zip";
			MessageDigest md = MessageDigest.getInstance("MD5");

			getDigestViaByteArray(new FileInputStream(file), md, 128);
			getDigestViaByteArray(new FileInputStream(file), md, 256);
			getDigestViaByteArray(new FileInputStream(file), md, 512);
			getDigestViaByteArray(new FileInputStream(file), md, 1024);
			getDigestViaByteArray(new FileInputStream(file), md, 2048);
			getDigestViaByteArray(new FileInputStream(file), md, 4096);
			getDigestViaByteArray(new FileInputStream(file), md, 8192);

			getDigestViaOneByteAtATime(new FileInputStream(file), md);

		} catch (Throwable e) {
			e.printStackTrace();
		}

	}

	public static String getDigestViaByteArray(InputStream is, MessageDigest md, int arraySize)
			throws NoSuchAlgorithmException, IOException {
		Date t1 = new Date();

		md.reset();
		byte[] bytes = new byte[arraySize];
		int numBytes;
		while ((numBytes = is.read(bytes)) != -1) {
			md.update(bytes, 0, numBytes);
		}
		byte[] digest = md.digest();
		String result = DatatypeConverter.printHexBinary(digest);
	//	String result = new String(Hex.encodeHex(digest));

		Date t2 = new Date();

		System.out.println("MD5 Digest:" + result);
		System.out.print("Using byte array (size " + arraySize + "): ");
		System.out.println((t2.getTime() - t1.getTime()) + " milliseconds\n");

		return result;
	}

	public static String getDigestViaOneByteAtATime(InputStream is, MessageDigest md) throws NoSuchAlgorithmException,
			IOException {
		Date t1 = new Date();

		md.reset();
		int oneByte;
		while ((oneByte = is.read()) != -1) {
			md.update((byte) oneByte);
		}

		byte[] digest = md.digest();
		String result = DatatypeConverter.printHexBinary(digest);
		//String result = new String(Hex.encodeHex(digest));

		Date t2 = new Date();

		System.out.println("MD5 Digest:" + result);
		System.out.print("One byte at a time: ");
		System.out.println((t2.getTime() - t1.getTime()) + " milliseconds\n");

		return result;
	}

}

Open in new window


Eclipse is giving compilation error for the zip folder path. How to fix it. Please advise
0
 
gudii9Author Commented:
when i gave back slash it worked.(            String file = "C:\\gptest\\test.zip";)


I got output as below

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 128): 16 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 256): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 512): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 1024): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 2048): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 4096): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
Using byte array (size 8192): 0 milliseconds

MD5 Digest:ABE634F7701DCA6FA8210715719E1AE7
One byte at a time: 0 milliseconds



What is the meaning of the output and how to interpret. please advise
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.