JBa*_*ber 6 .net c# hash md5 stream
问题就是这一切.这段代码
string hash = "";
using (var md5 = System.Security.Cryptography.MD5.Create())
{
hash = Convert.ToBase64String(md5.ComputeHash(streamReader.BaseStream));
}
Run Code Online (Sandbox Code Playgroud)
将始终返回相同的哈希值.
如果我将所有数据从BaseStream传递到MemoryStream,它每次都会给出一个唯一的哈希值.跑步也是如此
string hash = "";
using (var md5 = System.Security.Cryptography.MD5.Create())
{
hash = Convert.ToBase64String(md5.ComputeHash(
Encoding.ASCII.GetBytes(streamReader.ReadToEnd())));
}
Run Code Online (Sandbox Code Playgroud)
第二个实际上更快,但我听说这是不好的做法.
我的问题是,使用ComputeHash(流)的正确方法是什么.对我而言,它总是(我的意思是,即使我重新启动程序,意味着它不仅仅是散列引用)也会返回相同的散列,而不管流中的数据如何.
dtb*_*dtb 13
该Stream实例可能位于流的末尾.ComputeHash返回从当前位置到流末尾的字节的哈希值.因此,如果当前位置是流的结尾,则它将为空输入的哈希.确保Stream实例位于流的开头.