Java Checksum Calculate generateChecksum(String path, String flavor)

Here you can find the source of generateChecksum(String path, String flavor)

Description

Creates a checksum for a file by first reading in its byte and passing those bytes through a formula that allows each chunk of bytes to be read as an integer and then made into a String

License

Open Source License

Parameter

Parameter Description
path The abstract path to the File
flavor Which flavor of checksum for this file (Ex: 'MD5', 'SHA-1')

Exception

Parameter Description
Exception an exception

Return

The string representation of the files message digest

Declaration

public static String generateChecksum(String path, String flavor) throws Exception 

Method Source Code

//package com.java2s;
/**/*from  w  w  w. j  a  v a 2 s. c om*/
 * Copyright (c) 2013, Robert Cherry * All rights reserved.
 *
 * This file is part of the Faust Engine.
 *
 * The Faust Engine 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.
 *
 * The Faust Engine 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
 * the Faust Engine. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.FileInputStream;

import java.io.InputStream;

import java.security.MessageDigest;

public class Main {
    /**
     * Creates a checksum for a file by first reading in its byte and passing
     * those bytes through a formula that allows each chunk of bytes to be read
     * as an integer and then made into a String
     *
     * @param path The abstract path to the File
     * @param flavor Which flavor of checksum for this file (Ex: 'MD5', 'SHA-1')
     * @return The string representation of the files message digest
     * @throws Exception
     */
    public static String generateChecksum(String path, String flavor) throws Exception {

        // Create byte representation of checksum
        final byte[] digest = makeChecksum(path, flavor);

        // Our result
        String output = "";

        // Iterate over the digest
        for (int i = 0; i < digest.length; i++) {

            // @ internet-source-cite: This code portion was possible from many online tutorials and forums
            int current = (digest[i] & 0xff) + 0x100;

            // Add on to the digest 
            output += Integer.toString(current, 16).substring(1);
        }

        // Return our completed checksum
        return output;
    }

    /**
     *
     * @param path The abstract path to the File
     * @param flavor Which flavor of checksum for this file (Ex: 'MD5', 'SHA-1')
     * @return The checksum in byte representation
     * @throws Exception
     */
    private static byte[] makeChecksum(String path, String flavor) throws Exception {

        // Message Digest creates the checksum
        final MessageDigest message;

        // Attempt to open channel to the File
        try (InputStream fis = new FileInputStream(path)) {

            // Buffer for the File Input Stream
            final byte[] buffer = new byte[1024];

            // Create an instance of the MessageDigest.class
            message = MessageDigest.getInstance(flavor);

            // Our tracking variable
            int read;

            // Update the digest while read is not -1
            do {

                //
                read = fis.read(buffer);

                // Do not read empty bytes
                if (read > 0) {

                    // Update the digest
                    message.update(buffer, 0, read);
                }
            } while (read != -1);
        }

        // Return the digested message in byte representation
        return message.digest();
    }
}

Related

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