Skip to content

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

@Meridien

Description

@Meridien

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions