Java MD5 Stream computeMD5checksum(InputStream input)

Here you can find the source of computeMD5checksum(InputStream input)

Description

Computes the MD5 checksum of the bytes in the InputStream.

License

Open Source License

Parameter

Parameter Description
input is the InputStream for which to compute the checksum

Return

the MD5 checksum as a String of hexadecimal characters

Declaration

static public String computeMD5checksum(InputStream input) 

Method Source Code

//package com.java2s;
/*/*from   ww w .ja v a  2  s.co  m*/
 * dalclient library - provides utilities to assist in using KDDart-DAL servers
 * Copyright (C) 2015,2016,2017 Diversity Arrays Technology
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.io.InputStream;

import java.security.DigestInputStream;

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

import java.util.Formatter;

public class Main {
    private static final String DIGEST_MD5 = "MD5";

    /**
     * Computes the MD5 checksum of the bytes in the InputStream.
     * The input is close()d on exit.
     * @param input is the InputStream for which to compute the checksum
     * @return the MD5 checksum as a String of hexadecimal characters
     */
    static public String computeMD5checksum(InputStream input) {
        DigestInputStream dis = null;
        Formatter formatter = null;
        try {
            MessageDigest md = MessageDigest.getInstance(DIGEST_MD5);
            dis = new DigestInputStream(input, md);
            while (-1 != dis.read())
                ;

            byte[] digest = md.digest();
            formatter = new Formatter();
            for (byte b : digest) {
                formatter.format("%02x", b); //$NON-NLS-1$
            }
            return formatter.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException ignore) {
                }
            }
            if (formatter != null) {
                formatter.close();
            }
        }
    }
}

Related

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