pythonandroidioscryptographygoogle-authenticator

How to avoid '=' in the output of b32encode.decode() in Python?



I'm generating a secret that is to be used to generate a QR Code for Google Authenticator.

secret = b32encode(os.urandom(16)).decode()

This secret contains '=' symbol that is not recognized by Google Authenticator in an iPhone. Works fine with android though.

How to avoid '=' symbol while generating the secret? Any help or direction is appreciated.


Solution

  • Thank you for the directions.

    So, os.urandom(16) produces 16 bytes of data. i.e 16 x 8 = 128 bits. b32encode divides this data in groups of 5 bits each bit having 1 or 0, thus totalling to 32 values.

    The data produced by os.urandom(16) i.e 128 bits of data is grouped this way. 25x5 bits and 1x3 bits. Now in order to indicate that there is free space, i.e 2 bits, an appropriate padding is used with '=' signs.

    Inorder to remove the '=' signs, the number of bits being provided to b32encode should be in multiples of 5. So, I changed os.urandom(16) to os.urandom(20) and got the result as per my expectation.