当前位置:网站首页>如何校验两个文件内容是否相同
如何校验两个文件内容是否相同
2022-07-01 02:57:00 【靈熙雲】
今天做文件上传功能,需求要求文件内容相同不能重复上传。感觉这个需求挺简单的就交给了一位刚入行的新同学。等合并代码的时候发现这位同学居然用文件名称相同和文件大小相同作为两个文件相同的依据。这种条件判断可靠吗?
从概率上来说遇到两个文件名称和大小都一样的概率确实太小了。这种判断放在生产环境中也可以稳定的跑上一阵子,不过即使再低的可能性也是有可能的,如果能做到100%就好了。
文件摘要校验我相信同学们都下载过一些好心人开发的小工具,有些小工具会附带一个校验器让你校验附带提供的checksum值,防止有人恶意篡改小工具,保证小工具可以放心使用。

如果两个文件的内容相同,那么它们的摘要应该是相同的。这个原理能不能帮助我们鉴定两个文件是否相同呢?
Java实现文件摘要
/** * 提取文件 checksum * * @param path 文件全路径 * @param algorithm 算法名 例如 MD5、SHA-1、SHA-256等 * @return checksum * @throws NoSuchAlgorithmException the no such algorithm exception * @throws IOException the io exception */
public static String extractChecksum(String path, String algorithm) throws NoSuchAlgorithmException, IOException {
// 根据算法名称初始化摘要算法
MessageDigest digest = MessageDigest.getInstance(algorithm);
// 读取文件的所有比特
byte[] fileBytes = Files.readAllBytes(Paths.get(path));
// 摘要更新
digest.update(fileBytes);
//完成哈希摘要计算并返回特征值
byte[] digested = digest.digest();
// 进行十六进制的输出
return HexUtils.toHexString(digested);
}
内容不变
首先要证明一个文件在内容不变的情况下摘要是否有变化,多次执行下面的代码,断言始终都是true。
String path = "C:\\Users\\s1\\IdeaProjects\\demo\\src\\main\\resources\\application.yml";
String checksum = extractChecksum(path, "SHA-1");
String hash = "6bf4d6c101b4a7821226d3ec1f8d778a531bf265";
Assertions.assertEquals(hash,checksum);
而且我把文件名改成application-dev.yml,甚至application-dev.txt摘要都是相同的。我又把yml文件的内容作了改动,断言就false了。这证明了单个文件的情况下,内容不变,hash是不变的。
文件复制
我把yml文件复制了一份,改了文件名称和类型,不改变内容并存到了另一个目录中,来测试一下它们的摘要是否有变化。
String path1 = "C:\\Users\\s1\\IdeaProjects\\demo\\src\\main\\resources\\application.yml";
String path2 = "C:\\Users\\s1\\IdeaProjects\\demo\\src\\main\\resources\\templates\\application-dev.txt";
String checksum1 = extractChecksum(path1, "SHA-1");
String checksum2 = extractChecksum(path2, "SHA-1");
String hash = "6bf4d6c101b4a7821226d3ec1f8d778a531bf265";
Assertions.assertEquals(hash,checksum1);
Assertions.assertEquals(hash,checksum2);
结果断言通过,不过改变了其中一个文件的内容后断言就不通过了。
新建空文件
这里的新建空文件指的是没有进行任何操作的新建的空文件。新建的空文件会根据特定的算法返回一个固定值,比如SHA-1算法下的空文件值是:
da39a3ee5e6b4b0d3255bfef95601890afd80709
结论
通过实验证明了:
- 在相同算法下,任何
新建空文件的摘要值都是固定的。 - 任何
两个内容相同的文件的摘要值都是相同的,和路径、文件名、文件类型无关。 - 文件的
摘要值会随着文件内容的改变而改变。
边栏推荐
- Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
- 单片机 MCU 固件打包脚本软件
- Voici le programme de formation des talents de SHARE Creators!
- [small program project development -- Jingdong Mall] the home page commodity floor of uni app
- Densenet network paper learning notes
- Dell服务器重启iDRAC方法
- Poj-3486-computers[dynamic planning]
- 鼠标悬停效果十
- Catch 222222
- Restcloud ETL实践之数据行列转换
猜你喜欢

Densenet network paper learning notes

Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and

Cloud native annual technology inventory is released! Ride the wind and waves at the right time

Introduction and basic knowledge of machine learning

php批量excel转word

Saving images of different depths in opencv

Sampling Area Lights

Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend

园区运营效率提升,小程序容器技术加速应用平台化管理

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
随机推荐
鼠标悬停效果四
Magnetic manometer and measurement of foreign coins
Mouse over effect 10
Share Creators萌芽人才培養計劃來了!
[machine learning] vectorized computing -- a must on the way of machine learning
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
Nacos configuration center tutorial
【PR #5 A】双向奔赴(状压DP)
最新接口自动化面试题
Add / delete / modify query summary insert/create/put/add/save/post, delete/drop/remove, update/modify/change, select/get/list/find
【小程序项目开发-- 京东商城】uni-app之分类导航区域
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
MCU firmware packaging Script Software
Here comes the share creators budding talent training program!
使用ipmitool配置X86服务器的BMC网络和用户信息
Is it safe to open an account online in a small securities firm? Will my money be unsafe?
UE4 rendering pipeline learning notes
【微信小程序开发】样式汇总
Poj-3486-computers[dynamic planning]
servlet【初识】