同一字符串有两个不同的哈希值

发布于 2024-12-25 00:54:41 字数 1312 浏览 2 评论 0原文

我需要比较两个字符串的哈希值。 我使用字符串“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柳絮泡泡 2025-01-01 00:54:41

HCRPTHASH 是一个 ULONG_PTR typedef,根据 此处。这意味着它是一个指针:

HCRYPTHASH 数据类型用于表示哈希对象的句柄。这些句柄向 CSP 模块指示特定操作中正在使用哪个哈希。 CSP 模块无法直接操作哈希值。相反,用户通过哈希句柄来操作哈希值。

typedef ULONG_PTR HCRYPTHASH;

将指针与 == 进行比较就像将果冻(或美国公民的果冻)钉在树上或尝试训练一只猫一样有效:-)

您看到的“散列”值实际上是指针,相距 80 个字节(因为它们是指向两个不同内存块的指针)。

为了从句柄中获取实际哈希,您需要类似以下内容的内容,它会打印十六进制数字:

CHAR hexDigits[] = "0123456789abcdef";
BYTE md4Hash[MD4LEN];
DWORD cbHash = MD4LEN;
if (CryptGetHashParam (hHash1, HP_HASHVAL, md4Hash, &cbHash, 0)) {
    printf("MD4 hash is: ");
    for (DWORD i = 0; i < cbHash; i++) {
        printf ("%c%c", hexDigits[md4Hash[i] >> 4], hexDigits[md4Hash[i] & 0xf]);
    }
    printf("\n");
} else {
    DWORD dwStatus = GetLastError();
    printf ("CryptGetHashParam failed with code %d\n", dwStatus); 
}

为了比较两个哈希值,您可以执行以下操作:

BYTE md4Hash1[MD4LEN], md4Hash2[MD4LEN];
DWORD cbHash1 = MD4LEN, cbHash2 = MD4LEN;

CryptGetHashParam (hHash1, HP_HASHVAL, md4Hash1, &cbHash1, 0);
CryptGetHashParam (hHash2, HP_HASHVAL, md4Hash2, &cbHash2, 0);

if ((cbHash1 == cbHash2) &&
    (memcmp (md4Hash1, md4Hash2, cbHash1) == 0))
{
    // they are equal.
}

HCRPTHASH is a ULONG_PTR typedef, as per here. That means it's a pointer:

The HCRYPTHASH data type is used to represent handles to hash objects. These handles indicate to the CSP module which hash is being used in a particular operation. The CSP module does not enable direct manipulation of the hash values. Instead, the user manipulates the hash values through the hash handle.

typedef ULONG_PTR HCRYPTHASH;

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:

CHAR hexDigits[] = "0123456789abcdef";
BYTE md4Hash[MD4LEN];
DWORD cbHash = MD4LEN;
if (CryptGetHashParam (hHash1, HP_HASHVAL, md4Hash, &cbHash, 0)) {
    printf("MD4 hash is: ");
    for (DWORD i = 0; i < cbHash; i++) {
        printf ("%c%c", hexDigits[md4Hash[i] >> 4], hexDigits[md4Hash[i] & 0xf]);
    }
    printf("\n");
} else {
    DWORD dwStatus = GetLastError();
    printf ("CryptGetHashParam failed with code %d\n", dwStatus); 
}

For comparing the two hash values, you can do:

BYTE md4Hash1[MD4LEN], md4Hash2[MD4LEN];
DWORD cbHash1 = MD4LEN, cbHash2 = MD4LEN;

CryptGetHashParam (hHash1, HP_HASHVAL, md4Hash1, &cbHash1, 0);
CryptGetHashParam (hHash2, HP_HASHVAL, md4Hash2, &cbHash2, 0);

if ((cbHash1 == cbHash2) &&
    (memcmp (md4Hash1, md4Hash2, cbHash1) == 0))
{
    // they are equal.
}
悲凉≈ 2025-01-01 00:54:41

正如其他答案所指出的,hHash1 和 hHash2 是不透明的句柄 - 比较它们是没有意义的,因为这类似于比较具有相同值的两个不同对象的地址。

使用CryptGetHashParam获取哈希值并进行比较:

CryptGetHashParam 函数检索控制哈希对象操作的数据。 使用该函数可以获取实际的哈希值。

As the other answers have noted, hHash1 and hHash2 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:

The CryptGetHashParam function retrieves data that governs the operations of a hash object. The actual hash value can be retrieved by using this function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文