Here you can find the source of getCheckSum(String filePath)
Parameter | Description |
---|---|
filePath | the file path to hash |
Parameter | Description |
---|---|
NoSuchAlgorithmException | if the algorithm is not supported by the platform |
IOException | if the file to analyze does not exist |
public static String getCheckSum(String filePath) throws NoSuchAlgorithmException, IOException
//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(); } }