当前位置:网站首页>如何校验两个文件内容是否相同
如何校验两个文件内容是否相同
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
结论
通过实验证明了:
- 在相同算法下,任何
新建空文件的摘要值都是固定的。 - 任何
两个内容相同的文件的摘要值都是相同的,和路径、文件名、文件类型无关。 - 文件的
摘要值会随着文件内容的改变而改变。
边栏推荐
- Gartner research: in China, the adoption of hybrid cloud has become the mainstream trend
- Network address translation (NAT) technology
- Introduction and basic knowledge of machine learning
- Saving images of different depths in opencv
- VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
- 旷世轻量化网络ShuffulNetV2学习笔记
- 鼠标悬停效果六
- Is it safe to open a stock account? Shanghai stock account opening procedures.
- How to buy Hong Kong shares in China? What platform is safer?
- Od modify DLL and exe pop-up contents [OllyDbg]
猜你喜欢

Completely solve the lost connection to MySQL server at 'reading initial communication packet

Gartner研究:在中国,混合云的采用已成为主流趋势

Complete training and verification of a neural network based on pytorch
![[QT] add knowledge supplement of third-party database](/img/ea/ca8b07ad80485208f2bb8ee8a78a28.png)
[QT] add knowledge supplement of third-party database

PCB defect detection based on OpenCV and image subtraction

Example of Huawei operator level router configuration | example of configuring optionc mode cross domain LDP VPLS

【Qt】添加第三方库的知识补充

Dell server restart Idrac method

安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】

产业互联网中,「小」程序有「大」作为
随机推荐
访问url 404 的错误
Lenovo x86 server restart management controller (xclarity controller) or TSM method
Mouse over effect I
js 找出两个数组中的重复元素
A small document of JS method Encyclopedia
鼠标悬停效果七
Pychar open remote directory remote host
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
鼠标悬停效果五
Xception learning notes
js中的原型和原型链
How to determine the progress bar loaded in the loading interface when opening the game
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
产业互联网中,「小」程序有「大」作为
Nacos configuration center tutorial
Network address translation (NAT) technology
Codeforces Round #416 (Div. 2) C. Vladik and Memorable Trip
使用ipmitool配置X86服务器的BMC网络和用户信息
Voici le programme de formation des talents de SHARE Creators!
UE4 rendering pipeline learning notes