Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key conversion from Base32 to UTF-8/Hex #26

Open
Meridien opened this issue Jan 13, 2025 · 0 comments
Open

Key conversion from Base32 to UTF-8/Hex #26

Meridien opened this issue Jan 13, 2025 · 0 comments

Comments

@Meridien
Copy link

Meridien commented Jan 13, 2025

Hi @lucadentella

I was able to successfully use your library, thanks for making this in the first place!

One thing I found challenging was translating my existing TOTP keys from the more common Base32 key into the hex values needed in the sketch (FullExample_WiFi_NTP_OTP.ino).

// enter your hmacKey (10 digits)
uint8_t hmacKey[] = {0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x6b, 0x65, 0x79, 0x30};

I used python to do the translation.

import base64

base32_key = input("Enter the Base32-encoded TOTP key: ")

try:
    # Decode the Base32 string into raw binary data
    binary_data = base64.b32decode(base32_key, casefold=True)

    # Convert binary data into a UTF-8 string
    UTF8_STRING = binary_data.decode('utf-8')

    print("UTF-8 string:", UTF8_STRING)

except (base64.binascii.Error, UnicodeDecodeError) as e:
    print("Error: Unable to decode. The input may not be valid Base32 or UTF-8 encoded.")

print('////////////Copy below to ino//////////////')
print("uint8_t hmacKey[] = {", ", ".join(hex(ord(char)) for char in UTF8_STRING),'};')
print("TOTP totp = TOTP(hmacKey, ",len(UTF8_STRING),");")

I'd like to submit this as as a pull request if you are open to that.

Cheers,
Daniel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant