Java MD5 Stream computeMD5(InputStream message)

Here you can find the source of computeMD5(InputStream message)

Description

Computes the MD5 message digest of an InputStream and returns it as a hexidecimal String.

License

Open Source License

Return

A String that is 32 characters long consisting of digits and the lower case alphabetic characters a-f.

Declaration

public static String computeMD5(InputStream message) throws IOException 

Method Source Code

//package com.java2s;
import java.io.*;

import java.security.*;

public class Main {
    /**/*from  w w  w. j av  a 2s.c o  m*/
     * Computes the MD5 message digest of an <code>InputStream</code>
     * and returns it as a hexidecimal String. The caller is responsible
     * for calling the <code>close()</code> method of
     * <code>message</code>. 
     * @return A <code>String</code> that is 32
     * characters long consisting of digits and the lower case alphabetic characters <code>a-f</code>.
     */
    public static String computeMD5(InputStream message) throws IOException {
        int ch;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        while ((ch = message.read()) != -1) {
            out.write(ch);
        }
        out.close();
        return computeMD5(out.toByteArray());
    }

    /**
     * Computes the MD5 message digest of a <code>String</code> and
     * returns it as a hexidecimal String. Since Java strings are
     * Unicode characters, the string is first encoded into UTF-8, which
     * defines a unique octet sequence for the string. The returned
     * <code>String</code> is 32 characters long and alphabetic
     * characters <code>a-f</code> are lower-case.
     */
    public static String computeMD5(java.lang.String message) {
        try {
            return computeMD5(message.getBytes("UTF8"));
        } catch (UnsupportedEncodingException e) {
            return null;
        } // won't happen
    }

    /**
     * Computes the MD5 message digest of a <code>Byte[]</code> and
     * returns it as a hexidecimal String. The returned
     * <code>String</code> is 32 characters long and alphabetic
     * characters <code>a-f</code> are lower-case.
     */
    public static String computeMD5(byte[] message) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.reset();
            md5.update(message);
            byte[] digest = md5.digest();

            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < digest.length; i++) {
                if ((0xff & digest[i]) < 0x10)
                    sb.append('0');
                sb.append(Integer.toHexString(0xff & digest[i]));
            }
            return (sb.toString());
        } catch (NoSuchAlgorithmException e) {
            return null;
        } // won't happen
    }
}

Related

  1. computeMD5(InputStream inputStream)
  2. computeMd5(InputStream is)
  3. computeMD5checksum(InputStream input)
  4. computeMd5Hash(InputStream is)
  5. computeMD5Hash(InputStream is)
  6. computeMD5Hash(InputStream is)