为什么这个 SHA256 函数会打印一些奇怪的字符?

发布于 2024-11-11 16:40:13 字数 1727 浏览 2 评论 0原文

这是代码

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <byteswap.h>
#include "/usr/include/openssl/sha.h"
#include <evhttp.h>

bool hex2bin(unsigned char *p, const char *hexstr, size_t len);

bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
{
while (*hexstr && len) {
    char hex_byte[3];
    unsigned int v;

    if (!hexstr[1]) {
        //applog(LOG_ERR, "hex2bin str truncated");
        return false;
    }

    hex_byte[0] = hexstr[0];
    hex_byte[1] = hexstr[1];
    hex_byte[2] = 0;

    if (sscanf(hex_byte, "%x", &v) != 1) {
        //applog(LOG_ERR, "hex2bin sscanf '%s' failed",
            //hex_byte);
        return false;
    }

    *p = (unsigned char) v;

    p++;
    hexstr += 2;
    len--;
}

return (len == 0 && *hexstr == 0) ? true : false;
}

int main(int argc, char **argv)
{
unsigned char hash[SHA256_DIGEST_LENGTH], hash1[SHA256_DIGEST_LENGTH];
uint32_t *hash32 = (uint32_t *) hash;
unsigned char data[128];
uint32_t *data32 = (uint32_t *) data;
int i;

   hex2bin(data, argv[1], sizeof(data));

for (i = 0; i < 128/4; i++)
    data32[i] = bswap_32(data32[i]);

SHA256(data, 80, hash1);
SHA256(hash1, SHA256_DIGEST_LENGTH, hash);

printf("%s\n\n",hash1);

return 0;   
}

抱歉代码太长。另外,不要介意不必要的包含,我只是不确定需要哪些,所以我无论如何都编译了。

这是用 C 编写的(显然),在 Linux、Ubuntu 11.04 下运行。

该程序将某个字符串作为参数,并且必须计算它的 sha256 哈希值(两次,同时在实际对其进行哈希处理之前执行各种操作)。 尽管程序编译、运行并获取字符串,但它会输出一些奇怪的字符而不是哈希值。 为什么会这样呢?

聚苯乙烯 这段代码不是我的,我只是把它变成了一个单独的程序,但就像我说的,打印哈希失败并出现一些奇怪的字符!

P.S2 SHA256 在 LibSSL 的 libcrypto 库中定义。该程序是使用 gcc 使用 -lcrypto 参数进行编译的。

This is the code

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <byteswap.h>
#include "/usr/include/openssl/sha.h"
#include <evhttp.h>

bool hex2bin(unsigned char *p, const char *hexstr, size_t len);

bool hex2bin(unsigned char *p, const char *hexstr, size_t len)
{
while (*hexstr && len) {
    char hex_byte[3];
    unsigned int v;

    if (!hexstr[1]) {
        //applog(LOG_ERR, "hex2bin str truncated");
        return false;
    }

    hex_byte[0] = hexstr[0];
    hex_byte[1] = hexstr[1];
    hex_byte[2] = 0;

    if (sscanf(hex_byte, "%x", &v) != 1) {
        //applog(LOG_ERR, "hex2bin sscanf '%s' failed",
            //hex_byte);
        return false;
    }

    *p = (unsigned char) v;

    p++;
    hexstr += 2;
    len--;
}

return (len == 0 && *hexstr == 0) ? true : false;
}

int main(int argc, char **argv)
{
unsigned char hash[SHA256_DIGEST_LENGTH], hash1[SHA256_DIGEST_LENGTH];
uint32_t *hash32 = (uint32_t *) hash;
unsigned char data[128];
uint32_t *data32 = (uint32_t *) data;
int i;

   hex2bin(data, argv[1], sizeof(data));

for (i = 0; i < 128/4; i++)
    data32[i] = bswap_32(data32[i]);

SHA256(data, 80, hash1);
SHA256(hash1, SHA256_DIGEST_LENGTH, hash);

printf("%s\n\n",hash1);

return 0;   
}

Sorry for the long code. Also, don't mind the unnecessary includes, i just wasn't sure which were needed so i compiled regardless.

This is written in C(obviously) running under Linux, Ubuntu 11.04.

This program takes as an argument some string and must compute it's sha256 hash(twice, while doing various stuff before actually hashing it).
Although the program compiles,runs and takes the string, it spews some weird characters instead of a hash.
Why so?

P.S
This code is not mine, i just made it into a separate program for myself, but like i said, printing the hash fails with some weird characters!

P.S2
SHA256 is defined in the LibSSL's libcrypto library. The program is compiled with the -lcrypto argument using gcc.

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

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

发布评论

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

评论(1

梦里的微风 2024-11-18 16:40:13

哈希的输出几乎总是不透明的二进制数据 - 即任何字节。

您正在尝试将它们打印为文本。这意味着它将对二进制数据应用一些编码,尝试将其解释为文本。

基本上,您应该使用与 hex2bin 相反的方式将任意二进制数据转换为十六进制。

The output of a hash is almost always opaque binary data - i.e. any bytes.

You're trying to print those as if they were text. That means it'll be applying some encoding to the binary data, trying to interpret it as text.

Basically you should use the opposite of hex2bin in order to convert the arbitrary binary data into hex.

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