Java MD5 Stream computeMd5(InputStream is)

Here you can find the source of computeMd5(InputStream is)

Description

compute a hexadecimal, 32-char md5 string for content from a stream.

License

Apache License

Parameter

Parameter Description
is - the stream to read content data from

Exception

Parameter Description
IllegalArgumentException - internal error computing the Md5 Hex String

Return

the hexadecimal md5 string

Declaration

public static String computeMd5(InputStream is) throws NoSuchAlgorithmException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    private static final char[] HEXES = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**//from  w  w  w  . ja v  a  2  s. c  om
     * compute a hexadecimal, 32-char md5 string for content from a stream.
     *
     * @param is - the stream to read content data from
     * @return the hexadecimal md5 string
     * @throws java.security.NoSuchAlgorithmException
     *                                  - no MD5 Digester in JDK
     * @throws java.io.IOException      - cannot read stream
     * @throws IllegalArgumentException - internal error computing the Md5 Hex String
     */
    public static String computeMd5(InputStream is) throws NoSuchAlgorithmException, IOException {
        if (is == null)
            return null;
        MessageDigest digest = MessageDigest.getInstance("MD5");
        try {
            byte[] buffer = new byte[8192];
            int read;
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            return toHex(digest.digest());
        } finally {
            is.close();
        }
    }

    private static String toHex(byte[] raw) {
        if (raw.length != 16)
            throw new IllegalArgumentException("length must be 16, not " + raw.length);
        final char[] hex = new char[32];
        short i = 0;
        for (final byte b : raw) {
            hex[i] = HEXES[(b & 0xF0) >> 4];
            hex[i + 1] = HEXES[(b & 0x0F)];
            i += 2;
        }
        return new String(hex);
    }
}

Related

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