Java MD5 String md5(final InputStream in)

Here you can find the source of md5(final InputStream in)

Description

Read all of the input stream and return it's MD5 digest.

License

Open Source License

Parameter

Parameter Description
in input stream to be read

Exception

Parameter Description
IOException when reading input stream.

Return

MD5 digest.

Declaration

public static String md5(final InputStream in) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**//from   w w w. jav  a  2s.  c  o m
     * Read all of the input stream and return it's MD5 digest.
     * @param in input stream to be read
     * @return MD5 digest.
     * @throws IOException when reading input stream.
     */
    public static String md5(final InputStream in) throws IOException {
        return md5(in, 1024);
    }

    /**
     * Read all of the input stream and return it's MD5 digest.
     * @param in input stream to be read
     * @param bufferLength length of buffer to read at a time
     * @return MD5 digest.
     * @throws IOException when reading input stream.
     */
    static String md5(final InputStream in, final int bufferLength) throws IOException {
        final byte[] buffer = new byte[bufferLength];
        try {
            final MessageDigest md = MessageDigest.getInstance("MD5");
            final DigestInputStream dis = new DigestInputStream(in, md);
            while (dis.read(buffer) != -1) {
            }
            final byte[] digest = md.digest();
            return digestToString(digest);
        } catch (final NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the MD5 for the given <code>text</code>.
     *
     * @param text a <code>String</code>
     * @return MD5
     */
    public static String md5(final String text) {
        if (text == null) {
            throw new IllegalArgumentException("String was null");
        } else if (text.length() == 0) {
            throw new IllegalArgumentException("String was 0 length");
        }
        try {
            return md5(new ByteArrayInputStream(text.getBytes()));
        } catch (IOException ioe) {
            throw new RuntimeException(ioe); //should never be able to happen, though
        }
    }

    /**
     * @param digest MD5 digest as a byte array.
     * @return MD5 digest as a string.
     */
    public static String digestToString(final byte[] digest) {
        final StringBuilder sb = new StringBuilder();
        for (byte aDigest : digest) {
            sb.append(Integer.toHexString(0x100 + (aDigest & 0xFF)).substring(1));
        }
        return sb.toString();
    }
}

Related

  1. md5(File file)
  2. md5(File file)
  3. md5(File file)
  4. md5(File gcdZipFile)
  5. md5(final File file)
  6. md5(final String data)
  7. md5(final String inData)
  8. md5(final String input)
  9. md5(final String input)