詳解JAVA中獲取文件MD5值的四種方法
更新時(shí)間:2026-05-05 01:57:57
在??Java中,中獲D值獲取文件的取文MD5值是一種常見(jiàn)的需求,例如在文件校驗、詳解數據一致性檢查等方面,中獲D值MD5是取文一種廣泛使(shi)用的加密哈希函數,它可以生成一個(gè)128位(16字節)的詳解哈??希值,通常用32個(gè)十六進(jìn)制數字表示,中獲D值下面將介紹四種在Java中獲取文件MD5值的取文方法。
方法一:使用java.secur??ity.Message(╯‵□′)╯Digest類(lèi)
java.security.MessageDigest類(lèi)是詳解Java提供的一個(gè)用于計??算消息摘要的類(lèi),它可以實(shí)現多種摘要算法,中獲D值包括MD5,取文以下是使用MessageDigest類(lèi)計算文件MD5值的示例代碼:
import java.io.FileInputStream;import java.io.I??OException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Util { pub?lic static String getFileMD5(String filePath) throws IOException, NoSuchAlgori(′▽?zhuān)?)thmExcepti(′_ゝ`)on { MessageDigest md = MessageDigest.g(╯°□°)╯︵ ┻━┻et??Instance("MD5"(′;ω;`);); FileInputStream fis = new FileInputStream(filePath); byte[] buffer = ne(′ω`*)w byte[1024]; int len; while ((len = fis.read(buffer)) != -1) { md.update(buffer, 0, len); } fis.close(); byte[] digest = md.digest(); StringBuilder sb(??-)? = new StringBuilder(); for (byte b : digest) { sb.??appen(???)d(String.format("%02x&??quot;, b)); } return sb.t(//ω//)oString(); }}方法二:使用Apache(′?`) Commons Codec庫
Apache Com( ???)mons Codec庫提供了(′?_?`)一種(′;ω;`)簡(jiǎn)單的方式來(lái)計算文件的MD5值?,需要(yao)添加Apache Co??mmons Co??dec庫的依??賴(lài)到項??目中,可以使用DigestUtils類(lèi)的?md5Hex方法來(lái)計算文件的MD5值,以下是使用Apache Commons Codec庫計算文件MD5值的示例代碼:
imp??ort org??.apache.commons.codec.digest.Digest??Utils;import jav?????a.io.Fil???e;import java.io.IOException;import java.nio.file.Files;impo??rt java.nio.file.Path;import java.ni(′?ω?`)o.file.Pa( ?ω?)ths;public class MD5Util { public static String getFileMD5(String fileP(◎_◎;)ath) throws IOException { Path path = Paths.get(filePath); byte[] bytes = Files.readAllBytes(path); return DigestUtils.md5Hex(bytes); }}方法三:使用第三方庫Bouncy Castle
Bouncy Castle是一個(gè)開(kāi)源的J(′?`*)ava安全和密碼學(xué)庫,它提供了許多加密算法的實(shí)現,包括MD5,需要添加Boun(?????)cy Castle庫的(◎_◎;)依賴(lài)到項目中,可以使用org.bouncycastle.crypto.digests.MD5Digest類(lèi)來(lái)計??算文件的MD5值,以下是使用Bouncy Castl( ?ω?)e庫計算文件MD5值的示例代碼:
import?? org.bo??uncycastle.crypto.dige??sts.MD5Digest;imporヽ(′ー`)ノt org.bouncycas(/ω\)tle.util.encoders.Hex;import java.io??.FileInpu??tStream;impo??rt java.io.IOExc??eption;import java.security.NoSuchAlgorithmException;import java.security.Security;import java.security.spec.InvalidKeySpecException;import java.security.spec┐(′д`)┌.KeySpec;import javax.crypto.SecretKe┐(′д`)┌yFactory;import javax.crypto.spec.PBEKeySpec(?????);import javax.crypto.spec.SecretKeySpec;import java.math.BigInteger;import java.n(╯°□°)╯io.file.Files;import java.nio.fi(′?ω?`)le.Path;import java.nio.file.Paths;import java.security.*;import java.util.*;import javafx.util.*; // For Base64 encodin??g and decoding in JavaFX applications only! Use Java'(╬ ò﹏ó)s bui(?????)lt-in classes instead in non-JavaFX applications!private static final int ITERATION_COUNT = 65536; // Number of times to hash the da??ta bef(??ヮ?)?*:???ore return??ing the result (more iterations can increase security but also slow dow??n the process)priva(╬?益?)te static final int SALT_LENGTH = 8; // The length of the salt used for generating th??e key (must be at least 8 characters long)private static final int KEY_LENGTH = 128; // The length of the key generated by hashing the data with the salt (must be at least 128 bits long)private static final int PBE_ITERATION_COUNT = 65536; // Number of times to hash the data before returning the res(′▽?zhuān)?ult (more iterations can incre┐(′?`)┌ase sec(╯‵□′)╯urity but also slow down the process)private static final int PBE_SALT_LENGTH = 16; // The length of the salt used for generating the key (must be at least 16 characters long)private static final int PBE_KEY_LENGTH = 128; // The length of the key generate??d by hashing the data with the salt (must be at least 128 bits long)private static final String ALGORITHM = "PBKDF2WithHmacSHA1"; // The al??gorithm used to generate the ke(′ω`)y from the?? salt and password (must be a valid algorithm name)private static final String CIPHER_ALGORITHM = "AES/CBヽ(′▽?zhuān)?ノC/PKCS7Padding";ヽ(′ー`)ノ // The algorithm us??ed to en??crypt and decrypt the data (must be a valid al??gorithm name)private static final Str??ing PADDING_MODE = "PKCS7Padding"; // The mod??e used to pad the data before encryption or decryption (must be a valid mode name)private static final String PR┐(′д`)┌OVIDER = "BC"; // The provi(???)der used to ini??tialize the cryptographic objects (must be a valid provider name)private static final char[] PASSWORD = "password".toCharArray(); // The password used to generate the key from the salt (must be at least 8 characters long) private static final byte[] SALT = &quo??t;somesalt&quo(′Д` )t;.getBytes(); // The salt used to generate the key from the password (must be at least 8 characters long) private static final byte[] IV = new byte[16]; // The initialization vector used for encryption and decryption (must be at least 16 bytes long) private?? static final SecretKeyFactory SECRET_KEY_FACTORY = SecretKeyFactory

