Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a file that I need to reload in my application everytime it changes.

I'm checking its lastModified and I'd also like to check its md5sum before I process it.

I'm using Spring framework, in case there is something useful in there.

What's the best way to check this? Any examples/libraries that I should check?

Thanks.

the explanation and code snippet in this link might help you

just for the record , there is a common issue in most of snippets that use bigInteger , the bigInteger class removes extra zeros at the start of the string so you might want to add a check like that

    String res =new BigInteger(1,m.digest()).toString(16);
    if (res.length() == 31)
          res = "0" + res;
    InputStream in = new FileInputStream(filename);
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    byte[] buffer = new byte[1024];
    while (true)
        int c = in.read(buffer);
        if (c > 0)
            md5.update(buffer, 0, c);
        else if (c < 0)
            break;
    in.close();
    byte[] result = md5.digest();

You can do it with Java builtin functions:

MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
digest.update(...your data here...);
byte[] hash = digest.digest();

or try another implementation here, certainly faster (according to it's name :)

EDIT :

they seem to provide file md5sum, exactly what you want !

You want the extra convenience methods for hashing a file...

String hash = MD5.asHex(MD5.getHash(new File(filename)));
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.