为什么他们想要一个“unsigned char*”?而不仅仅是普通的字符串或“char*”

发布于 2024-12-08 06:56:21 字数 1236 浏览 0 评论 0原文

编辑:在听取建议后,我重新排列了参数和参数。类型。但是当我现在调用digest()函数时应用程序崩溃了?有什么想法出了什么问题吗?

const std::string message = "to be encrypted";
unsigned char* hashMessage;

SHA256::getInstance()->digest( message, hashMessage ); // crash occurs here, what am I doing wrong?
printf("AFTER: n"); //, hashMessage); // line never reached

我正在使用 C++ 中的 SHA256 算法的开源实现。我的问题是了解如何传递字符串的 unsigned char* 版本以便对其进行哈希处理?

这是采用字符串的 unsigned char* 版本的函数:

void SHA256::digest(const std::string &buf, unsigned char *dig) {
    init();
    update(reinterpret_cast<const unsigned char *>(buf.c_str()), static_cast<unsigned int>(buf.length()));
    final();
    digest(dig);
}

如何将我的字符串(我想要散列的)转换为 unsigned char*?

我编写的以下代码会导致运行时错误当我去打印字符串内容时:

const std::string hashOutput;
char message[] = "to be encrypted";

printf("BEFORE: %s bb\n", hashOutput.c_str());
SHA256::getInstance()->digest( hashOutput, reinterpret_cast<unsigned char *>(message) );
printf("AFTER: %s\n", hashOutput.c_str()); // CRASH occurs here

PS:我一直在研究 SHA256 和 SHA256 的许多实现。它们都采用 unsigned char* 作为要散列的消息。他们为什么这么做?为什么不使用 char* 或字符串来代替?

EDIT: After taking adivce I have rearranged the parameters & types. But the application crashes when I call the digest() function now? Any ideas whats going wrong?

const std::string message = "to be encrypted";
unsigned char* hashMessage;

SHA256::getInstance()->digest( message, hashMessage ); // crash occurs here, what am I doing wrong?
printf("AFTER: n"); //, hashMessage); // line never reached

I am using an open source implementation of the SHA256 algorithm in C++. My problem is understanding how to pass a unsigned char* version of my string so it can be hashed?

This is the function that takes a unsigned char* version of my string:

void SHA256::digest(const std::string &buf, unsigned char *dig) {
    init();
    update(reinterpret_cast<const unsigned char *>(buf.c_str()), static_cast<unsigned int>(buf.length()));
    final();
    digest(dig);
}

How can I convert my string(which I want hashed) to an unsigned char*?

The following code I have made causes a runtime error when I go to print out the string contents:

const std::string hashOutput;
char message[] = "to be encrypted";

printf("BEFORE: %s bb\n", hashOutput.c_str());
SHA256::getInstance()->digest( hashOutput, reinterpret_cast<unsigned char *>(message) );
printf("AFTER: %s\n", hashOutput.c_str()); // CRASH occurs here

PS: I have been looking at many implementations of SHA256 & they all take an unsigned char* as the message to be hashed. Why do they do that? Why not a char* or a string instead?

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

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

发布评论

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

评论(3

小镇女孩 2024-12-15 06:56:21

您的参数设置错误。 Buf 是输入(要哈希的数据),dig 是输出摘要(哈希)。

此外,哈希是二进制数据。在将其打印到屏幕之前,您必须将所述二进制数据转换为某种字符串表示形式。通常,人们选择使用十六进制字符串来实现此目的。

You have the parameters around the wrong way. Buf is the input (data to be hashed) and dig is the output digest ( the hash).

Furthermore, a hash is binary data. You will have to convert said binary data into some string representation prior to printing it to screen. Normally, people choose to use a hexadecimal string for this.

予囚 2024-12-15 06:56:21

使用unsigned char的原因是它保证了按位运算、移位和溢出时的行为。

char(当它对应于signed char时)不提供任何这些保证,因此对于旨在直接作用于底层位的操作来说,它的用处要小得多。细绳。


问题的答案:“为什么会崩溃?”是“你很幸运!”。您的代码有未定义的行为。简而言之,您正在通过一个从未初始化为指向任何内存的指针 hashMessage 进行写入。对您正在使用的库的源代码进行简短调查后发现,它需要 digest 指针指向至少为 SHA256_DIGEST_SIZE 的有效内存块 char很长。

要解决此问题,您需要做的就是确保作为 digest 参数传入的指针 (hashMessage) 已正确初始化,并指向足够大小的内存块。在代码中:

const std::string message("to be encrypted");
unsigned char hashMessage[SHA256_DIGEST_SIZE];

SHA256::getInstance()->digest( message, hashMessage );

//hashMessage should now contain the hash of message.

The reason that unsigned char is used is that it has guaranteed behaviours under bitwise operations, shifts, and overflow.

char, (when it corresponds to signed char) does not give any of these guarantees, and so is far less useable for operations intended to act directly on the underlying bits in a string.


The answer to the question: "why does it crash?" is "you got lucky!". Your code has undefined behaviour. In short, you are writing through a pointer hashMessage that has never been initialised to point to any memory. A short investigation of the source code for the library that you are using reveals that it requires the digest pointer to point to a block of valid memory that is at least SHA256_DIGEST_SIZE chars long.

To fix this problem, all that you need to do is to make sure that the pointer that you pass in as the digest argument (hashMessage) is properly initialised, and points to a block of memory of sufficient size. In code:

const std::string message("to be encrypted");
unsigned char hashMessage[SHA256_DIGEST_SIZE];

SHA256::getInstance()->digest( message, hashMessage );

//hashMessage should now contain the hash of message.
聚集的泪 2024-12-15 06:56:21

我不知道 SHA256 哈希是如何生成的,但也许它涉及某种需要在无符号数据类型上完成的算术。

为什么这很重要?通过调用 c_str() 方法从字符串对象获取 char*,然后转换为 unsigned char*

I don't know how a SHA256 hash is produced but maybe it involves some sort of arithmetic that needs to be done on a unsigned data type.

Why does it matter? Get a char* from your string object by calling the c_str() method then cast to unsigned char*.

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