lucadentella / TOTP-Arduino

152 stars 33 forks source link

Unable to get correct code #21

Closed SudoerWithAnOpinion closed 2 years ago

SudoerWithAnOpinion commented 2 years ago

I've tried looking at past issues and the example code but I cannot get Google Authenticator and the sketch to agree. I am able to use the totp secret in MS authenticator, Google Authenticator, and other places and they all agree (as well as actually working for an account I was testing; this was removed from the account before posting for security reasons)

I thought the issue might be upper vs lowercasing but that seems unhelpful.

The example provided is great, but without including the original secret, it's very hard to verify if I've done something wrong. I've included an abridged copy of my code, laving out the WiFi and NTP details as I've already confirmed the time being output is correct.

  // TOTP keys were converted using http://www.lucadentella.it/OTP/ and the "Arduino HEX array" used.
  // TOTP KEY: hnt6rjlskdnglyzc
  uint8_t hardHMAC_L[] = {0x68, 0x6e, 0x74, 0x36, 0x72, 0x6a, 0x6c, 0x73, 0x6b, 0x64, 0x6e, 0x67, 0x6c, 0x79, 0x7a, 0x63};
  // TOTP KEY: HNT6RJLSKDNGLYZC
  uint8_t hardHMAC_U[] = {0x48, 0x4e, 0x54, 0x36, 0x52, 0x4a, 0x4c, 0x53, 0x4b, 0x44, 0x4e, 0x47, 0x4c, 0x59, 0x5a, 0x43};

  long currentTime = timeClient.getEpochTime();
  Serial.println("Current Time: " + timeClient.getFormattedTime());

  TOTP totpClient_L(hardHMAC_L,16);
  String totpCode_L = totpClient_L.getCode(currentTime); 
  Serial.println("TOTP Code (hnt6rjlskdnglyzc): " + totpCode_L);

  TOTP totpClient_U(hardHMAC_U,16);
  String totpCode_U = totpClient_U.getCode(currentTime); 
  Serial.println("TOTP Code (HNT6RJLSKDNGLYZC): " + totpCode_U);

These are the outputs I get: Google Authenticator TOTP Code: 841238 (Arduino) TOTP Code (hnt6rjlskdnglyzc): 976485 (Arduino) TOTP Code (HNT6RJLSKDNGLYZC): 415344

Do you have any pointer as to what I may be doing wrong? Any help would be appreciated.

lucadentella commented 2 years ago

Hi

most of the application requires a base32 encoded version of the key...

For example I was able to test your secret key (lowercase) with the following website: https://totp.danhersam.com/?key=NBXHINTSNJWHG23ENZTWY6L2MM

My arduino agrees with the codes generated by the website, NBX... is the encoded version of "hnt6rjlskdnglyzc"

Let me know...

lucadentella commented 2 years ago

I also tested the GAuth app with the base32 version of the key as "configuration code" and it works fine

SudoerWithAnOpinion commented 2 years ago

Can you walk me through what you do with hnt6rjlskdnglyzc to make it work? I put it into your site to convert to the HEX array and used it as the TOTP's input but it didn't work.

lucadentella commented 2 years ago

Hi

I simply copied your definition:

uint8_t hardHMAC_L[] = {0x68, 0x6e, 0x74, 0x36, 0x72, 0x6a, 0x6c, 0x73, 0x6b, 0x64, 0x6e, 0x67, 0x6c, 0x79, 0x7a, 0x63};

in my example and run the sketch