Java Checksum Calculate generateChecksum(InputStream is)

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

Description

generate Checksum

License

Open Source License

Declaration

public static String generateChecksum(InputStream is) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w ww .j a va2s.c  om
 * Project Wonderland
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * Sun designates this particular file as subject to the "Classpath" 
 * exception as provided by Sun in the License file that accompanied 
 * this code.
 */

import java.io.BufferedInputStream;

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

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

public class Main {
    private static final String DEFAULT_ALGORITHM = "SHA-1";

    public static String generateChecksum(InputStream is) throws IOException {
        return generateChecksum(is, DEFAULT_ALGORITHM);
    }

    public static String generateChecksum(InputStream is, String checksumAlgorithm) throws IOException {
        MessageDigest digest;

        try {
            digest = MessageDigest.getInstance(checksumAlgorithm);
        } catch (NoSuchAlgorithmException nsae) {
            IOException ioe = new IOException("No such algorithm " + checksumAlgorithm);
            ioe.initCause(nsae);
            throw ioe;
        }

        byte[] buf = new byte[1024 * 1024];
        int bytesRead = 0;
        BufferedInputStream bis = new BufferedInputStream(is);
        InputStream in = new DigestInputStream(bis, digest);

        /* Read in the entire file */
        do {
            bytesRead = in.read(buf);
        } while (bytesRead != -1);
        in.close();

        /* Compute the checksum with the digest */
        byte[] byteChecksum = digest.digest();
        digest.reset();

        return toHexString(byteChecksum);
    }

    /**
     * Converts the checksum given as an array of bytes into a hex-encoded
     * string.
     *
     * @param bytes The checksum as an array of bytes
     * @return The checksum as a hex-encoded string
     */
    private static String toHexString(byte bytes[]) {
        StringBuffer ret = new StringBuffer();
        for (int i = 0; i < bytes.length; ++i) {
            ret.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
        }
        return ret.toString();
    }
}

Related

  1. checksum_icmpv6(byte pkt[])
  2. checksumBytesToString(byte[] digestBytes)
  3. checksumFile(File file)
  4. checksumLength(int algorithmCode, int dataLength)
  5. createChecksum(File file, String shaAlgo)
  6. generateChecksum(String path, String flavor)
  7. generateChecksum(String pType, String pInFile)
  8. generateCheckSum(String string)
  9. getChecksum(byte[] buffer, int offset, int length)