2016-01-16 572 views
2

我安裝了此SHA庫:https://github.com/Cathedrow/Cryptosuite。我想使用安裝在Win上的Arduino IDE 1.6.7來實現HMAC256。 10和控制器是ATMEGA328。如何解決此問題:從'const char *'無效轉換爲'const uint8_t *

我複製了他們的網頁中給出的例子。我還是新手,想要測試和嘗試。我在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.

回答

4

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); 

UPDATE

爲了解決新的編譯錯誤,只是添加const在前面所有包含PROGMEM的聲明在庫源中。對於insance:

沙/ sha1.cpp(第11行)

const uint8_t sha1InitState[] PROGMEM = { 

沙/ sha256.cpp(第6行)

const uint32_t sha256K[] PROGMEM = { 

沙/ SHA256 .cpp(line 11)

const uint8_t sha256InitState[] PROGMEM = { 
+1

謝謝。但沒有解決問題。查看已編輯的帖子。 – user2192774

+2

查看更新(這是另一個與您的初始錯誤無關的問題) – jyvet

+0

解決了問題:) –

相關問題