dcsimg
Calculate MD5 Checksum hash of a file?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Cad_Developer
Posted On:   Thursday, February 28, 2002 08:55 AM

What is the most efficient way of calculating MD5 checksum hash without using JNI? This is the only pure java code I found so far... import java.security.*; import java.io.*; public class jmd5sum { public static void main(String []args) throws Exception { if (args.length != 1) { System.err.println("Usage: java jmd5sum "); System.exit(1); } BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0])); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int c; while ((c = bis.read()) > -1) { baos.write(c); } bis.close(); byte[] buf = baos.toByteArray();    More>>
			
What is the most efficient way of calculating MD5 checksum hash without using JNI?

This is the only pure java code I found so far...

import java.security.*;
import java.io.*;

public class jmd5sum
{
public static void main(String []args) throws Exception {
if (args.length != 1) {
System.err.println("Usage: java jmd5sum ");
System.exit(1);
}

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0]));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = bis.read()) > -1) {
baos.write(c);
}
bis.close();
byte[] buf = baos.toByteArray();

MessageDigest md = MessageDigest.getInstance("MD5");
md.update(buf);
byte[] digest = md.digest();

int decValue;
for (int i=0; i
if (digest[i] >= 0)
decValue = digest[i];
else
decValue = 256 + digest[i];

String hexVal = Integer.toHexString(decValue);
if (hexVal.length() == 1) hexVal = "0" + hexVal; //prefix a zero to look uniformed;
System.out.print(hexVal + " ");
}
System.out.print(" "+ args[0]);
}
}
   <<Less

Re: Calculate MD5 Checksum hash of a file?

Posted By:   Stephen_Ostermiller  
Posted On:   Friday, March 1, 2002 06:36 AM

I have an MD5 utility that can do this.

Re: Calculate MD5 Checksum hash of a file?

Posted By:   Jay_Meyer  
Posted On:   Thursday, February 28, 2002 08:15 PM

You used the same method I used for solving this problem: read in the whole file and use the MessageDigest("MD5"). I believe that I could assume that my files were pretty small, so I just read the whole file into a single String instead of the way you did it. But I don't think that will help your performance.

Re: Calculate MD5 Checksum hash of a file?

Posted By:   Shashi_Sharma  
Posted On:   Thursday, February 28, 2002 09:15 AM

Use JSSE. Its pure Java and in 1.4 its included with the JRE.
About | Sitemap | Contact