在本文中,我们将向您展示如何使用SHA-256和MD5算法为文件生成校验和。
- MessageDigest.getInstance(“算法”)
- Apache Commons编解码器
1. MessageDigest
d:\server.log
hello world
1.1使用SHA256算法生成文件校验和。
FileCheckSumSHA.java
package com.mkyong.hashing;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FileCheckSumSHA {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
MessageDigest md = MessageDigest.getInstance("SHA-256"); //SHA, MD2, MD5, SHA-256, SHA-384...
String hex = checksum("d:\\server.log", md);
System.out.println(hex);
}
private static String checksum(String filepath, MessageDigest md) throws IOException {
// file hashing with DigestInputStream
try (DigestInputStream dis = new DigestInputStream(new FileInputStream(filepath), md)) {
while (dis.read() != -1) ; //empty loop to clear the data
md = dis.getMessageDigest();
}
// bytes to hex
StringBuilder result = new StringBuilder();
for (byte b : md.digest()) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
输出量
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
1.2使用MD5算法生成文件校验和。
FileCheckSumMD5.java
package com.mkyong.hashing;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class FileCheckSumMD5 {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
MessageDigest md = MessageDigest.getInstance("MD5");
String hex = checksum("d:\\server.log", md);
System.out.println(hex);
}
private static String checksum(String filepath, MessageDigest md) throws IOException {
// DigestInputStream is better, but you also can hash file like this.
try (InputStream fis = new FileInputStream(filepath)) {
byte[] buffer = new byte[1024];
int nread;
while ((nread = fis.read(buffer)) != -1) {
md.update(buffer, 0, nread);
}
}
// bytes to hex
StringBuilder result = new StringBuilder();
for (byte b : md.digest()) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
输出量
5eb63bbbe01eeed093cb22bb8f5acdc3
2. Apache Commons编解码器
pom.xml
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
2.1文件校验和。
FileCheckSum.java
package com.mkyong.hashing;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.FileInputStream;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
String checksumSHA256 = DigestUtils.sha256Hex(new FileInputStream("d:\\server.log"));
System.out.println("checksumSHA256 : " + checksumSHA256);
String checksumMD5 = DigestUtils.md5Hex(new FileInputStream("d:\\server.log"));
System.out.println("checksumMD5 : " + checksumMD5);
}
}
输出量
checksumSHA256 : b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
checksumMD5 : 5eb63bbbe01eeed093cb22bb8f5acdc3
参考文献
- Java –可用的MessageDigest算法列表
- Apache Commons编解码器
- Java –如何将字节转换为十六进制
- 维基百科– MD5
- MessageDigest JavaDoc
- Java SHA哈希示例
- Java MD5哈希示例
翻译自: https://mkyong.com/java/how-to-generate-a-file-checksum-value-in-java/