当前位置:网站首页>How to verify whether the contents of two files are the same
How to verify whether the contents of two files are the same
2022-07-01 03:01:00 【Lingxiyun】
Do file upload function today , The requirements require that the contents of the documents are the same and cannot be uploaded repeatedly . I feel this demand is very simple, so I gave it to a new classmate who has just entered the industry . When merging the code, I found that the student actually used the same file name and the same file size as the basis for the same two files . Is this condition reliable ?
In terms of probability, the probability of encountering two files with the same name and size is really too small . This kind of judgment can run stably in the production environment for a while , But even the lowest possibility is possible , If we can do it 100% Just fine .
I believe the students have downloaded some small tools developed by kind people , Some gadgets come with a verifier that allows you to verify the provided checksum value , Prevent someone from maliciously tampering with gadgets , Ensure that gadgets can be used safely .
If the contents of the two files are the same , Then their summaries should be the same . Can this principle help us identify whether the two documents are the same ?
Java Implement file summary
/** * Extract files checksum * * @param path File full path * @param algorithm Algorithm name for example MD5、SHA-1、SHA-256 etc. * @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 {
// Initialize the digest algorithm according to the algorithm name
MessageDigest digest = MessageDigest.getInstance(algorithm);
// Read all bits of the file
byte[] fileBytes = Files.readAllBytes(Paths.get(path));
// Summary update
digest.update(fileBytes);
// Complete the hash summary calculation and return the characteristic value
byte[] digested = digest.digest();
// Hexadecimal output
return HexUtils.toHexString(digested);
}
The content remains the same
First of all, it is necessary to prove whether the summary of a document changes when the content remains unchanged , Execute the following code multiple times , Assertions are always 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);
And I changed the file name to application-dev.yml
, even to the extent that application-dev.txt
The summaries are the same . I have put the yml The contents of the document have been changed , Assertion on false 了 . This proves the case of a single document , The content remains the same ,hash It is the same. .
File replication
I put yml A copy of the document , Changed the file name and type , The unchanged contents coexist in another directory , To test whether their summaries have changed .
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);
The result assertion passed , However, after changing the content of one of the files, the assertion will not pass .
New empty file
The new empty file here refers to the new empty file without any operation .
The new empty file will return a... According to a specific algorithm Fixed value
, such as SHA-1
The empty file value under the algorithm is :
da39a3ee5e6b4b0d3255bfef95601890afd80709
Conclusion
It is proved by experiments that :
- Under the same algorithm , whatever
The summary values of new empty files are fixed
. - whatever
The summary values of two files with the same content are the same
, And the path 、 file name 、 File type independent . - Of documents
The summary value will change with the content of the file
.
边栏推荐
- Network address translation (NAT) technology
- 股票开户安全吗?上海股票开户步骤。
- MCU firmware packaging Script Software
- 股票开账户如何优惠开户?还有,在线开户安全么?
- Mouse over effect III
- [small program project development -- Jingdong Mall] the home page commodity floor of uni app
- Prototype and prototype chain in JS
- Mouse over effect 10
- Scale SVG to container without mask / crop
- Mnasnet learning notes
猜你喜欢
STM32——一线协议之DS18B20温度采样
Servlet [first introduction]
DenseNet网络论文学习笔记
旷世轻量化网络ShuffulNetV2学习笔记
基于OPENCV和图像减法的PCB缺陷检测
【小程序项目开发 -- 京东商城】uni-app 商品分类页面(上)
联想X86服务器重启管理控制器(XClarity Controller)或TSM的方法
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
Completely solve the lost connection to MySQL server at 'reading initial communication packet
【机器学习】向量化计算 -- 机器学习路上必经路
随机推荐
VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license
【小程序项目开发-- 京东商城】uni-app之首页商品楼层
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
A shooting training method based on the digital measurement of Joule energy and posture of sphygmomanometer air bag with standard air pressure
MCU firmware packaging Script Software
鼠标悬停效果六
鼠标悬停效果七
Sampling Area Lights
Share Creators萌芽人才培养计划来了!
Metadata in NFT
鼠标悬停效果五
PTA 1017
Contrastive learning of Class-agnostic Activation Map for Weakly Supervised Object Localization and
【EXSI】主机间传输文件
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
安装VCenter6.7【VCSA6.7(vCenter Server Appliance 6.7) 】
Xception learning notes
第03章_用户与权限管理
[PR # 5 A] two way running (state pressure DP)
[applet project development -- JD mall] uni app commodity classification page (Part 2)