Compute the md5 hash of a InputStream. In case of errors, returns an empty string. - Java Security

Java examples for Security:MD5

Description

Compute the md5 hash of a InputStream. In case of errors, returns an empty string.

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    /**/* w  w w.j  a  v a 2  s.  co  m*/
     * Byte mask.
     */
    private static final int BYTE_MSK = 0xFF;

    /**
     * Compute the md5 hash of a InputStream. In case of errors, returns an
     * empty string. It takes care of closing the InputStream
     *
     * @param inputStream
     * the InputStream to be hashed
     * @return a 32-character hexadecimal number
     * @throws IOException
     * when there are problem reading the stream
     */
    public static String computeMd5OfInputStream(
            final InputStream inputStream) throws IOException {
        final int base = 16;
        byte[] b = md5AsBytes(inputStream);
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            result.append(Integer.toString(
                    (b[i] & BYTE_MSK) + BYTE_MSK + 1, base).substring(1));
        }
        return result.toString();
    }

    /**
     * Compute the md5 hash of a File. In case of errors, returns a zero
     * length array (file not existent, not readable, ...), also takes care
     * of closing the InputStream
     *
     * @param fis
     * the input stream to be hashed
     * @return a 32-character hexadecimal number
     * @throws IOException
     * when InputStream can't be read properly
     */
    private static byte[] md5AsBytes(final InputStream fis)
            throws IOException {
        MessageDigest complete;
        try {
            complete = computeMd5MessageDigest(fis);
        } catch (NoSuchAlgorithmException ex) {
            throw new UnsupportedOperationException(ex);
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ex) {
                return new byte[0];
            }
        }
        return complete.digest();
    }

    /**
     * Compute the md5 hash of a InputStream.
     *
     * @param fis
     * the InputStream
     * @throws IOException
     * when there are problems reading the stream
     * @throws NoSuchAlgorithmException
     * should not bother to catch this exception
     * @return a MessageDigest
     */
    private static MessageDigest computeMd5MessageDigest(
            final InputStream fis) throws IOException,
            NoSuchAlgorithmException {
        int numRead;
        final int chunk = 1024;
        MessageDigest complete = MessageDigest.getInstance("MD5");
        byte[] buffer = new byte[chunk];
        do {
            numRead = fis.read(buffer, 0, chunk);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
        return complete;
    }
}

Related Tutorials