[英]runtime comparison of boost SHA1 hashing with md5sum/sha1sum
我使用Boost来计算SHA1哈希,然后将朗姆时间与Linux命令md5sum
和sha1sum
。 文件大小为28G。 md5sum
和sha1sum
均为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.