How can I generate an MD5 hash?

Total Post:20

Points:140

Java 
 1348  View(s)
Ratings:
Rate this:
Is there any method to generate MD5 hash of a string in Java?
  1. Re: How can I generate an MD5 hash?

    There's an input stream decorator, java.security.DigestInputStream, so that you can compute the digest while using the input stream as you normally would, instead of having to make an extra pass over the data.

    MessageDigest md = MessageDigest.getInstance("MD5");
    try (InputStream is = Files.newInputStream(Paths.get("file.txt"))) {
      DigestInputStream dis = new DigestInputStream(is, md);
      /* Read stream to EOF as normal... */
    }
    byte[] digest = md.digest();

    Use DigestUtils from Apache Commons Codec library:
    FileInputStream fis = new FileInputStream(new File("foo"));
    String md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
    fis.close() 

      Modified On Apr-10-2018 12:59:01 AM

Answer

Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.