AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

MessageDigest different

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java
View previous topic :: View next topic  
Author Message
lydie
Guest





PostPosted: Fri Nov 19, 2004 3:58 pm    Post subject: MessageDigest different Reply with quote



Hi,

I have an application that get PDF documents from the Web and store
them in a database. I don't want to add the same document many times
so I decided to calculate the digest of these documents.
Here is want I do:

I am getting a document from the WEB as an inputStream using the
document's URL
then I calculate the digest
URL url = new URL("http://www.aoac.org/testkits/030406certrpt.pdf");
DataInputStream bis = new DataInputStream(url.openStream());
bis.mark(Integer.MAX_VALUE);

MessageDigest msg = MessageDigest.getInstance("SHA-1");
byte[] buf = new byte[4096];
long len;
while ( (len = bis.read(buf)) != -1){
msg.update(buf);
}
res1 = msg.digest();

bis.reset();

for(int i=0; i {
int x = res1[i] &0xFF;
if(x < 0x10) System.out.print("0");
System.out.print(Integer.toHexString(x));
}

If I run this code many times I don't get the same digest.

If I try the same code with a file saved on the computer: it works
perfectly fine.
Here is also the code in case you need it...
File pdf = new File("C:/temp2.pdf");
FileInputStream str = new FileInputStream(pdf);
DataInputStream bis = new DataInputStream(str);

MessageDigest msg = MessageDigest.getInstance("SHA-1");
byte[] buf = new byte[4096];
long len;
while ( (len = bis.read(buf)) != -1){
msg.update(buf);
}
res1 = msg.digest();

for(int i=0; i {
int x = res1[i] &0xFF;
if(x < 0x10) System.out.print("0");
System.out.print(Integer.toHexString(x));
}
System.out.println();


THanks a lot for your help

Lydie
Back to top
Michael Amling
Guest





PostPosted: Sat Nov 20, 2004 2:26 am    Post subject: Re: MessageDigest different Reply with quote



lydie wrote:
Quote:
Hi,

I have an application that get PDF documents from the Web and store
them in a database. I don't want to add the same document many times
so I decided to calculate the digest of these documents.
Here is want I do:

I am getting a document from the WEB as an inputStream using the
document's URL
then I calculate the digest
URL url = new URL("http://www.aoac.org/testkits/030406certrpt.pdf");
DataInputStream bis = new DataInputStream(url.openStream());
bis.mark(Integer.MAX_VALUE);

MessageDigest msg = MessageDigest.getInstance("SHA-1");
byte[] buf = new byte[4096];
long len;
while ( (len = bis.read(buf)) != -1){
msg.update(buf);

This is only correct if buf is always filled to capacity. What you
want is msg.update(buf, 0, len). When fetching data from the web, you're
apparently getting some partial buffers as your implementation waits for
packets from www.aoac.org.

Quote:
}
res1 = msg.digest();

bis.reset();

for(int i=0; i {
int x = res1[i] &0xFF;
if(x < 0x10) System.out.print("0");
System.out.print(Integer.toHexString(x));
}

If I run this code many times I don't get the same digest.

If I try the same code with a file saved on the computer: it works
perfectly fine.
Here is also the code in case you need it...
File pdf = new File("C:/temp2.pdf");
FileInputStream str = new FileInputStream(pdf);
DataInputStream bis = new DataInputStream(str);

I recommend getting rid of the DataInputStream, too. The
documentation says "An application uses a data output stream to write
data that can later be read by a data input stream." You're reading data
that was not written by a DataOutputStream.

Quote:

MessageDigest msg = MessageDigest.getInstance("SHA-1");
byte[] buf = new byte[4096];
long len;
while ( (len = bis.read(buf)) != -1){
msg.update(buf);

This is still wrong (unless you can guarantee the file's length is a
multiple of 4096 bytes), but consistent because there's no delay in
getting data from your local hard drive, so only the last bufferful is
partially filled from the file, and the extra data you're hashing is
leftover from the immediately previous read.

Quote:
}
res1 = msg.digest();

for(int i=0; i {
int x = res1[i] &0xFF;
if(x < 0x10) System.out.print("0");
System.out.print(Integer.toHexString(x));
}
System.out.println();

--Mike Amling

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Security and Java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.