网站首页 > 教程文章 正文
为了校验文件在传输中保证完整性和准确性,因此需要发送方先对源文件产生一个校验码,并将该值传输给接收方,将附件通过ftph或http方式传输后,由接收方使用相同的算法对接收文件再获取一个新的校验码,将该值和发送方传的校验码进行对比。本文会提供四种算法来生成该校验码,包括:md5、sm3、sha256、crc,其中md5执行速度最快,但是会发生2个文件生成校验码一样的情况(很少发生,项目实际几乎没遇到过),sm3是国密的方式,现在信创系统比较推荐的,sha256我只在集成区块链的项目时遇到过(文件上链一般需要md5和sha256两个值),crc是数据块的多项式除法余数来生成一个固定长度的校验码(在linux环境可以用cksum 路径来生成)
package com;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.zip.CRC32;
/**
* commons-codec-1.10.jar
* commons-io-2.8.0.jar
* bcprov-jdk15on-1.59.jar
*
*/
public class Test {
static Logger logger = LoggerFactory.getLogger(Test.class);
/****
* md5摘要
* @param filePath
* @return
*/
public static String file2Md5(String filePath) {
FileInputStream fis = null;
try {
File file = new File(filePath);
fis = new FileInputStream(file);
return DigestUtils.md5Hex(fis);
}catch (Exception e){
logger.error("获取文件md5异常:"+filePath,e);
return "";
}finally {
IOUtils.closeQuietly(fis);
}
}
/****
* sm3摘要
* bcpov-jdk15on-1.59.jar
*/
public static String file2Sm3(String filePath){
File file = new File(filePath);
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
byte[] bytes = IOUtils.toByteArray(fis);
SM3Digest sm3Digest = new SM3Digest();
sm3Digest.update(bytes,0,bytes.length);
byte bt[] = new byte[sm3Digest.getDigestSize()];
sm3Digest.doFinal(bt, 0);
return ByteUtils.toHexString(bt);
}catch(Exception e){
logger.error("获取文件sm3异常:"+filePath,e);
return "";
}finally {
IOUtils.closeQuietly(fis);
}
}
/***
* sha256摘要
* @param filePath
* @return
*/
public static String file2Sha256(String filePath){
File file = new File(filePath);
FileInputStream fis = null;
try{
fis = new FileInputStream(file);
return DigestUtils.sha256Hex(fis);
}catch (Exception e){
logger.error("获取文件sha256异常:"+filePath,e);
return "";
}finally{
IOUtils.closeQuietly(fis);
}
}
/****
* 循环冗余校验
* @param filePath
* @return
*/
public static String file2Crc32(String filePath) {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
CRC32 crc32 = new CRC32();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
crc32.update(buffer, 0, bytesRead);
}
return String.valueOf(crc32.getValue());
} catch (Exception e) {
logger.error("获取文件crc异常:"+filePath,e);
return "";
}finally {
IOUtils.closeQuietly(bis);
IOUtils.closeQuietly(fis);
}
}
}
猜你喜欢
- 2024-12-06 Android 网络--我是怎么做的: Volley+OkHttp+Https
- 2024-12-06 最全前端加密方式、对称加密DES 非对称加密 RSA加密 MD5 base64
- 2024-12-06 我把公司 10 年老系统改造 Maven,真香
- 2024-12-06 干掉复杂的工具类,国产Java工具类库 Hutool 很香!
- 2024-12-06 ECC加密算法Illegal key size错误源码详解
- 2024-12-06 Java,SSL/TLS协议,单向和双向认证,命令制作及代码生成证书
- 2024-12-06 非对称加密的方法和使用
- 2024-12-06 iText7实现PDF电子签章
- 2024-12-06 java使用SM4加密报错 No such algorithm 解决记录
- 2024-12-06 Java的SM3加密算法,实战教学(附GitHub源码)
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)