?Java?,?????MessageDigest
??????????????????,???????SHA-256??????????:
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class FileHash { public static void main(String[] args) { String filePath = "path/to/your/file"; String hash = getFileHash(filePath, "SHA-256"); System.out.println("?????: " + hash); } public static String getFileHash(String filePath, String algorithm) { try { MessageDigest md = MessageDigest.getInstance(algorithm); File file = new File(filePath); FileInputStream fis = new FileInputStream(file); byte[] dataBytes = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, bytesRead); } byte[] digestBytes = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digestBytes) { sb.append(String.format("x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException | IOException e) { e.printStackTrace(); return null; } } }
?path/to/your/file
????????????????????????algorithm
???????????,??MD5
?SHA-1
?