简体   繁体   English

在32位数组上复制16位哈希

[英]Copy 16 bits hash on 32 bits array

I have a problem with my use of sprintf and strcat . 我对sprintfstrcat使用有问题。 Here's my code : 这是我的代码:

unsigned char hashResults[8][16];
unsigned char tmp[2];
unsigned char hash[8][32];

transformation("toto", 4, hashResults);

for (int k = 0; k < 8; ++k)
{
    for (int i = 0; i < 16; ++i)
    {
        sprintf(tmp,"%2.2x",hashResults[k][i]);
        strcat(hash[k],tmp);
    }
    printf("%d \n%s\n", strlen(hash[k]), hash[k]);
}
printf("Test : %s\n", hash[3]);

The function transformation() gives me 8 hashes on 16 bits. 函数transformation()在16位上给了我8个哈希。

I use sprintf and strcat to get hash on 32 bits. 我使用sprintfstrcat获取32位哈希值。 When I try to read all hash[k] , strlen(hash[8]) returns to me 32 (it's correct), and the string is correct too. 当我尝试读取所有hash[k]strlen(hash[8])返回32(正确),字符串也正确。 But, when I try to read hash[3] outside the loop it contains all values after it. 但是,当我尝试在循环外读取hash[3] ,它包含其后的所有值。

For example, the output of my program : 例如,我程序的输出:

32 - 4a18e332afba75b9734e875323f452f8

32 - b96833277faf31a5915c769f44634506

32 - f89f6dd8cd5aee79de3b2c0c27cafe2e

32 - c9f629472c862c1e7542f4cb2835d02b

32 - 09fc12cfb0a81a38513dbd5edff19e52

32 - 35564354793555a3ae1382f647044445

Test : b96833277faf31a5915c769f44634506f89f6dd8cd5aee79de3b2c0c27cafe2ec9f629472c862c1e7542f4cb2835d02b09fc12cfb0a81a38513dbd5edff19e5235564354793555a3ae1382f647044445

Does anyone see the problem? 有人看到这个问题吗? I wish to use these hashes to compare them to others hashes. 我希望使用这些哈希将它们与其他哈希进行比较。

You should increase unsigned char tmp[2] to unsigned char tmp[3] and unsigned char hash[8][32] to unsigned char hash[8][33]; 您应该将unsigned char tmp[2]增加到unsigned char tmp[3]并将unsigned char hash[8][32]增加到unsigned char hash[8][33];

One problem is sprintf(tmp,"%2.2x",hashResults[k][i]) , because it writes two characters + '\\0' which takes three elements in the array tmp . 一个问题是sprintf(tmp,"%2.2x",hashResults[k][i]) ,因为它写了两个字符+'\\ 0',这在数组tmp占用了三个元素。

But the biggest problem is strcat(hash[k],tmp); 但是最大的问题是strcat(hash[k],tmp); . At the end of every inner for loop you have written in the hash[k] arrays 33 characters(32 chars + '\\0'). 在每个内部for循环的结尾,您已经在hash [k]数组中编写了33个字符(32个字符+'\\ 0')。 What happens is when you populate one of the hash[k] arrays in the inner for loop you also write \\0' to the first element in the next array, that's why printf("%d \\n%s\\n", strlen(hash[k]), hash[k]); 当在内部for循环中填充一个hash[k]数组时,还会在下一个数组的第一个元素中写入\\0' ,这就是为什么printf("%d \\n%s\\n", strlen(hash[k]), hash[k]); prints correct results. 打印正确的结果。 That misleads you to believing you have null terminated the hash[k] arrays. 这使您误以为您已经终止了null hash[k]数组。 Now when you enter the next inner for loop you override the null you have written to this array the previous time you exited the inner for loop, no longer terminating the previous array. 现在,当您进入下一个内部for循环时,您将覆盖上一次退出内部for循环时已写入该数组的null,而不再终止先前的数组。

So you null terminate your hash[k] array by writing zero to the first element of the hash[k+1] array on the exit of the inner for loop. 因此,您可以通过在内部for循环的出口处将0写入hash[k+1]数组的第一个元素来终止hash[k]数组。 Then you override this null value every time you enter the inner for loop. 然后,每次进入内部for循环时,您都将覆盖此null值。

In the end you have no terminating nulls in your arrays and only the final null on the last array is there. 最后,您的数组中没有终止空值,只有最后一个数组中的最终空值存在。

I wonder how do you even get this to work every time, because you write the terminating null beyond the array size which leads to undefined behavior. 我不知道您怎么会每次都使用它,因为您写的终止null超出了数组大小,导致未定义行为。

Making tmp[3] and hash[8][33] should fix your problem. 制作tmp[3]hash[8][33]应该可以解决您的问题。

In order for the strcat() function to work properly you have to have at least one null in the array you are concatenating to, because otherwise it wouldn't know where to concatenate to. 为了使strcat()函数正常工作,您必须在要连接的数组中至少有一个null,因为否则它将不知道在何处连接。 You have to add hash[k][0] = 0; 您必须添加hash [k] [0] = 0; before entering every inner loop: 进入每个内部循环之前:

for (int k = 0; k < 8; ++k)
{
    hash[k][0] = 0;
    for (int i = 0; i < 16; ++i)
    {
        sprintf(tmp,"%2.2x",hashResults[k][i]);
        strcat(hash[k],tmp);
    }
    printf("%d \n%s\n", strlen(hash[k]), hash[k]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM