【发布时间】:2019-07-22 18:34:34
【问题描述】:
关于这个领域,我的问题/问题可能有点新手,但我找不到任何解决方案或明确的解释来实现我想要的。
问题
由于大小,我必须将哈希值存储和使用为 BYTE 而不是 STRING。 (这给我带来了其他方面的麻烦)
该函数为文件生成一个 MD5 哈希,用于 windows 操作系统。
当前代码
std::string MD5Checksum(const path &file)
{
std::string result;
try
{
CryptoPP::Weak::MD5 md5;
CryptoPP::HashFilter f5(md5, new CryptoPP::HexEncoder(new CryptoPP::StringSink(result)));
CryptoPP::ChannelSwitch cs;
cs.AddDefaultRoute(f5);
CryptoPP::FileSource ss(file.string().c_str(), true /*pumpAll*/, new CryptoPP::Redirector(cs));
}
catch (CryptoPP::Exception const& exception)
{
//
}
return result;
}
我测试了什么
std::string MD5Checksum(const path &file)
{
std::string result;
try
{
CryptoPP::Weak::MD5 md5;
CryptoPP::HashFilter f5(md5, new CryptoPP::HexEncoder(new CryptoPP::StringSink(result)));
CryptoPP::ChannelSwitch cs;
cs.AddDefaultRoute(f5);
CryptoPP::FileSource ss(file.string().c_str(), true /*pumpAll*/, new CryptoPP::Redirector(cs));
}
catch (CryptoPP::Exception const& exception)
{
//
}
string decoded;
CryptoPP::StringSource ss(result, true /*pumpAll*/, new CryptoPP::StringSink(decoded));
const BYTE* data = reinterpret_cast<const BYTE*>(decoded.data());
printf(L"sizeof result: %d, sizeof data: %d"), sizeof(result), sizeof(data));
return result;
}
这似乎达到了预期的结果,因为结果字符串的大小是 40,数据的大小是 8,这对我来说是一个巨大的减少。
但是我不认为这是一个好的解决方案,我很确定必须有一种更简单、更清洁的方法。
非常感谢任何示例。
【问题讨论】:
-
sizeof的指针总是指针本身的大小,而不是它可能指向的大小。 -
@Someprogrammerdude 你让我思考...
-
此外,对象的
sizeof是对象本身的大小,而不是它可能包装的任何数据(std::string本质上是指向字符串数据的指针,以及数据的大小,可能还有一些内部元数据用于管理)。 -
@Someprogrammerdude 那么我的结果是假的吗?我以为 sizeof 返回数据的大小...
-
std::string的长度是size()或length()成员函数的返回值。