Java MD5 Stream computeMd5Hash(InputStream is)

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

Description

Computes the MD5 hash of the data in the given input stream and returns it as an array of bytes.

License

Open Source License

Declaration

public static byte[] computeMd5Hash(InputStream is) throws NoSuchAlgorithmException, IOException 

Method Source Code


//package com.java2s;
/*/*  w  w  w.  j av  a 2  s.c  o m*/
 * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

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

public class Main {
    /**
     * Computes the MD5 hash of the data in the given input stream and returns it as an array of bytes.
     */
    public static byte[] computeMd5Hash(InputStream is) throws NoSuchAlgorithmException, IOException {
        return computeHash(is, MessageDigest.getInstance("MD5"));
    }

    public static byte[] computeHash(InputStream is, MessageDigest messageDigest) throws IOException {
        try {
            byte[] buffer = new byte[16384];
            int bytesRead = -1;
            while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
                messageDigest.update(buffer, 0, bytesRead);
            }
            return messageDigest.digest();
        } finally {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }
}

Related

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