简体   繁体   English

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

[英]Why do they want an 'unsigned char*' and not just a normal string or 'char*'

EDIT: After taking adivce I have rearranged the parameters & types. 编辑:分开后,我已经重新排列了参数和类型。 But the application crashes when I call the digest() function now? 但是,当我现在调用digest()函数时,应用程序崩溃了吗? 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++. 我在C ++中使用SHA256算法的开源实现。 My problem is understanding how to pass a unsigned char* version of my string so it can be hashed? 我的问题是了解如何传递字符串的未签名char *版本,以便对其进行哈希处理?

This is the function that takes a unsigned char* version of my string: 这是采用我字符串的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);
}

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. PS:我一直在研究SHA256的许多实现,它们都将未签名的char *作为要散列的消息。 Why do they do that? 他们为什么这样做? Why not a char* or a string instead? 为什么不使用char *或字符串呢?

You have the parameters around the wrong way. 您使用错误的方式设置了参数。 Buf is the input (data to be hashed) and dig is the output digest ( the hash). Buf是输入(要散列的数据),dig是输出摘要(散列)。

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. 通常,人们选择为此使用十六进制字符串。

The reason that unsigned char is used is that it has guaranteed behaviours under bitwise operations, shifts, and overflow. 使用unsigned char的原因是在按位操作,移位和溢出情况下,它具有保证的行为。

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. char ,(当它对应于带signed char )不提供任何保证,因此对于打算直接作用于字符串中基础位的操作而言,它的用处远不及此保证。


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. 简而言之,您正在编写从未初始化为指向任何内存的指针hashMessage 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 char s long. 对正在使用的库的源代码进行的简短调查显示,它要求digest指针指向至少SHA256_DIGEST_SIZE char长的有效内存块。

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. 要解决此问题,您需要做的就是确保正确初始化作为digest参数( hashMessage )传递的指针,并指向足够大小的内存块。 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.

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. 我不知道SHA256散列是如何产生的,但可能涉及某种需要对无符号数据类型进行的算术运算。

Why does it matter? 为什么这有关系? Get a char* from your string object by calling the c_str() method then cast to unsigned char* . 通过调用c_str()方法从字符串对象中获取char* ,然后转换为unsigned char*

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

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