MD5功能无法正常工作
MD5 function not functioning properly
这是我当前的MD5函数。在 Windows 8.1 上使用时,它始终返回一个值,但在 Windows 7 上运行时,它只返回大约 50% 的时间的值。这对我来说很奇特。有什么想法吗?
事实证明,它在win7和win8上都是狡猾的。显然,对CryptGetHashParam的调用有时会因ERROR_MORE_DATA而失败。
std::string MD5(std::string input)
{
HCRYPTPROV CryptProv;
HCRYPTHASH CryptHash;
BYTE BytesHash[33];//!
DWORD dwHashLen;
std::string final;
if (CryptAcquireContext(&CryptProv,NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET))
{
if (CryptCreateHash(CryptProv, CALG_MD5, 0, 0, &CryptHash))
{
if (CryptHashData(CryptHash, (BYTE*)input.c_str(), input.length(), 0))
{
if (CryptGetHashParam(CryptHash, HP_HASHVAL, BytesHash, &dwHashLen, 0))
{
final.clear();
std::string hexcharset = "0123456789ABCDEF";
for (int j = 0; j < 16; j++)
{
final += hexcharset.substr(((BytesHash[j] >> 4) & 0xF), 1);
final += hexcharset.substr(((BytesHash[j]) & 0x0F), 1);
}
}
}
}
}
CryptDestroyHash(CryptHash);
CryptReleaseContext(CryptProv, 0);
return final;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pp = "derp";
std::string derp = MD5(std::string(pp));
printf("%sn", derp.c_str());
system("pause");
return 0;
}
我有一个绝妙的主意:在这些条件中添加else
子句,以便您知道哪个条件失败了。将输出放在这些子句中,以便您可以查看有关错误条件的详细信息。然后你就会知道你的函数出了什么问题。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程