同一字符串有两个不同的哈希值
我需要比较两个字符串的哈希值。 我使用字符串“template”进行测试。 但我得到了这个字符串的不同哈希值,所以它总是不一样。 我使用 CryptoApi 和 MD4
int _tmain(int argc, _TCHAR* argv[])
{
std::hash_map<int,int> table;
HCRYPTPROV hProv1,hProv2;
BYTE *pbBuffer1=(BYTE*)"template";//data to hash
DWORD dwBufferLen1=strlen((char*)pbBuffer1)+1;
HCRYPTHASH hHash1,hHash2;
//first hash
CryptAcquireContext(&hProv1,NULL,NULL,PROV_RSA_AES,0);
CryptCreateHash(hProv1,CALG_MD4,0,0,&hHash1);
CryptHashData(hHash1,pbBuffer1,dwBufferLen1,0);
/*---------*/
BYTE *pbBuffer2=(BYTE*)"template";//data to hash
DWORD dwBufferLen2=strlen((char*)pbBuffer2)+1;
//second hash
CryptAcquireContext(&hProv2,NULL,NULL,PROV_RSA_AES,0);
CryptCreateHash(hProv2,CALG_MD4,0,0,&hHash2);
CryptHashData(hHash2,pbBuffer2,dwBufferLen2,0);
if (hHash1==hHash2)
printf("The Same\n");
else printf("Not same\n");
/*---------*/
std::cout<<hHash1<<std::endl;
std::cout<<hHash2<<std::endl;
if (hProv1)
CryptReleaseContext(hProv1,0);
if (hProv2)
CryptReleaseContext(hProv2,0);
system("pause");
return 0;
}
例如 hHash1 中的哈希值
691136
hHash2 中的哈希值
691216
I need to compare hash values of two strings.
I use string "template" for testing.
But I got different hash values for this string so it is always not the same.
I use CryptoApi and MD4
int _tmain(int argc, _TCHAR* argv[])
{
std::hash_map<int,int> table;
HCRYPTPROV hProv1,hProv2;
BYTE *pbBuffer1=(BYTE*)"template";//data to hash
DWORD dwBufferLen1=strlen((char*)pbBuffer1)+1;
HCRYPTHASH hHash1,hHash2;
//first hash
CryptAcquireContext(&hProv1,NULL,NULL,PROV_RSA_AES,0);
CryptCreateHash(hProv1,CALG_MD4,0,0,&hHash1);
CryptHashData(hHash1,pbBuffer1,dwBufferLen1,0);
/*---------*/
BYTE *pbBuffer2=(BYTE*)"template";//data to hash
DWORD dwBufferLen2=strlen((char*)pbBuffer2)+1;
//second hash
CryptAcquireContext(&hProv2,NULL,NULL,PROV_RSA_AES,0);
CryptCreateHash(hProv2,CALG_MD4,0,0,&hHash2);
CryptHashData(hHash2,pbBuffer2,dwBufferLen2,0);
if (hHash1==hHash2)
printf("The Same\n");
else printf("Not same\n");
/*---------*/
std::cout<<hHash1<<std::endl;
std::cout<<hHash2<<std::endl;
if (hProv1)
CryptReleaseContext(hProv1,0);
if (hProv2)
CryptReleaseContext(hProv2,0);
system("pause");
return 0;
}
For example hash value in hHash1
691136
Hash value in hHash2
691216
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HCRPTHASH
是一个ULONG_PTR
typedef,根据 此处。这意味着它是一个指针:将指针与
==
进行比较就像将果冻(或美国公民的果冻)钉在树上或尝试训练一只猫一样有效:-)您看到的“散列”值实际上是指针,相距 80 个字节(因为它们是指向两个不同内存块的指针)。
为了从句柄中获取实际哈希,您需要类似以下内容的内容,它会打印十六进制数字:
为了比较两个哈希值,您可以执行以下操作:
HCRPTHASH
is aULONG_PTR
typedef, as per here. That means it's a pointer:Comparing pointers with
==
is about as productive as nailing jelly (or jello to US citizens) to a tree, or trying to train a cat :-)The values you're seeing as your "hashes" are actually the pointers, 80 bytes apart (since they are pointers to two different memory blocks).
In order to get the actual hash from the handle, you need something like the following, which prints the hex digits:
For comparing the two hash values, you can do:
正如其他答案所指出的,hHash1 和 hHash2 是不透明的句柄 - 比较它们是没有意义的,因为这类似于比较具有相同值的两个不同对象的地址。
使用
CryptGetHashParam
获取哈希值并进行比较:
As the other answers have noted,
hHash1
andhHash2
are opaque handles — comparing them is pointless, as that's akin to comparing the addresses of two different objects that have the same value.Use
CryptGetHashParam
to get the hash values and compare those instead:hHash1和hHash2不是哈希码,而是句柄。
http://msdn.microsoft。 com/en-us/library/windows/desktop/aa379908(v=vs.85).aspx
hHash1 and hHash2 are not the hash codes, but handles.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379908(v=vs.85).aspx