Java Hash Calculate getHash(InputStream is, String algorithm)

Here you can find the source of getHash(InputStream is, String algorithm)

Description

Reads the input stream and returns an hash for it, based on the algorithm passed as parameter.

License

Creative Commons License

Declaration

public static String getHash(InputStream is, String algorithm) throws IOException, NoSuchAlgorithmException 

Method Source Code

//package com.java2s;
/*/*from  ww  w.  j a v a2 s.  c o  m*/
 * __________
 * CREDITS
 * __________
 *
 * Team page: http://isatab.sf.net/
 * - Marco Brandizi (software engineer: ISAvalidator, ISAconverter, BII data management utility, BII model)
 * - Eamonn Maguire (software engineer: ISAcreator, ISAcreator configurator, ISAvalidator, ISAconverter,  BII data management utility, BII web)
 * - Nataliya Sklyar (software engineer: BII web application, BII model,  BII data management utility)
 * - Philippe Rocca-Serra (technical coordinator: user requirements and standards compliance for ISA software, ISA-tab format specification, BII model, ISAcreator wizard, ontology)
 * - Susanna-Assunta Sansone (coordinator: ISA infrastructure design, standards compliance, ISA-tab format specification, BII model, funds raising)
 *
 * Contributors:
 * - Manon Delahaye (ISA team trainee:  BII web services)
 * - Richard Evans (ISA team trainee: rISAtab)
 *
 *
 * ______________________
 * Contacts and Feedback:
 * ______________________
 *
 * Project overview: http://isatab.sourceforge.net/
 *
 * To follow general discussion: isatab-devel@list.sourceforge.net
 * To contact the developers: isatools@googlegroups.com
 *
 * To report bugs: http://sourceforge.net/tracker/?group_id=215183&atid=1032649
 * To request enhancements:  http://sourceforge.net/tracker/?group_id=215183&atid=1032652
 *
 *
 * __________
 * License:
 * __________
 *
 * This work is licenced under the Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License. To view a copy of this licence, visit http://creativecommons.org/licenses/by-sa/2.0/uk/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
 *
 * __________
 * Sponsors
 * __________
 * This work has been funded mainly by the EU Carcinogenomics (http://www.carcinogenomics.eu) [PL 037712] and in part by the
 * EU NuGO [NoE 503630](http://www.nugo.org/everyone) projects and in part by EMBL-EBI.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class Main {
    /**
     * Reads the input stream and returns an hash for it, based on the algorithm passed as parameter. algorithm 
     * is passed to {@link MessageDigest} 
     */
    public static String getHash(InputStream is, String algorithm) throws IOException, NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance(algorithm);
        byte buffer[] = new byte[1024];

        try {
            for (int read = is.read(buffer); read != -1; read = is.read(buffer))
                if (read > 0)
                    md.update(buffer, 0, read);
        } finally {
            is.close();
        }
        byte[] digest = md.digest();
        if (digest == null)
            return null;
        StringBuilder strDigest = new StringBuilder();
        for (int i = 0; i < digest.length; i++)
            strDigest.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1));
        return strDigest.toString();
    }

    /**
     * A wrapper to {@link #getHash(InputStream, String)} that opens a file.
     */
    public static String getHash(File f, String algorithm) throws IOException, NoSuchAlgorithmException {
        return getHash(new FileInputStream(f), algorithm);
    }
}

Related

  1. getHash(File file)
  2. getHash(File file, String hashType)
  3. getHash(final byte[] data, final String hashAlgorithm)
  4. getHash(final String text, final Charset charset, final MessageDigest algorithm)
  5. getHash(InputStream in, String algorithm)
  6. getHash(int iterationNb, String password, byte[] salt)
  7. getHash(String algorithm, int i)
  8. getHash(String credentials)
  9. getHash(String Data)