使用SHA1CryptoServiceProvider获取正在计算的SHA1的进度
Get progress of SHA1 being computed using SHA1CryptoServiceProvider
当前我正在C++/CLI代码中实现一个函数,该函数返回文件的SHA1值。它是Visual Studio中的Windows窗体应用程序。
我之所以选择实现.Net Framework类SHA1CryptoServiceProvider,是因为它真的很快(相信我)。我测试了几种算法,但没有一种能像SHA1CryptoServiceProvider类那样快。
问题是,在我的应用程序中,有一个progressBar显示了计算SHA1的进度,而SHA1CryptoServiceProvider类没有任何返回计算SHA1进度的函数。
这是代码:
using namespace System::Security::Cryptography;
using namespace System::IO;
StreamReader^ Reader = gcnew StreamReader("C:\abc.exe");
SHA1CryptoServiceProvider^ SHA1 = gcnew SHA1CryptoServiceProvider();
String^ Hash = "";
Hash = BitConverter::ToString(SHA1->ComputeHash(Reader->BaseStream));
return Hash;
我终于完成了。我发布了代码,也许有人会觉得它很有用。我知道代码不干净,我还在学习。它可以计算大于2^31字节的文件的SHA1。在22GB的文件上进行了测试。在后台工作良好工人:)
#define SHA1_BUFFER_SIZE 65535
//input buffer
array<unsigned char,1>^ buf = gcnew array<unsigned char,1>(SHA1_BUFFER_SIZE);
pin_ptr<unsigned char> pointer = &buf[0];
//Open file in 64-bit mode
FILE *file = _fopeni64("some_large_file.txt","rb");
SHA1CryptoServiceProvider^ SHA1 = gcnew SHA1CryptoServiceProvider();
//Move pointer to End of File
_fseeki64(file,0,SEEK_END);
//Read pointer position (file size)
unsigned __int64 size = (__int64)_ftelli64(file);
// Move pointer to begining of file
_fseeki64(file,0,SEEK_SET);
__int64 i = 1; // counter
float wyn = 0; // help variable for progress Percentage (float)
__int64 conv = 0; // help variable for progress Percentage (int)
//output buffer
array<unsigned char,1>^ outputbuffer = gcnew array<unsigned char,1>(SHA1_BUFFER_SIZE);
while(1)
{
//Read SHA1_BUFFER_SIZE bytes to buffer
size_t bufLen = fread( pointer, 1, SHA1_BUFFER_SIZE, file );
if (bufLen == 0) //End of file
{
if (ferror(file)) //Error opening file
return;
break;
}
//buffer has the last block of bytes of the file
if ( SHA1_BUFFER_SIZE*i >= size )
SHA1->TransformFinalBlock(buf,0,bufLen);
else
SHA1->TransformBlock(buf,0,bufLen,outputbuffer,0);
wyn = SHA1_BUFFER_SIZE*100; /* Getting */
wyn /= size; /* the */
wyn *= i; /* progress */
conv = wyn; /* Percentage */
ComputeSHA1->ReportProgress(conv);
\Update our progressBar
i++;
} //End main loop
String^ sHash = "";
//Get the computed hash and convert to System::String
sHash = BitConverter::ToString(SHA1->Hash);
//Replace the '-' characters in hash to white spaces
sHash = sHash->Replace('-',' ');
//Removing whitespaces from hash
sHash = System::Text::RegularExpressions::Regex::Replace(sHash, "\s+", System::String::Empty);
//Filling a textBox with the SHA1
ComputeSHA1->ReportProgress(0,sHash);
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 递归函数计算序列中的平方和(并输出过程)
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的字符计数代码计算错误.为什么
- 在计算中使用二的幂有多有利可图
- 如何计算文件中的"columns"数?
- 计算排序向量的向量中唯一值的计数
- 如何使用 std::累积在 C++ 中计算总和立方体
- 使用Qt C++计算类似Git的SHA1哈希
- OpenCV C++.快速计算混淆矩阵
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- C++如何计算用户输入的数字中的偶数位数
- 如何计算数据类型的范围,例如int
- 类似枚举的计算常量
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 计算缩放多边形的比例,得到给定的多边形面积
- 如何在C++中可移植地计算sha1哈希
- 如何计算sha1哈希图像
- 使用SHA1CryptoServiceProvider获取正在计算的SHA1的进度