当前位置:网站首页>如何校验两个文件内容是否相同
如何校验两个文件内容是否相同
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
结论
通过实验证明了:
- 在相同算法下,任何
新建空文件的摘要值都是固定的。 - 任何
两个内容相同的文件的摘要值都是相同的,和路径、文件名、文件类型无关。 - 文件的
摘要值会随着文件内容的改变而改变。
边栏推荐
- 鼠标悬停效果九
- Restcloud ETL practice to realize incremental data synchronization without identification bit
- Const and the secret of pointers
- ssh配置免密登录时报错:/usr/bin/ssh-copy-id: ERROR: No identities found 解决方法
- Dell server restart Idrac method
- Mouse over effect VI
- Network address translation (NAT) technology
- 鼠标悬停效果一
- Visual effects, picture to cartoon function
- Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
猜你喜欢

Huawei operator level router configuration example | BGP VPLS and LDP VPLS interworking example

Saving images of different depths in opencv

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

Metadata in NFT

Const and the secret of pointers

Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
![[applet project development -- Jingdong Mall] classified navigation area of uni app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[applet project development -- Jingdong Mall] classified navigation area of uni app

记一次服务部署失败问题排查

最新接口自动化面试题

基于OPENCV和图像减法的PCB缺陷检测
随机推荐
鼠标悬停效果五
mybati sql 语句打印
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
Restcloud ETL practice data row column conversion
[wechat applet development] style summary
STM32 - DS18B20 temperature sampling of first-line protocol
php批量excel转word
Here comes the share creators budding talent training program!
如果在小券商办理网上开户安全吗?我的资金会不会不安全?
通信协议——分类及其特征介绍
Saving images of different depths in opencv
Const and the secret of pointers
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
STM32——一线协议之DS18B20温度采样
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
[linear DP] longest common subsequence
XXL job User Guide
园区运营效率提升,小程序容器技术加速应用平台化管理
Pychar open remote directory remote host