SHA1 HMAC一个带有Arduino的字节数组
如何在Arduino上创建HMAC字节数组?我找到了SHA1 HMAC,但它似乎只用于字符串 我在一个以null结尾的字节数组中传递了个字节。这确实给了我正确的结果。但是,对于包含零的字节数组来说,效果不是很好SHA1 HMAC一个带有Arduino的字节数组,arduino,hmacsha1,Arduino,Hmacsha1,如何在Arduino上创建HMAC字节数组?我找到了SHA1 HMAC,但它似乎只用于字符串 我在一个以null结尾的字节数组中传递了个字节。这确实给了我正确的结果。但是,对于包含零的字节数组来说,效果不是很好 uint8_t hmacKey1[]={ 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0xde, 0xad, 0xbe, 0xef }; uint8_t time[]={ 0xb2, 0x00 }; Sha1.initHmac(hmacKey1, 1
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);
要么我需要另找一个图书馆(看起来很有希望,但没有任何我正在做的例子),要么我要把Cathedrow图书馆改造成我想要的
有人知道另一种方法吗?添加我自己的方法似乎成功了:
void Sha1Class::writebytes(const uint8_t* data, int length) {
for (int i=0; i<length; i++)
{
write(data[i]);
}
}
void Sha1Class::writebytes(常量uint8\u t*数据,整数长度){
对于(int i=0;i如果不想更改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("0123456789abcdef"[hash[i]>>4]);
Serial.print("0123456789abcdef"[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() { }
IIRC基类Print
提供了这样的方法。否则只需使用循环和Print::write
@leppie是的,循环帮了我的忙,很高兴我能帮忙:)很久没有为Arduino编码了。
Input: testing
Key: testKey
Hmac-sha1 (hex): 60d41271d43b875b791e2d54c34bf3f018a29763