gpt4 book ai didi

c# - 如何从文本文件创建 MD5 散列摘要?

转载 作者:IT王子 更新时间:2023-10-29 04:27:28 25 4
gpt4 key购买 nike

我想使用 C# 创建一个文本文件的 MD5 散列。我怎样才能做到这一点?

更新:感谢大家的帮助。我终于确定了以下代码 -

// Create an MD5 hash digest of a file
public string MD5HashFile(string fn)
{
byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
return BitConverter.ToString(hash).Replace("-", "");
}

最佳答案

这是我目前正在使用的例程。

    using System.Security.Cryptography;

public string HashFile(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return HashFile(fs);
}
}

public string HashFile( FileStream stream )
{
StringBuilder sb = new StringBuilder();

if( stream != null )
{
stream.Seek( 0, SeekOrigin.Begin );

MD5 md5 = MD5CryptoServiceProvider.Create();
byte[] hash = md5.ComputeHash( stream );
foreach( byte b in hash )
sb.Append( b.ToString( "x2" ) );

stream.Seek( 0, SeekOrigin.Begin );
}

return sb.ToString();
}

关于c# - 如何从文本文件创建 MD5 散列摘要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2150455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com