Java Checksum Calculate getCheckSum(String filePath)

Here you can find the source of getCheckSum(String filePath)

Description

Calculates the sha-256 hash of a file and returns the string value

License

Open Source License

Parameter

Parameter Description
filePath the file path to hash

Exception

Parameter Description
NoSuchAlgorithmException if the algorithm is not supported by the platform
IOException if the file to analyze does not exist

Return

string representing sha-256 hash of the file

Declaration

public static String getCheckSum(String filePath) throws NoSuchAlgorithmException, IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.xml.bind.DatatypeConverter;

public class Main {
    /**/*from   w w w .  j a v a  2s .c om*/
     * Calculates the sha-256 hash of a file and returns the string value
     * 
     * @param filePath
     *            the file path to hash
     * @return string representing sha-256 hash of the file
     * @throws NoSuchAlgorithmException
     *             if the algorithm is not supported by the platform
     * @throws IOException
     *             if the file to analyze does not exist
     */
    public static String getCheckSum(String filePath) throws NoSuchAlgorithmException, IOException {
        MessageDigest dig = MessageDigest.getInstance("sha-256");
        byte[] bytes = Files.readAllBytes(Paths.get(filePath));
        byte[] resDig = dig.digest(bytes);
        String b = DatatypeConverter.printHexBinary(resDig);
        return b.toLowerCase();
    }
}

Related

  1. generateChecksum(String path, String flavor)
  2. generateChecksum(String pType, String pInFile)
  3. generateCheckSum(String string)
  4. getChecksum(byte[] buffer, int offset, int length)
  5. getChecksum(final File file)
  6. getChecksumAlder32(File file)
  7. getCheckSums(String archiveFile)