Generate MD5

Discussion in 'Java' started by Bumsys@gmail.com, Dec 8, 2008.

  1. Guest

    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?
     
    , Dec 8, 2008
    #1
    1. Advertising

  2. In article
    <>,
    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/
     
    John B. Matthews, Dec 8, 2008
    #2
    1. Advertising

  3. Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Dec 8, 2008
    #3
  4. Arne Vajhøj Guest

    John B. Matthews wrote:
    > In article
    > <>,
    > 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
     
    Arne Vajhøj, Dec 8, 2008
    #4
  5. In article <493da0da$0$90268$>,
    Arne Vajhøj <> wrote:

    > John B. Matthews wrote:
    > > In article
    > > <>,
    > > 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/
     
    John B. Matthews, Dec 9, 2008
    #5
  6. Guest

    I increase the buffer more than 1024 but it does not help. The speed
    is the same.
     
    , Dec 10, 2008
    #6
  7. In article
    <>,
    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/
     
    John B. Matthews, Dec 10, 2008
    #7
  8. Roedy Green Guest

    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
     
    Roedy Green, Dec 11, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Afonin

    How to generate an MD5 data from the string

    Peter Afonin, Aug 20, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    500
  2. Replies:
    9
    Views:
    17,211
    John Salerno
    May 8, 2006
  3. dutche

    How to generate a md5 hash?

    dutche, Jul 20, 2005, in forum: C Programming
    Replies:
    16
    Views:
    1,935
    CBFalconer
    Jul 22, 2005
  4. Peter Afonin

    How to generate an MD5 data from the string

    Peter Afonin, Aug 20, 2004, in forum: ASP .Net Security
    Replies:
    3
    Views:
    187
  5. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    246
    Brian Candler
    Nov 21, 2008
Loading...

Share This Page