如何解决这个问题:从 'const char*' 到 'const uint8_t* 的无效转换

How to resolve this: invalid conversion from 'const char*' to 'const uint8_t*

我安装了这个 SHA 库:https://github.com/Cathedrow/Cryptosuite。我想使用安装在 Win 上的 Arduino IDE 1.6.7 来实现 HMAC256。 10,控制器是ATMEGA328。

我复制了他们网页上给出的例子。我还是新手,想测试和尝试。我在 Arduino IDE 中编写了这段代码。

#include "sha256.h"

void setup() {
    // put your setup code here, to run once:
    uint8_t *hash;
    //static const char hash[450]={};
    //const char *hash; hash={};
    Sha256.initHmac("hash key",8); // key, and length of key in bytes
    Sha256.print("This is a message to hash");
    hash = Sha256.resultHmac();
    //Serial.print(hash,HEX);
}

void loop() {
    // put your main code here, to run repeatedly:
}

我收到这个错误:

invalid conversion from 'const char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

我不知道为什么会这样。该示例是原始的,原样来自图书馆站点。你能帮忙吗?

编辑: 我试图改变行:

Sha256.initHmac((const uint8_t*)"hash key",8);

至:

Sha256.initHmac((const uint8_t*)"hash key",8);

但是编译还是失败了。它说:

Arduino: 1.6.7 (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/arduino.h:28:0,

             from C:\Users\e\Documents\Arduino\libraries\Sha\sha1_config.h:13,

             from C:\Users\e\Documents\Arduino\libraries\Sha\sha1.h:4,

             from C:\Users\e\Documents\Arduino\libraries\Sha\sha1.cpp:1:

C:\Users\e\Documents\Arduino\libraries\Sha\sha1.cpp:8:25: error: variable 'sha1InitState' must be const in order to be put into read-only section by means of 'attribute((progmem))'

uint8_t sha1InitState[] PROGMEM = {

                     ^

exit status 1 Error compiling.

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

initHmac 函数签名是:

void initHmac(const uint8_t* secret, int secretLength);

但是你使用 const char* 作为秘密。

解决方案

尝试将秘密变量转换为const uint8_t*(或const unsigned char*):

Sha256.initHmac((const uint8_t*)"hash key",8);

更新

要解决您的新编译错误,只需在库源中所有包含 PROGMEM 的声明前添加 const。例如:

Sha/sha1.cpp(第11行)

const uint8_t sha1InitState[] PROGMEM = {

Sha/sha256.cpp(第6行)

const uint32_t sha256K[] PROGMEM = {

Sha/sha256.cpp(第11行):

const uint8_t sha256InitState[] PROGMEM = {