阿根廷网站后缀,scrm企业微信管理系统,宣威网站建设公司,discuz破解为了校验文件在传输中保证完整性和准确性#xff0c;因此需要发送方先对源文件产生一个校验码#xff0c;并将该值传输给接收方#xff0c;将附件通过ftph或http方式传输后#xff0c;由接收方使用相同的算法对接收文件再获取一个新的校验码#xff0c;将该值和发送方传的… 为了校验文件在传输中保证完整性和准确性因此需要发送方先对源文件产生一个校验码并将该值传输给接收方将附件通过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);}}
}