在网络传输中,传输一些数据或者文件,都可能出现数据丢失或者被篡改的情况,所以就需要对传输的数据或者文件内容进行验证,常有的做法是使用校验和(checksum),先保存一份数据的checksum值到数据库,然后传输过程对数据重新计算checksum值,两个值进行对比,如果一样,说明没有丢失文件内容,也可以说两个文件是一样的。所以checksum不仅可以用来校验网络传输有没有丢包,也可以用来校验上传的两个文件是不是一样的。
校验和(checksum),是应用于网络传输中校验数据完整性一种常见方法,以检查是否已收到完整的消息。有几种常见的校验和生成算法,例如 Adler32 和 CRC32,当然也可以使用MD5、哈希函数算法SHA256等等来生成checksum值
在java中实现checksum可以使用jdk提供的CRC32
来实现,而对于文件的,可以使用CheckedInputStream
来处理文件流,提高处理效率
package com.test.util;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
@Slf4j
public class ChecksumUtil {
/**
* 获取checksum值
*
* @Date 2024/07/31 17:05
* @Param [stream, bufferSize]
* @return long
*/
public static long getChecksumCRC32(byte[] bytes) {
Long checksum = null;
try {
Checksum crc32 = new CRC32();
crc32.update(bytes, 0, bytes.length);
checksum = crc32.getValue();
} catch (Exception e) {
log.error("getChecksumCRC32 exception:{}", e);
}
return checksum;
}
/**
* 获取checksum值,文件方式
*
* @Date 2024/07/31 17:05
* @Param [stream, bufferSize]
* @return long
*/
public static long getChecksumCRC32(InputStream stream, int bufferSize) throws IOException {
CheckedInputStream checkedInputStream = new CheckedInputStream(stream, new CRC32());
byte[] buffer = new byte[bufferSize];
while (checkedInputStream.read(buffer, 0, buffer.length) >= 0) {}
long checkSum = checkedInputStream.getChecksum().getValue();
stream.close();
checkedInputStream.close();
return checkSum;
}
/**
* 获取checksum值,url是远程文件的url
*
* @Date 2024/07/31 17:05
* @Param [stream, bufferSize]
* @return long
*/
public static long getChecksumCRC32(String url, int bufferSize){
Long checksum = null;
try {
InputStream fileInputStream = new URL(url).openStream();
checksum = getChecksumCRC32(fileInputStream, bufferSize);
return checksum;
} catch (Exception e) {
log.error("getChecksumCRC32 exception:{}", e);
}
return checksum;
}
}
测试一下文本和文件的checksum获取
@Test
public void test01() {
String content = "123456";
long textChecksum = ChecksumUtil.getChecksumCRC32(content.getBytes());
String fileUrl = "http://test.com/1.jpeg";
long fileChecksum = ChecksumUtil.getChecksumCRC32(fileUrl,4096);
}
也可以使用md5、sha-256等算法,获取摘要的值
package com.test.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.buf.HexUtils;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@Slf4j
public class ChecksumUtil {
public static final String MD5 = "MD5";
public static final String SHA1 = "SHA-1";
public static final String SHA256 = "SHA-256";
public static String getChecksum(byte[] buffer, String algorithm) {
String checksum = StrUtil.EMPTY;
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
messageDigest.update(buffer);
byte[] digested = messageDigest.digest();
checksum = HexUtils.toHexString(digested);
} catch (Exception e) {
if (e instanceof NoSuchAlgorithmException) {
log.error("noSuchAlgorithm exception:{}", e);
} else {
log.error("getChecksum exception:{}", e);
}
}
return checksum;
}
public static String getChecksum(String url, String algorithm) {
String checksum = StrUtil.EMPTY;
try {
byte[] buffer = HttpUtil.downloadBytes(url);
checksum = getChecksum(buffer, algorithm);
} catch (Exception e) {
if (e instanceof IOException) {
log.error("ioException:{}", e);
} else {
log.error("getChecksum exception:{}", e);
}
}
return checksum;
}
}
测试一下checksum获取
public void test02() {
String checksum = ChecksumUtil.getChecksum(String fileUrl = "http://test.com/1.jpeg";
, ChecksumUtil.MD5);
String checksum = ChecksumUtil.getChecksum("123456".getBytes(), ChecksumUtil.MD5);
}