invalid char* conversion

I installed this SHA library: GitHub - Cathedrow/Cryptosuite: Cryptographic suite for Arduino (SHA, HMAC-SHA). I want to implement HMAC256. 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;
  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:

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

In file included from C:\Users\e\Documents\Arduino\codes\test_v1\test_v1.ino:1:0:

C:\Users\e\Documents\Arduino\libraries\Sha/sha256.h:26:18: error: conflicting return type specified for ‘virtual void Sha256Class::write(uint8_t)’

virtual void write(uint8_t);

^

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

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:224,

from sketch\test_v1.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:48:20: error: overriding ‘virtual size_t Print::write(uint8_t)’

virtual size_t write(uint8_t) = 0;

^

C:\Users\e\Documents\Arduino\codes\test_v1\test_v1.ino: In function ‘void setup()’:

test_v1:6: error: invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]

Sha256.initHmac(“hash key”,8); // key, and length of key in bytes

^

In file included from C:\Users\e\Documents\Arduino\codes\test_v1\test_v1.ino:1:0:

C:\Users\e\Documents\Arduino\libraries\Sha/sha256.h:23:10: error: initializing argument 1 of ‘void Sha256Class::initHmac(const uint8_t*, int)’ [-fpermissive]

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

^

test_v1:9: error: call of overloaded ‘print(uint8_t*&, int)’ is ambiguous

Serial.print(hash,HEX);

^

C:\Users\e\Documents\Arduino\codes\test_v1\test_v1.ino:9:24: note: candidates are:

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

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,

from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:224,

from sketch\test_v1.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:62:12: note: size_t Print::print(unsigned char, int)

size_t print(unsigned char, int = DEC);

^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:62:12: note: no known conversion for argument 1 from ‘uint8_t* {aka unsigned char*}’ to ‘unsigned char’

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:63:12: note: size_t Print::print(int, int)

size_t print(int, int = DEC);

^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:63:12: note: no known conversion for argument 1 from ‘uint8_t* {aka unsigned char*}’ to ‘int’

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:64:12: note: size_t Print::print(unsigned int, int)

size_t print(unsigned int, int = DEC);

^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:64:12: note: no known conversion for argument 1 from ‘uint8_t* {aka unsigned char*}’ to ‘unsigned int’

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:65:12: note: size_t Print::print(long int, int)

size_t print(long, int = DEC);

^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:65:12: note: no known conversion for argument 1 from ‘uint8_t* {aka unsigned char*}’ to ‘long int’

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:66:12: note: size_t Print::print(long unsigned int, int)

size_t print(unsigned long, int = DEC);

^

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:66:12: note: no known conversion for argument 1 from ‘uint8_t* {aka unsigned char*}’ to ‘long unsigned int’

exit status 1
invalid conversion from ‘const char*’ to ‘const uint8_t* {aka const unsigned char*}’ [-fpermissive]

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

I tried to replace the uint_t *hash by this type: static const char hash[450]={};
But again, got the same error.

Can you help me please.

That library is 6 years old, and it seems that the Arduino Stream or Print class has been updated since. Perhaps the rest of the errors is the result of the first error. The "Sha1Class::write" and "Sha256Class::write" should be altered to return the number of written bytes. I'm not sure how to do that, the slightest mistake could make the encryption useless.

Do you know any better library?

No, I'm sorry. There are many SHA256 arduino libraries on Github, but I don't know which one is okay. I tried to find a recent project with SHA256, but I could not find it.

The problem you have is already reported : https://github.com/Cathedrow/Cryptosuite/issues and the project is forked 58 times ! One of those forkes should probably work.

The subject of this topic is not what you are looking for. You could start a new topic, for example: "SHA256 for Arduino Uno needed", or you can modify your top post of this topic and change the subject. If you start a new topic, insert a link to this one.

The changes to the library should actually be harmless. Changing a return type void to a return type size_t does not affect anything.

sha256.h

    // 20160116; changed void to size_t
    virtual size_t write(uint8_t);

sha256.cpp; match the function/method to the prototype

// 20160116; changed void to size_t
size_t Sha256Class::write(uint8_t data) {
  ++byteCount;
  addUncounted(data);
}

The C/CPP compiler is quite forgiving for the fact that the last change above does not return an size_t value. It should probably return 1 as one byte is written.

Next the compiler will complain about the PROGMEM lines in sha256.cpp. I'm quite close to 100% confident that the following changes in sha256.cpp don't effect the operation of the library

// 20160116; added const
const uint32_t sha256K[] PROGMEM = {
...
...
// 20160116; added const
const uint8_t sha256InitState[] PROGMEM = {
...
...

After which there was only an error remaining in your setup() function

void setup() {
  Serial.begin(9600);

  uint8_t *hash;
  Sha256.initHmac((uint8_t*)"hash key", 8); // key, and length of key in bytes
  Sha256.print("This is a message to hash");
  hash = Sha256.resultHmac();
  Serial.write(hash, HASH_LENGTH);
}

The result is code that at least compiles. I do not guarantee the result but, as said, I'm quite confident that the changes don't effect anything with regards to result. Personally I would write / use a Windows/Linux program to verify results to make 100% sure.

Note: I only copied sha256.cpp and sha256.h to the project directory, not the full library.