簡體   English   中英

Boost SHA1哈希與md5sum / sha1sum的運行時比較

[英]runtime comparison of boost SHA1 hashing with md5sum/sha1sum

我使用Boost來計算SHA1哈希,然后將朗姆時間與Linux命令md5sumsha1sum 文件大小為28G。 md5sumsha1sum均為8.4版。

這就是我使用Boost來計算SHA1的方式:

void Sha1_Key(const std::string& inputfilename_, uint32_t* inputfile_sha1)
{
    std::string sha1_key_string;
    uint32_t local_sha1[5];

    for (uint32_t i = 0; i < 5; ++i)
    {
        inputfile_sha1[i] = local_sha1[i] = 0;
    }

    std::ifstream ifs(inputfilename_,std::ios::binary);

    boost::uuids::detail::sha1 sha1;
    std::stringstream sha1_key;
    char buf[128*1024];

    clock_t begin = clock();
    while(ifs.good()) {
      ifs.read(buf,sizeof(buf));
      sha1.process_bytes(buf,ifs.gcount());
    }

    ifs.close();
    sha1.get_digest(local_sha1);
    for(std::size_t i=0; i<sizeof(local_sha1)/sizeof(local_sha1[0]); ++i) {
      sha1_key<<std::hex<<local_sha1[i];
      inputfile_sha1[i] = local_sha1[i];
    }
    sha1_key_string = sha1_key.str();
    clock_t end = clock();
    std::cout << "run time for Boost SHA1: " << double(end - begin)/CLOCKS_PER_SEC << " sec"; 
}

這是運行時間比較:

  • 提升SHA1:170秒

  • md5sum:54.719秒

  • sha1sum:81.795s

sha1sum的復雜度高於md5sum ,因此sha1sum花費的時間增加了50%。 但是Boost SHA1方法的運行時間是Linux sha1sum兩倍。 為什么?

有什么我可以改善我的代碼的嗎? 或對其他哈希方法使用有任何建議?

boost::uuids::detail::sha1 sha1;

看到了嗎? ::detail::

那里的右邊告訴您您正在濫用一個未記錄的實現細節,該細節不適合您使用。

SHA的實現是UUID生成的基礎,並不是為了快速消化大型順序流而設計的。

就這樣。

使用適當的sha1實現(libcrypto,cryptoc ++等)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM