使用
using System.Security.Cryptography;
var md5 = MD5.Create()
方式一:
var openFile = File.OpenRead(filePath)
var hash = md5.ComputeHash(openFile);
方式二:
var stream = new BufferedStream((File.OpenRead(openFileDialog1.FileName)), BUFFER_SIZE);
md5.ComputeHash(stream);
1
xiaoyanbot OP 是因为 private const int BUFFER_SIZE = 1200000 导致的?
|
2
xiaoyanbot OP 使用 BufferedStream 之后, 跟 BUFFER_SIZE 也没关系。 换了文件也是出来这个:
D41D8CD98F00B204E9800998ECF8427E |
3
Athrob 2020-06-25 12:22:08 +08:00
https://dotnetfiddle.net/PkbHWP
.Net Core 测试正常 |
4
xiaoyanbot OP @Athrob
整理了一下, 好像是写法的问题: ~~~ using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace ConsoleApp2 { class Program { static void Main(string[] args) { String path = "C:\\Users\\ABC\\Downloads\\DiskGenius.exe"; var md5 = MD5.Create(); var openFile = File.OpenRead(path); var hash = md5.ComputeHash(openFile); Console.WriteLine("计算正确:"); Console.WriteLine(BitConverter.ToString(hash).Replace("-", String.Empty).ToLower()); Console.WriteLine(); Console.WriteLine("独特的:"); var stream = new BufferedStream(openFile); var hash2 = md5.ComputeHash(stream); Console.WriteLine(BitConverter.ToString(hash2).Replace("-", String.Empty).ToLower()); Console.WriteLine(); Console.WriteLine("另外一种用法(计算正确):"); FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); var bufferedStream3 = new BufferedStream(fileStream, 1024 * 4); var hash3 = md5.ComputeHash(bufferedStream3); Console.WriteLine(BitConverter.ToString(hash3).Replace("-", String.Empty).ToLower()); Console.ReadLine(); } } } ~~~ |
5
xiaoyanbot OP 独特的那个,无论换什么文件,都是出来固定的一个字符: D41D8CD98F00B204E9800998ECF8427E
.NET 4.6 (没用 core ) |
6
xiaoyanbot OP stream 情况下, 又获取了一遍 stream 导致的?
|
7
geelaw 2020-06-25 12:34:43 +08:00 via iPhone
难道不是因为第二个调用的时候 openFile 已经到文件结尾了吗?你可以 Seek 回开头再做第二次调用,就会一样了。
|
8
geelaw 2020-06-25 12:37:15 +08:00 via iPhone
另外搜索一下 D41D8CD98F00B204E9800998ECF8427E 就知道这是长度为 0 的字节串的 MD5
|
9
xiaoyanbot OP ~~~
计算正确: 1b3851736679ffb7eb8b06bb751bc3bf 独特的: d41d8cd98f00b204e9800998ecf8427e 另外一种用法(计算正确): 1b3851736679ffb7eb8b06bb751bc3bf ~~~ |
10
xiaoyanbot OP @geelaw 感谢
|
11
xiaoyanbot OP |