Generate MD5

B

Bumsys

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?
 
J

John B. Matthews

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>
 
A

Arne Vajhøj

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
 
A

Arne Vajhøj

John 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 [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
 
J

John B. Matthews

Arne Vajhøj said:
John 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 [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.
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top