?
Solved

MessageDigest with byte arrays or one byte at a time

Posted on 2014-01-31
8
Medium Priority
?
338 Views
Last Modified: 2014-02-13
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
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 39826788
Can you make sure that you have the commons-codec in your classpath?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39828750
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
 
LVL 7

Author Comment

by:gudii9
ID: 39842801
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 39842811
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
 
LVL 36

Expert Comment

by:mccarl
ID: 39843862
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
 
LVL 7

Author Comment

by:gudii9
ID: 39854747
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
 
LVL 7

Author Comment

by:gudii9
ID: 39854751
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
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39854867
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

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question