使用C++中的`memcmp`比较哈希摘要。

huangapple go评论54阅读模式
英文:

comparing hash digests with memcmp in C++

问题

我目前正在为一个生成MD5哈希的函数编写一些测试。该函数返回一个unsigned char*。我在测试中将一个参考样本硬编码以进行比较。根据我的研究,似乎`memcmp`是正确的方法,但是我在结果方面遇到了问题。

在终端打印时它们是匹配的,但是`memcmp`返回一个负匹配。

CODE示例:
```cpp
unsigned char ref_digest[] = "d41d8cd98f00b204e9800998ecf8427e";
unsigned char *calculated_digest = md5_gen_ctx.get_digest();

std::cout << std::setfill('0') << std::setw(2) << std::hex << ref_digest << endl;

for(int i = 0; i < MD5_DIGEST_LENGTH; i++) {
    std::cout << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(calculated_digest[i]);
}
cout << endl;
int compare = std::memcmp(calculated_digest, ref_digest , MD5_DIGEST_LENGTH);
cout << "Comparison result: " << compare << endl;

输出

2: Test timeout computed to be: 10000000
2: d41d8cd98f00b204e9800998ecf8427e
2: d41d8cd98f00b204e9800998ecf8427e
2: Comparison result: 70

有人能指导我在这里做错了什么吗?我在想我的参考哈希定义是否有问题。在测试中有更好的管理比较的方法吗?

谢谢。


<details>
<summary>英文:</summary>

I&#39;m currently writing some tests for an MD5 hash generating function. The functions returns an unsigned char*. I have a reference sample to compare to hard coded into the test. From my research it appears that `memcmp` is the correct way to go, however I am having issues with the results. 

When printed to the terminal they match, however `memcmp` is returning a negative match. 

CODE sample:

unsigned char ref_digest[] = "d41d8cd98f00b204e9800998ecf8427e";
unsigned char *calculated_digest = md5_gen_ctx.get_digest();

std::cout << std::setfill('0') << std::setw(2) << std::hex << ref_digest << endl;

for(int i = 0; i &lt; MD5_DIGEST_LENGTH; i++) {
    std::cout &lt;&lt; std::setfill(&#39;0&#39;) &lt;&lt; std::setw(2) &lt;&lt; std::hex &lt;&lt; static_cast&lt;int&gt;(calculated_digest[i]);
}
cout &lt;&lt; endl;
int compare = std::memcmp(calculated_digest, ref_digest , MD5_DIGEST_LENGTH);
cout &lt;&lt; &quot;Comparison result: &quot; &lt;&lt; compare &lt;&lt; endl;

OUTPUT

2: Test timeout computed to be: 10000000
2: d41d8cd98f00b204e9800998ecf8427e
2: d41d8cd98f00b204e9800998ecf8427e
2: Comparison result: 70


Can anyone guide me as to what I am doing incorrectly here? I am wondering if there are issues with the definition of my reference hash. Is there a better way of managing this comparison for the test? 

Cheers.


</details>


# 答案1
**得分**: 2

这是错误的:

```c
unsigned char ref_digest[] = &quot;d41d8cd98f00b204e9800998ecf8427e&quot;;

这是一个包含32个字符的字符串,而你想要的是一个包含16个字节的数组。请注意,两个十六进制字符(4+4位)对应一个字节。

要修复它,你可以使用一对64位整数:

uint64_t ref_digest[] = {htobe64(0xd41d8cd98f00b204), htobe64(0xe9800998ecf8427e)};

我使用了htobe64()函数来按正确的顺序放置字节,例如0xd4需要是第一个字节。

英文:

This is wrong:

unsigned char ref_digest[] = &quot;d41d8cd98f00b204e9800998ecf8427e&quot;;

That is a string of 32 characters, when what you want is an array of 16 bytes. Note that two hexadecimal characters (4+4 bits) corresponds to one byte.

To fix it, you can use a pair of 64-bit integers:

uint64_t ref_digest[] = {htobe64(0xd41d8cd98f00b204), htobe64(0xe9800998ecf8427e)};

I used htobe64() to put the bytes in the correct order, e.g. 0xd4 needs to be the first byte.

huangapple
  • 本文由 发表于 2023年1月9日 01:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定