2012-01-07 147 views
3

我怎樣才能在Arduino上HMAC一個字節數組?我發現SHA1 HMAC的this library,但它似乎只用於字符串。SHA1 HMAC一個字節數組與Arduino

我已經將它傳遞給以空字節結尾的字節數組中的字節。這確實給了我正確的結果。但對於包含零的字節數組並不適用!

uint8_t hmacKey1[]={ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef }; 
uint8_t time[]={ 0xb2, 0x00 }; 

Sha1.initHmac(hmacKey1, 10); 
Sha1.print((char*)time); 

要麼我需要找到另一個另一個庫(crypto-arduino-library看起來很有希望,但不包括我在做什麼任何的例子),或劈了Cathedrow庫做什麼我之後。

有沒有人知道另一種方式?

+0

我IRC基類「Print」提供了這樣的方法。否則只是使用循環和'打印::寫' – leppie 2012-01-07 08:17:50

+0

@leppie是的,循環做了我的技巧thx – russau 2012-01-07 08:56:02

+0

很高興我可以幫助:)未經編碼的Arduino年齡。 – leppie 2012-01-07 09:18:22

回答

3

加入我自己的方法似乎做的伎倆:

void Sha1Class::writebytes(const uint8_t* data, int length) { 
for (int i=0; i<length; i++) 
{ 
    write(data[i]); 
} 
} 
1

如果你不想改變sha1.cpp你可以循環,並打印每個單字節,關鍵是要使用Sha1.print((char) basestring[i]);,像這樣:

#include <avr/pgmspace.h> 
#include <sha1.h> 

char key[] = "testKey"; 
uint8_t basestring[] = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67 }; // testing 

void printHash(uint8_t* hash) { 
    for (int i=0; i<20; i++) { 
    Serial.print("abcdef"[hash[i]>>4]); 
    Serial.print("abcdef"[hash[i]&0xf]); 
    } 
    Serial.println(); 
} 

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

    Serial.print("Input:    "); 
    for (int i=0; i<sizeof(basestring); i++) { 
    Serial.print((char) basestring[i]); 
    } 
    Serial.println(); 

    Serial.print("Key:    "); 
    Serial.println(key); 

    Serial.print("Hmac-sha1 (hex): "); 
    Sha1.initHmac((uint8_t*)key, strlen(key)); 

    for (int i=0; i<sizeof(basestring); i++) { 
    Sha1.print((char) basestring[i]); 
    } 

    uint8_t *hash; 
    hash = Sha1.resultHmac(); 
    printHash(hash); 

} 

void loop() { } 

輸出

Input:    testing 
Key:    testKey 
Hmac-sha1 (hex): 60d41271d43b875b791e2d54c34bf3f018a29763