/*  Checksum
 *
 *  Copyright (C) 2023  Rene Pawlitzek, Robert Schoech
 *  
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import org.apache.commons.codec.binary.Hex;

public class CheckSum 
{
    public static void main( String[] args ) throws Exception
    {
        checkSumOfMessage();
        
        checkSumOfCompositeMessage();
        
        checkSumOfFile();
        
    }
    
    public static void checkSumOfMessage() throws Exception
    {
        // checksum of message with SHA-256
        String message = "Meine eigene Pruefsumme";
         
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] digest = md.digest(message.getBytes());

        System.out.println("Message: "+ message);
        System.out.println("Checksum: "+ Hex.encodeHexString(digest));
        System.out.println("Length in Bytes:" + (Hex.encodeHexString(digest).length() / 2));
        System.out.println("Lenght in Bits:" + ((Hex.encodeHexString(digest).length() / 2) * 8));
    } 
    
    public static void checkSumOfCompositeMessage() throws Exception
    {
        // checksum of a composite message with SHA-256
        String m1 ="Meine ";
        String m2 ="eigene ";
        String m3 ="Pruefsumme";
         
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.reset();
        md.update(m1.getBytes());
        md.update(m2.getBytes());
        md.update(m3.getBytes());
        byte[] digest = md.digest();
            
        System.out.println("Composite message: "+ m1 + m2 + m3);
        System.out.println("Checksum: "+ Hex.encodeHexString(digest));
        System.out.println("Length in Bytes:" + (Hex.encodeHexString(digest).length() / 2));
        System.out.println("Lenght in Bits:" + ((Hex.encodeHexString(digest).length() / 2) * 8));
    }
    
    public static void checkSumOfFile() throws Exception
    {
        // checksum of a file with SHA-256
        File file = new File(".\\20230223_171546.jpg");
        
        FileInputStream fis = new FileInputStream(file);
        
        MessageDigest md = MessageDigest.getInstance("SHA-256");

        // create byte array to read data in chunks
        byte[] byteArray = new byte[1024];
        int bytesCount = 0; 
        
        // read until end of file
        while ((bytesCount = fis.read(byteArray)) != -1) 
        {
            md.update(byteArray, 0, bytesCount);
        }
        fis.close();
       
        // get the hash's bytes
        byte[] digest = md.digest();
        
        System.out.println("File name: " + file.toString());
        System.out.println("Checksum: "+ Hex.encodeHexString(digest));
        System.out.println("Length in Bytes:" + (Hex.encodeHexString(digest).length() / 2));
        System.out.println("Lenght in Bits:" + ((Hex.encodeHexString(digest).length() / 2) * 8));
    }
}


