lucadentella / TOTP-Arduino

152 stars 33 forks source link

Generate hex array #16

Closed bert2002 closed 3 years ago

bert2002 commented 3 years ago

Hi, I managed to get the library working, but only when I generate the hex array outside of Arduino and hard code it in the code. Do you have a way to generate it dynamically from the secret string or even the base32 string? Tried to find a way, but somehow I am failing. E.g.:

char msgArray[] = "SuperLongSecret";
   uint8_t hmacKey[] = {};
   Serial.println(msgArray);
  for (int i = 0; i<sizeof(msgArray)-1; i++)
  {
    Serial.print(msgArray[i], HEX);
    Serial.printf("\n");
    String NewCharacter = String(msgArray[i],HEX);
    hmacKey[i] = NewCharacter;
    Serial.println(hmacKey[i]);
  }

Of course this is failing with error: cannot convert 'String' to 'uint8_t {aka unsigned char}' in assignment.

Any idea how to achieve this?

bert

lucadentella commented 3 years ago

Hi Bert! Not sure about your request, the hex array is the secret string... if you give a look to my example:

// The shared secret is MyLegoDoor
uint8_t hmacKey[] = {0x4d, 0x79, 0x4c, 0x65, 0x67, 0x6f, 0x44, 0x6f, 0x6f, 0x72};

and you convert the hex values with the ASCII table you'll find that they are exactly the letters "MyLegoDoor":

0x4d = M
0x79 = y
0x4c = L
...

Could you please explain better your request? thanks

bert2002 commented 3 years ago

Exactly trying that, but havent found a way to generate that uint8_t hex array yet. My plan is to get the secret string from a sqlite and then convert it to the hex array.

lucadentella commented 3 years ago

can't test now, but if you're working with String objects, you could use the toCharArray method: https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/tochararray/

this compiles, give it a try and let me know

void setup() {

  char hmacKey[11];
  String myPassword = "MySecretPwd";
  myPassword.toCharArray(hmacKey, 11);
  TOTP totp = TOTP(hmacKey, 11);
} 
bert2002 commented 3 years ago

May be something is wrong with my setup, I tried your example before (I think) and now as well:

/home/user/Arduino/sketch_nov26a/sketch_nov26a.ino: In function 'void setup()':
sketch_nov26a:8:31: error: invalid conversion from 'char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
   TOTP totp = TOTP(hmacKey, 11);
                               ^
In file included from /home/user/Arduino/sketch_nov26a/sketch_nov26a.ino:1:0:
/home/user/Arduino/libraries/TOTP-Arduino/src/TOTP.h:16:3: note:   initializing argument 1 of 'TOTP::TOTP(uint8_t*, int)'
   TOTP(uint8_t* hmacKey, int keyLength);
   ^
exit status 1
invalid conversion from 'char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]

I just cloned your library again to see if I was running something old, but the same. Using Arduino 1.8.12.

lucadentella commented 3 years ago

strange, anyway try an explicit cast from char to unit8_t, it should not complain

bert2002 commented 3 years ago

Okay, if I understand correctly I tried (with ntp working), but it gives me a wrong code back when I compare it to a website.

void setup() {
  char hmacKey[11];
  String myPassword = "MySecretPwd";
  myPassword.toCharArray(hmacKey, 11);
  TOTP totp = TOTP((uint8_t*)hmacKey, 11);
} 

Just want to check if thats what you mean?

lucadentella commented 3 years ago

Yes your code is correct but I found a weird behavior of the "toCharArray" method (never used String objects before). It seems it adds a string terminator as last char of the array, so you need an additional char:

void setup() {
  char hmacKey[12];
  String myPassword = "MySecretPwd";
  myPassword.toCharArray(hmacKey, 12);
  TOTP totp = TOTP((uint8_t*)hmacKey, 11);
} 

I tested the full example included in the library with the code above and the following website and it works https://totp.danhersam.com/?key=JV4VGZLDOJSXIUDXMQ======

bert2002 commented 3 years ago

WOW. That works indeed. Thanks a lot. I will close this one, but how did you figure out that weird char?

lucadentella commented 3 years ago

how did you figure out that weird char?

if you print out all the characters when you call this method:

myPassword.toCharArray(hmacKey, 11);

you'll get "MySecretPw" followed by the NULL char... so the method gets 10 chars from the original string and add \0 as the last char in the resulting one