Solved

MessageDigest with byte arrays or one byte at a time

Posted on 2014-01-31
8
309 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
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:girionis
Comment Utility
Can you make sure that you have the commons-codec in your classpath?
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
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
Comment Utility
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 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
topping1 challenge 7 48
firstChar challenge 13 83
create a gui in perl 3 42
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now