为什么输入相同的值时,其哈希值会不同?在C中

发布于 2025-01-17 20:27:36 字数 1404 浏览 7 评论 0原文

我正在使用 SHA1 来加密我的 ID。

但是,即使我输入相同的 ID,它的哈希值也会不同。

这是我的代码:

#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

char *sha1_hash(char *input_url, char *hashed_url) {
    unsigned char hashed_160bits[20];
    char hashed_hex[41];
    int i;
    
    SHA1(input_url, 160, hashed_160bits);

    for(i=0; i < sizeof(hashed_160bits); i++) {
        sprintf(hashed_hex + i*2, "%02x", hashed_160bits[i]);
    }        

    strcpy(hashed_url, hashed_hex);

    return hashed_url;
}

int main()
{   
    char *input_url;
    char *hashed_url;
    
    while(1) {
        input_url = malloc(sizeof(char)* 1024);
        hashed_url = malloc(sizeof(char) * 1024);
        
        printf("input url> ");
        scanf("%s", input_url);
        
                if (strcmp(input_url, "bye") == 0) {
                        free(hashed_url);
                        free(input_url);
                        break;
                }

        sha1_hash(input_url, hashed_url);
    
        printf("hashed_url: %s\n", hashed_url);
        free(hashed_url);
        free(input_url);
        }

    return 0;
}

如果我为第一次尝试和第二次尝试输入相同的值,则会以不同的方式对其进行哈希处理,但第三次尝试将与第二次尝试进行相同的哈希处理。

我认为动态分配是一个问题,但我想不出解决办法。

I am using SHA1 to encrypt my ID.

However, even if I enter the same ID, it is hashed differently.

This is my code:

#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

char *sha1_hash(char *input_url, char *hashed_url) {
    unsigned char hashed_160bits[20];
    char hashed_hex[41];
    int i;
    
    SHA1(input_url, 160, hashed_160bits);

    for(i=0; i < sizeof(hashed_160bits); i++) {
        sprintf(hashed_hex + i*2, "%02x", hashed_160bits[i]);
    }        

    strcpy(hashed_url, hashed_hex);

    return hashed_url;
}

int main()
{   
    char *input_url;
    char *hashed_url;
    
    while(1) {
        input_url = malloc(sizeof(char)* 1024);
        hashed_url = malloc(sizeof(char) * 1024);
        
        printf("input url> ");
        scanf("%s", input_url);
        
                if (strcmp(input_url, "bye") == 0) {
                        free(hashed_url);
                        free(input_url);
                        break;
                }

        sha1_hash(input_url, hashed_url);
    
        printf("hashed_url: %s\n", hashed_url);
        free(hashed_url);
        free(input_url);
        }

    return 0;
}

If I enter the same value for the first attempt and the second attempt, it will be hashed differently, but the third attempt will be hashed the same as the second attempt.

I think the dynamic allocation is a problem, but I can not think of a way to fix it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

真心难拥有 2025-01-24 20:27:36

您没有正确调用 SHA1

SHA1(input_ID, 160, hashed_ID_160bits);

第二个参数是要哈希的数据的长度。相反,您要传递散列中的位数。因此,您将把 input_ID 中包含的字符串的末尾读入未初始化的内存中,并且可能会超出已分配内存段的末尾。这会触发未定义行为

相反,你想要:

SHA1(input_ID, strlen(input_ID), hashed_ID_160bits);

You're not calling SHA1 correctly:

SHA1(input_ID, 160, hashed_ID_160bits);

The second parameter is the length of the data to hash. You're instead passing in the number of bits in the hash. As a result, you're reading past the end of the string contained in input_ID into uninitialized memory and possibly past the end of the allocated memory segment. This triggers undefined behavior.

You instead want:

SHA1(input_ID, strlen(input_ID), hashed_ID_160bits);
九命猫 2025-01-24 20:27:36
SHA1(input_ID, 160, hashed_ID_160bits);

那条线是错误的。您始终会获得 160 字节的哈希值。我假设您只需要输入文本的哈希值,因此请使用该长度:

SHA1(input_ID, strlen(input_ID), hashed_ID_160bits);

SHA1 始终生成 160 位的哈希值,因此您不需要传递 160 作为参数。如果您想要不同大小的 SHA 哈希值,则需要使用不同的函数, 记录在此处,然后当然要修改其余代码以匹配该哈希大小。


为什么在不同时间得到不同的哈希值是因为访问了 malloc 缓冲区的未初始化部分。这是未定义的行为,因此“任何事情”都可能发生,尝试弄清楚到底发生了什么通常没有用,因为它不一定是非常确定的。如果您想更深入地挖掘,您可以使用调试器来检查不同循环迭代中的内存地址和内容,以查看到底发生了什么变化。不过,由于这是未定义的行为,因此当您尝试在调试器下运行代码或添加调试打印时,不良代码的行为不同是众所周知的。

SHA1(input_ID, 160, hashed_ID_160bits);

That line is wrong. You are always getting hash for 160 bytes. I assume you want the hash for the input text only, so use that length:

SHA1(input_ID, strlen(input_ID), hashed_ID_160bits);

SHA1 always produces hash of 160 bits, so you do not need to pass 160 as a parameter. If you want different size of SHA hash, you need to use a different function, documented here, and then of course modify rest of the code to match that hash size.


Why you get different hashes at different times is because of accessing uninitialized part of malloc buffer. This is Undefined Behavior, so "anything" can happen, and it's not generally useful to try and figure out what exactly happens, because it's not necessarily very deterministic. If you want to dig deeper than that, you could for example use a debugger to examine the memory addresses and contents on different loop iterations to see what exactly changed. Though, since this is Undefined Behavior, it's notoriously common for bad code to behave differently when you try to run it under debugger, or add debug prints.

三人与歌 2025-01-24 20:27:36

问题似乎出在您正在分配的未初始化内存中。

malloc 为您保留内存,但内容是“以前存在过的内容”。由于您不仅对字符串内容进行哈希处理,而且对整个缓冲区进行哈希处理,因此每次都会得到不同的结果。

尝试使用 calloc、在缓冲区上运行 memset 或将散列限制为 strlen(input) 并查看是否有帮助。

The problem seems to be in the uninitialized memory you are allocating.

malloc reserves memory for you, but the contents are 'whatever has been in there before'. And since you are not only hashing the string contents, but the entire buffer, you get different results each time.

Try using calloc, running memset over the buffer or limit your hashing to strlen(input) and see if that helps.

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