【发布时间】:2018-07-14 15:43:22
【问题描述】:
我的应用程序的一部分有望能够对用户指定的文件进行哈希处理,但我被困在实际代码本身上。请注意:
filepath512(.text) = 用户插入文件路径的文本框
fileout(.text) = 输出文本框
button21_click = "Hash/confirm" 按钮用于启动散列算法
当我运行应用程序并运行哈希算法时,什么也没有发生(结果没有出现在输出文本框中)。 几周前,我实际上用相同的代码(好吧,相同的结构)成功地执行了散列算法,并且运行得非常好! 我刚刚开始使用 C#,所以请原谅任何混乱的代码!
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string filePath = e.Argument.ToString();
byte[] buffer;
int bytesRead;
long size;
long totalBytesRead = 0;
using (Stream file = File.OpenRead(filePath))
{
size = file.Length;
using (HashAlgorithm hasher = SHA512.Create())
{
do
{
buffer = new byte[4096];
bytesRead = file.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
backgroundWorker1.ReportProgress((int)((double)totalBytesRead / size * 100));
}
while (bytesRead != 0);
hasher.TransformFinalBlock(buffer, 0, 0);
e.Result = MakeHashString(hasher.Hash);
}
}
}
private static string MakeHashString(byte[] hashbytes)
{
StringBuilder hash = new StringBuilder(32);
foreach (byte b in hashbytes)
hash.Append(b.ToString("X2").ToLower());
return hash.ToString();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar3.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
fileout512.Text = e.Result.ToString();
progressBar3.Value = 0;
}
private void button21_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(filepath512.Text);
}
【问题讨论】:
-
你应该为你的控件命名。
标签: c# algorithm hash cryptography