Library providing various hash algorithms (MD5, SHA1, SHA256, etc) in Java?

18,624

Solution 1

The DigestUtils class of Apache Commons Codec is suitable for the purpose of simplifying the use of the MessageDigest class, which appears to be what you are attempting to use in the first place.

Solution 2

It's built-in: java.security.MessageDigest

Share:
18,624
Naftuli Kay
Author by

Naftuli Kay

Updated on July 16, 2022

Comments

  • Naftuli Kay
    Naftuli Kay almost 2 years

    Is there a simple library out there for Java in a Maven repository that will give me simple, one-liner hash methods for the popular hash functions such as MD5, SHA1, SHA256, and SHA512? I'd really hate to have to reinvent the wheel to do this.

  • Jcs
    Jcs about 13 years
    javax.crypto.Mac provides message authentication which is something like a "keyed" hash. The question was about digest algorithms. In this case the java.crypto.MessageDigest class should be used.
  • Maarten Bodewes
    Maarten Bodewes about 13 years
    For other users of this class, don't instantiate it even if it has a public constructor, use DigestUtils.function() directly. The constructor should of course have been made private.
  • Admin
    Admin over 11 years
    Bouncy Castle also has good Java Cryptography APIs. bouncycastle.org/documentation.html
  • user207421
    user207421 almost 11 years
    @MartinAndersson What are you talking about? The question is about a 'single library' for all those algorithms, and the answer provides one. He didn't ask for a list. Why should anyone re-'populate' a correct answer? Your comment is inane. You could always provide your own answer, if you think the ones already here aren't adequate.
  • Martin Andersson
    Martin Andersson almost 11 years
    hmm, figured I would get a response like yours. Thing is one asks when one doesn't know. The built in MessageDigest is more than enough for someone who wants a "simple" library, so that he doesn't have to "reinvent the wheel". If a user asked for a String library, would you do the same; find him a String library and recommend that? Or would you provide a link to the JavaDoc of java.lang.String? May I also ask what is the harm in providing a list of suggested libraries? Do you yourself favor answers that has a limited amount of alternatives? Thank you for caring and providing feedback!
  • user207421
    user207421 almost 11 years
    @MartinAndersson If MessageDigest is 'more than enough', that's the end of the discussion. If the user asked for a String library I would indeed point him to the Javadoc for java.lang.String, and that's exactly what I have done here for MessageDigest. I favor answers that answer the question myself. I've said nothing about 'harm in providing a list of suggested libraries', but it wouldn't answer the question being asked here. You seem to be commenting on a different question, and you haven't exactly taken up the suggestion to provide your own answer. Feel free.