Use MD5 checksum to verify PDF file integrity
When PDF files containing only image content cannot extract text, MD5 checksums provide a reliable way to detect whether the file has been modified.
Calculate MD5 checksum using System.Security.Cryptography.MD5
Using the System.Security.Cryptography.MD5 namespace, the steps to calculate the MD5 checksum are as follows:
<code class="language-csharp">using System.Security.Cryptography.MD5; // 打开PDF文件进行读取 using (var stream = File.OpenRead(filename)) { // 创建MD5哈希对象 using (var md5 = MD5.Create()) { // 从文件流计算哈希值 return md5.ComputeHash(stream); } }</code>
Compare hash results
The generated byte arrays can be compared directly or converted to base64 encoding for easier processing. To output the hash value as a hex string:
<code class="language-csharp">static string CalculateMD5(string filename) { using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(filename)) { var hash = md5.ComputeHash(stream); return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); } } }</code>
By regularly calculating and storing MD5 checksums, you can effectively detect whether any modifications to PDF files have occurred between downloads.
The above is the detailed content of Can MD5 Checksums Detect PDF File Modifications?. For more information, please follow other related articles on the PHP Chinese website!