密码哈希到字符串哈希在C

时间:2021-06-14 06:20:02

How can one create a simple password hashing function in C? I know that there is a standard library available, crypt.h, and also openssl/sha.h But that does not produce a string. I have tried different ways to print the sha256 string, but the string is not the same as other sha256 string of the same word.

如何在C中创建一个简单的密码哈希函数?我知道有一个标准的图书馆,crypt。h,openssl /沙。但是这不会产生弦。我尝试了不同的方法来打印sha256字符串,但是这个字符串与其他相同单词的sha256字符串不同。

The code for hashing into sha256 I have found in a topic on this website:

我在这个网站上的一个主题中找到了对sha256进行哈希的代码:

char input[] = "hello";
int length = sizeof(input);
SHA256_CTX context;
unsigned char md[SHA256_DIGEST_LENGTH];
SHA256_Init(&context);
SHA256_Update(&context, (unsigned char *)input, length);
SHA256_Final(md, &context);

printf("%02x\n", md); // every time different value: c94ce410, 46d384c0 ..
printf("sizeof md = %zu\n", sizeof(md));
int i;
for(i = 0; i <= sizeof(md); i++) {
    printf("%02x", md[i]); // not a sha256..
    printf("%u", md[i]); // only numeric, not correct..
}
printf("\n");

The string that it produces is: f3aefe62965a91903610f0e23cc8a69d5b87cea6d28e75489b0d2ca02ed7993c62

它产生的字符串是:f3aefe62965a91903610f0e23cc8a69d5b87cea6d28e75489b0d2ca02ed7993c62

But that is not a sha256 string for hello, because it is not recognized by online decryption services. I am using #include <openssl/sha.h> for this one.

但是这并不是一个用于hello的sha256字符串,因为在线解密服务无法识别它。我正在使用#include 这一个。

Edit:

编辑:

Correct settings:

正确设置:

int length = strlen(input);
int i;
    for(i = 0; i < sizeof(md); i++) {
        printf("%0x", md[i]);
        //printf("%u", md[i]);
    }
    printf("\n");

Now a correct sha256 hash string is produced.

现在生成了一个正确的sha256散列字符串。

1 个解决方案

#1


0  

Your length is wrong. Typedef return the length of a type, here a char array witch is ended by \0. You need to use strlen instead.

你的长度是错误的。Typedef返回类型的长度,这里的char数组女巫以\0结束。你需要使用strlen代替。

#1


0  

Your length is wrong. Typedef return the length of a type, here a char array witch is ended by \0. You need to use strlen instead.

你的长度是错误的。Typedef返回类型的长度,这里的char数组女巫以\0结束。你需要使用strlen代替。