2

I noted down the secret key from the platform where I am trying to signin and then scanned the code through Microsoft Authenticator Application.

Now I am trying to get the same otp using python libraries like pyotp, otpauth, onetimepassword like I have in the MS Authenticator. Until yesterday, pyotp was generating the correct otp for me. But now its not giving me the same otp.

OTP generated by MS Authenticator is working fine but the libraries are generating different OTP's and the secret is same for all of them.

Here is the code that I was using:

import pyotp
import time

totp = pyotp.TOTP('ABCDEFG')
otp  = totp.now()
print(otp)


Second one which I tried:

import otpauth

totp = otpauth.TOTP(b"ABCDEF")

# generate a code for now
code: int = totp.generate()

# you may want to convert it to string
str_code: str = totp.string_code(code)

print(str_code)
import onetimepass as otp 
secret_key = 'ABCDEFG' 
otp_code = otp.get_totp(secret_key) 
print(otp_code)

This code (last) is giving me this error: binascii.Error: Incorrect padding

I am getting different codes from both the libraries and Authenticator application is showing me something else. Is there anything I am missing out or Is there anything else which I can consider?

Shishank
  • 43
  • 5
  • check secret key and time synchronization , still getting error then you can also use onetimepassword – Hasan Raza Jul 21 '23 at 06:45
  • import onetimepassword as otp secret_key = 'ABCDEFG' otp_code = otp.get_totp(secret_key) print(otp_code) – Hasan Raza Jul 21 '23 at 06:45
  • Edited the question and added code. Didn't work. @HasanRaza – Shishank Jul 21 '23 at 07:13
  • import otpauth secret_key = b'ABCDEFG' totp = otpauth.TOTP(secret_key) # Generate a code for now code: int = totp.generate() str_code: str = str(code) print(str_code) – Hasan Raza Jul 21 '23 at 07:23
  • try this way if its work – Hasan Raza Jul 21 '23 at 07:23
  • Generated otp does not matches with the Authenticator application at any time and the otp fails to signin as well. @HasanRaza – Shishank Jul 21 '23 at 07:32
  • then there must be an issue such as Incorrect Secret Key ,Clock Drift issue, or may be invalid implementation or may be encoding issue – Hasan Raza Jul 21 '23 at 07:43
  • Thanks for the help. @HasanRaza Found the issue. The issue was in the time. There was approx. 1 min. difference. When I checked earlier, the minute was same but that was just instance. Later I found that the time is not synchronized properly. – Shishank Jul 21 '23 at 07:51
  • I am mentioning the issue in answer let mark it done – Hasan Raza Jul 21 '23 at 08:05

1 Answers1

0

there must be an issue such as Incorrect Secret Key ,Clock Drift issue, or may be invalid implementation or may be encoding issue

Hasan Raza
  • 424
  • 3
  • 9