Search code examples
c++arduinoshahmacarduino-ide

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


I installed this SHA library: https://github.com/Cathedrow/Cryptosuite. I want to implement HMAC256 using Arduino IDE 1.6.7 installed on Win. 10 and the controller is ATMEGA328.

I copied the example given in their webpage. I am still new and want to test and try. I wrote this code in 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:
}

I got this error:

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

I do not know why this happens. The example is primitive taken as is from the library site. Can you help?

EDIT: I tried to change the line from:

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

to:

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

But again, the compilation fails. It says:

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.


Solution

  • The initHmac function signature is:

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

    But you use const char* for secret.

    Solution

    Try to cast the secret variable to const uint8_t* (or const unsigned char*):

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

    UPDATE

    To solve your new compilation error, just add const in front of all declarations containing PROGMEM in the library sources. For insance:

    In Sha/sha1.cpp (line 11)

    const uint8_t sha1InitState[] PROGMEM = {
    

    In Sha/sha256.cpp (line 6)

    const uint32_t sha256K[] PROGMEM = {
    

    In Sha/sha256.cpp (line 11):

    const uint8_t sha256InitState[] PROGMEM = {