Search code examples
arduinohmacsha1

SHA1 HMAC a byte array with Arduino


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?


Solution

  • 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]);
     }
    }