【问题标题】:Get progress of SHA1 being computed using SHA1CryptoServiceProvider获取使用 SHA1CryptoServiceProvider 计算的 SHA1 的进度
【发布时间】:2023-04-10 11:35:01
【问题描述】:

目前我正在我的 C++/CLI 代码中实现一个返回文件 SHA1 值的函数。它是 Visual Studio 中的一个 Windows 窗体应用程序。

我选择实现 .NetFramework 类 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;

【问题讨论】:

  • 你能不能挂钩到流中,即将streamreader包装在你自己的流类中,计算读取的字节数并报告进度?
  • 这种报告进度的方法是我想要实现的。类似这样的事情: 1. 我们打开文件 2. 我们将 X 字节读入缓冲区 3. 我们调用 ComputeHash(buffer) 但是如何编码呢?我的意思是 ComputeHash(buffer)?

标签: .net c++ visual-studio-2010 sha1 progress


【解决方案1】:

我终于做到了。我发布代码,也许有人会发现它有用。我知道代码不干净,我还在学习。它可以计算大于 2^31 字节的文件的 SHA1。在 22GB 文件上进行了测试。在 backgroundWorker 中工作正常 :)

#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);

【讨论】:

  • 我很好奇:计算这个 22GB 文件的哈希需要多长时间(以挂钟时间计)?
  • @TonyK 正好 4:30.05(当然是分钟,而不是小时)文件大小 22 675 783 680 字节,因此散列速度约为 80MB/s。可能会更好......
猜你喜欢
  • 2016-05-23
  • 1970-01-01
  • 2019-07-08
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
  • 2022-11-27
  • 2015-12-09
  • 1970-01-01
相关资源
最近更新 更多