Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generate MD5

Reply
Thread Tools

Generate MD5

 
 
Bumsys@gmail.com
Guest
Posts: n/a
 
      12-08-2008
I generate md5 for file using the following code:

private static byte[] createChecksum(final File file) throws
IOException,
NoSuchAlgorithmException {
InputStream fis = new FileInputStream(file);

byte[] buffer = new byte[1024];
MessageDigest complete = MessageDigest.getInstance("MD5");
int numRead;
do {
numRead = fis.read(buffer);
if (numRead > 0) {
complete.update(buffer, 0, numRead);
}
} while (numRead != -1);
fis.close();

return complete.digest();
}

How can I increase the getting md5 for file?
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      12-08-2008
In article
<8f5280cf-4bf0-4bc2-b974->,
wrote:

> I generate md5 for file using the following code:
>
> private static byte[] createChecksum(final File file) throws
> IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
> How can I increase the [efficiency of] getting [the] md5 for [a] file?


Consider wrapping your FileInputStream in a BufferedInputStream:

<http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-08-2008
wrote:
> I generate md5 for file using the following code:
>
> private static byte[] createChecksum(final File file) throws
> IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
> How can I increase the getting md5 for file?


Bigger buffer than 1024.

Arne
 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-08-2008
John B. Matthews wrote:
> In article
> <8f5280cf-4bf0-4bc2-b974->,
> wrote:
>> I generate md5 for file using the following code:
>>
>> private static byte[] createChecksum(final File file) throws
>> IOException,
>> NoSuchAlgorithmException {
>> InputStream fis = new FileInputStream(file);
>>
>> byte[] buffer = new byte[1024];
>> MessageDigest complete = MessageDigest.getInstance("MD5");
>> int numRead;
>> do {
>> numRead = fis.read(buffer);
>> if (numRead > 0) {
>> complete.update(buffer, 0, numRead);
>> }
>> } while (numRead != -1);
>> fis.close();
>>
>> return complete.digest();
>> }
>>
>> How can I increase the [efficiency of] getting [the] md5 for [a] file?

>
> Consider wrapping your FileInputStream in a BufferedInputStream:
>
> <http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>


If the program is reading into a sufficient large buffer, then then
there are no benefits of using BufferedInputStream.
BufferedInputStream is a huge benefit when reading just a few bytes
at a time - on some more selective data processing scenario.

Arne
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      12-09-2008
In article <493da0da$0$90268$>,
Arne Vajhøj <> wrote:

> John B. Matthews wrote:
> > In article
> > <8f5280cf-4bf0-4bc2-b974->,
> > wrote:
> >> I generate md5 for file using the following code:
> >>
> >> private static byte[] createChecksum(final File file) throws
> >> IOException,
> >> NoSuchAlgorithmException {
> >> InputStream fis = new FileInputStream(file);
> >>
> >> byte[] buffer = new byte[1024];
> >> MessageDigest complete = MessageDigest.getInstance("MD5");
> >> int numRead;
> >> do {
> >> numRead = fis.read(buffer);
> >> if (numRead > 0) {
> >> complete.update(buffer, 0, numRead);
> >> }
> >> } while (numRead != -1);
> >> fis.close();
> >>
> >> return complete.digest();
> >> }
> >>
> >> How can I increase the [efficiency of] getting [the] md5 for [a] file?

> >
> > Consider wrapping your FileInputStream in a BufferedInputStream:
> >
> > <http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html>

>
> If the program is reading into a sufficient large buffer, then then
> there are no benefits of using BufferedInputStream.
> BufferedInputStream is a huge benefit when reading just a few bytes
> at a time - on some more selective data processing scenario.


Thanks, Arne. I see your point: a MessageDigest instance can process an
entire buffer-sized chunk, so a bigger buffer is better than double
buffering.

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/
 
Reply With Quote
 
Bumsys@gmail.com
Guest
Posts: n/a
 
      12-10-2008
I increase the buffer more than 1024 but it does not help. The speed
is the same.
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      12-10-2008
In article
<21863fb5-2d62-4423-be45->,
wrote:

> I increase the buffer more than 1024 but it does not help. The speed
> is the same.


Why not prepare an sscce <http://pscode.org/sscce.html> showing the
unchanged speed? Perhaps someone will spot the problem.

--
John B. Matthews
trashgod at gmail dot com
http://home.roadrunner.com/~jbmatthews/
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-11-2008
On Mon, 8 Dec 2008 01:35:01 -0800 (PST), wrote,
quoted or indirectly quoted someone who said :

>I generate md5 for file using the following code:
>
>private static byte[] createChecksum(final File file) throws
>IOException,
> NoSuchAlgorithmException {
> InputStream fis = new FileInputStream(file);
>
> byte[] buffer = new byte[1024];
> MessageDigest complete = MessageDigest.getInstance("MD5");
> int numRead;
> do {
> numRead = fis.read(buffer);
> if (numRead > 0) {
> complete.update(buffer, 0, numRead);
> }
> } while (numRead != -1);
> fis.close();
>
> return complete.digest();
> }
>
>How can I increase the getting md5 for file?


Generate the MD5 purely in RAM with my code at
http://mindprod.com/jgloss/md5.html

Compare the speed. If they are the same, the problem is in the
complexity of computing MD5. You might try a faster hash, like Adler.

If they are different, your problem is in the i/o. You need bigger
buffers, or perhaps reading the file into RAM in a single I/O. without
buffering.
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create a md5 / md5 passwd with a salt Peter Woodsky Ruby 6 11-21-2008 09:08 AM
md5 from python different then md5 from command line ursache.marius@gmail.com Python 9 05-07-2006 11:49 PM
How to generate a md5 hash? dutche C Programming 16 07-22-2005 05:01 PM
How to generate an MD5 data from the string Peter Afonin ASP .Net 3 08-24-2004 09:09 AM
I remember someone asking about an MD5 javascript: http://pajhome.org.uk/crypt/md5/ Mozzie ³ »\( òvó \)« Javascript 0 07-12-2004 01:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57