SHA1 HMAC 与 Arduino 的字节数组

发布于 2024-12-25 04:10:44 字数 586 浏览 1 评论 0原文

如何在 Arduino 上 HMAC 字节数组?我找到了用于 SHA1 HMAC 的此库,但它似乎仅用于字符串。

我已经以空终止字节数组的形式向它传递了字节。这确实给了我正确的结果。但对于包含零的字节数组来说效果不太好!

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 库来做我想做的事情。

有谁知道另一种方法吗?

How can I HMAC a byte array on an Arduino? I've found this library for SHA1 HMACs, but it appears to be used only for strings.

I've passed it bytes in a null-terminated byte array. This does give me the correct result. But doesn't work so well for byte arrays that contain zeros!

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

Either I need to find another another library (crypto-arduino-library looks promising, but doesn't include any examples for what I'm doing), or hack up the Cathedrow library to do what I'm after.

Does anyone know of another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪#恶を代价 2025-01-01 04:10:44

添加我自己的方法似乎已经成功了:

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

Adding my own method appears to have done the trick:

void Sha1Class::writebytes(const uint8_t* data, int length) {
 for (int i=0; i<length; i++)
 {
   write(data[i]);
 }
}
苏别ゝ 2025-01-01 04:10:44

如果您不想更改 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() { }

输出

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

If you don't want to change sha1.cpp you can just loop and print each single byte, the trick is to use Sha1.print((char) basestring[i]);, like so:

#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() { }

Output

Input:              testing
Key:                testKey
Hmac-sha1 (hex):    60d41271d43b875b791e2d54c34bf3f018a29763
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文