在Java开发中,计算MD5或SHA-256摘要是常见的需求,比如校验文件完整性、存储用户密码的摘要值等,Java内置的MessageDigest类已经提供了完整的摘要计算能力,不需要引入第三方依赖就可以实现相关功能。

MessageDigest类基础说明
MessageDigest是Java安全包下的一个类,用于为应用程序提供信息摘要算法的功能,比如MD5、SHA-1、SHA-256等。它支持对字节数组、输入流等内容进行摘要计算,最终返回固定长度的字节数组结果。
使用MessageDigest的核心步骤分为三步:
- 获取指定算法的
MessageDigest实例 - 向实例中传入需要计算摘要的内容
- 调用
digest方法获取最终的摘要字节数组
字符串摘要计算实现
计算字符串的MD5或SHA-256摘要时,需要先将字符串转换为字节数组,注意要指定统一的字符编码,避免出现乱码导致摘要结果不一致。
工具类完整代码
下面是一个封装好的工具类,支持计算字符串的MD5和SHA-256摘要,并且将结果转换为十六进制字符串返回:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;
public class DigestUtil {
/**
* 计算字符串的MD5摘要
* @param content 待计算的内容
* @return 十六进制格式的MD5摘要,出错返回null
*/
public static String md5(String content) {
return digest(content, "MD5");
}
/**
* 计算字符串的SHA-256摘要
* @param content 待计算的内容
* @return 十六进制格式的SHA-256摘要,出错返回null
*/
public static String sha256(String content) {
return digest(content, "SHA-256");
}
/**
* 通用摘要计算方法
* @param content 待计算的内容
* @param algorithm 算法名称,如MD5、SHA-256
* @return 十六进制格式的摘要结果
*/
private static String digest(String content, String algorithm) {
if (content == null || content.isEmpty()) {
return null;
}
try {
// 获取指定算法的MessageDigest实例
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
// 将字符串转换为字节数组,使用UTF-8编码避免乱码
byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);
// 计算摘要
byte[] digestBytes = messageDigest.digest(contentBytes);
// 将字节数组转换为十六进制字符串
return bytesToHex(digestBytes);
} catch (NoSuchAlgorithmException e) {
// 算法不存在时抛出异常,实际开发中MD5和SHA-256是Java内置支持的,不会走到这里
e.printStackTrace();
return null;
}
}
/**
* 字节数组转十六进制字符串
* @param bytes 字节数组
* @return 十六进制字符串
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder hexBuilder = new StringBuilder();
for (byte b : bytes) {
// 将字节转换为无符号整数,再格式化为两位十六进制
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexBuilder.append('0');
}
hexBuilder.append(hex);
}
return hexBuilder.toString();
}
// 测试示例
public static void main(String[] args) {
String testContent = "hello world";
String md5Result = md5(testContent);
String sha256Result = sha256(testContent);
System.out.println("原始内容:" + testContent);
System.out.println("MD5摘要:" + md5Result);
System.out.println("SHA-256摘要:" + sha256Result);
}
}
代码说明
上述代码中,digest方法是核心逻辑,首先通过MessageDigest.getInstance获取对应算法的实例,这里传入的算法名称需要和Java支持的算法名称一致,MD5对应MD5,SHA-256对应SHA-256。
字符串转字节数组时使用了StandardCharsets.UTF_8,保证不同环境下编码一致,避免因为系统默认编码不同导致摘要结果不同。最后通过bytesToHex方法将摘要得到的字节数组转换为十六进制字符串,这是摘要结果最常用的展示形式。
文件摘要计算实现
除了字符串,很多时候需要计算文件的MD5或SHA-256摘要,用来校验文件是否被篡改。计算文件摘要时需要使用输入流逐步读取文件内容,避免大文件一次性加载到内存导致内存溢出。
文件摘要计算代码
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileDigestUtil {
private static final int BUFFER_SIZE = 8192;
/**
* 计算文件的MD5摘要
* @param filePath 文件路径
* @return 十六进制格式的MD5摘要,出错返回null
*/
public static String fileMd5(String filePath) {
return fileDigest(filePath, "MD5");
}
/**
* 计算文件的SHA-256摘要
* @param filePath 文件路径
* @return 十六进制格式的SHA-256摘要,出错返回null
*/
public static String fileSha256(String filePath) {
return fileDigest(filePath, "SHA-256");
}
/**
* 通用文件摘要计算方法
* @param filePath 文件路径
* @param algorithm 算法名称
* @return 十六进制格式的摘要结果
*/
private static String fileDigest(String filePath, String algorithm) {
if (filePath == null || filePath.isEmpty()) {
return null;
}
try (InputStream inputStream = new FileInputStream(filePath)) {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
byte[] buffer = new byte[BUFFER_SIZE];
int len;
// 循环读取文件内容,更新摘要
while ((len = inputStream.read(buffer)) != -1) {
messageDigest.update(buffer, 0, len);
}
// 获取最终摘要
byte[] digestBytes = messageDigest.digest();
return bytesToHex(digestBytes);
} catch (NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 字节数组转十六进制字符串,和之前的逻辑一致
*/
private static String bytesToHex(byte[] bytes) {
StringBuilder hexBuilder = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexBuilder.append('0');
}
hexBuilder.append(hex);
}
return hexBuilder.toString();
}
// 测试示例
public static void main(String[] args) {
String testFilePath = "test.txt";
String fileMd5 = fileMd5(testFilePath);
String fileSha256 = fileSha256(testFilePath);
System.out.println("文件路径:" + testFilePath);
System.out.println("文件MD5摘要:" + fileMd5);
System.out.println("文件SHA-256摘要:" + fileSha256);
}
}
代码说明
文件摘要计算使用了update方法逐步更新摘要内容,每次读取8KB的内容传入update方法,直到文件读取完成,最后调用digest方法获取最终结果。这里使用了try-with-resources语法自动关闭输入流,避免资源泄漏。
注意事项
- MD5算法已经被证明存在碰撞漏洞,不适合用于安全敏感的场景,比如密码存储,这类场景建议使用SHA-256或者更安全的算法。
- 计算摘要时,输入内容的编码必须统一,尤其是字符串场景,否则不同环境下会得到不同的摘要结果。
- 摘要算法是单向的,无法从摘要结果反推原始内容,这也是它适合做校验和密码存储的原因。
- 如果计算大文件的摘要,不要一次性将文件内容读取到内存,使用流式读取的方式可以避免内存溢出问题。
注意:摘要算法和加密算法不同,加密算法是可逆的,有对应的解密方法,而摘要算法是不可逆的,只能用于校验内容的一致性,不能用于加密传输敏感数据。
JavaMessageDigestMD5SHA-256修改时间:2026-06-20 06:48:39