Java Digest digest(InputStream is, String digestAlgorithm)

Here you can find the source of digest(InputStream is, String digestAlgorithm)

Description

digest

License

LGPL

Parameter

Parameter Description
is a parameter
digestAlgorithm a parameter

Exception

Parameter Description
NoSuchAlgorithmException an exception
IOException an exception

Return

a byte array with the calculated digest.

Declaration

private static byte[] digest(InputStream is, String digestAlgorithm)
        throws NoSuchAlgorithmException, IOException 

Method Source Code

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

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

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

public class Main {
    /**//from  ww  w  .j  av a2  s .  c  o m
     * 
     * @param is
     * @param digestAlgorithm
     * @return a byte array with the calculated digest.
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    private static byte[] digest(InputStream is, String digestAlgorithm)
            throws NoSuchAlgorithmException, IOException {

        MessageDigest digestor = MessageDigest.getInstance(digestAlgorithm);

        // reads file in 1Mbyte chunks (1048576 bytes - 2^20 bytes)
        int bufSize = 1048576; // 1 MByte
        byte[] buffer = new byte[bufSize];
        int n = is.read(buffer, 0, bufSize);
        while (n != -1) {
            digestor.update(buffer, 0, n);
            n = is.read(buffer, 0, bufSize);
        }
        is.close();

        return digestor.digest();
    }
}

Related

  1. digest(final InputStream inputStream, final MessageDigest digester)
  2. digest(final java.security.MessageDigest messageDigest, final java.nio.ByteBuffer data)
  3. digest(final String algorithm, final byte[] bytes)
  4. digest(final String password)
  5. digest(InputStream input, String algorithm)
  6. digest(MessageDigest digest, InputStream data)
  7. digest(MessageDigest method, File f)
  8. digest(Serializable object)
  9. digest(String alg, byte[] plainByte)