?Java???????????????????????????????????????,??????????????,????????????????????MD5?SHA-1?SHA-256??
?????????,?????Java???MD5????????????:
import java.io.File; import java.io.FileInputStream; import java.security.MessageDigest; public class FileIntegrityChecker { public static String calculateMD5(File file) { try { MessageDigest md = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { md.update(buffer, 0, bytesRead); } fis.close(); byte[] md5sum = md.digest(); StringBuilder sb = new StringBuilder(); for (byte b : md5sum) { sb.append(String.format("x", b)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } public static void main(String[] args) { File file = new File("test.txt"); String expectedMD5 = "e5398e26e6e735a6f56ecf4d9d1b3a8c"; String actualMD5 = calculateMD5(file); if (expectedMD5.equals(actualMD5)) { System.out.println("File integrity verified."); } else { System.out.println("File integrity verification failed."); } } }
???????,???????calculateMD5
??,???????File
??????,?????MD5????????16???????????main
???,??????test.txt
???????MD5???,????MD5???????,??????????
????????????????,????????????????????????????????