Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of girionis
girionis
Flag of Greece image

Can you make sure that you have the commons-codec in your classpath?
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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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.
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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial