0

I'm currently working on a two-factor authentication system I have write some program to generate base32 secrete key and using that secrete key program will generate new time based otp in every 30 sec using same secrete key.I want to implement this on badger2040 device using micropython.

https://github.com/nehadubey10/TOTP_GENERATION, I have used this code to implement totp on badger2040. When i used same code to implement on windows or linux system it will generate the same otp using that key which we are generating. I have tested weather my program or application are generating same code using same secret key for that I have used this site https://totp.danhersam.com/ . But when i try to implement on badger2040 it is generating different otp using same secret key, can any on tell me the reason.

Neha
  • 1
  • 1

1 Answers1

0

A little late to the game I know, but this might help others.

Possible cause for out of sync code generation

From what I understand, OTP uses a synchronised time to generate the code, and a quick glance at your code shows that you don't set the time before using it in otp(), thus your generated code on the 2040 is based on the default epoch (IIRC that's Jan 1st 2021 00:00:00.00 by default on a RPI pico).

Setting the time with an RTC object

To set the time you can use micropython's machine RTC class to set the time like so (reproduced from linked doc):

rtc = machine.RTC()
rtc.datetime((2020, 1, 21, 2, 10, 32, 36, 0))
print(rtc.datetime())

(2020, 1, 21, 2, 10, 32, 36, 0) is a tuple of length 8 with the following format:

(year, month, day of month, timezone, hour, minutes, seconds, miliseconds)

Caveat

Note that if you don't have a battery backed RTC module like a DS3231 or a DS1307, it means the rp2040 will lose track of the time as soon as it looses power and it will have to be set again.
An alternative to using a battery backed RTC module is have the rp2040's RTC automatically synced when plugged via USB either with a python script or alternatively, by using rshell, which automatically sets the date/time, e.g:

$ rshell exit
Connecting to /dev/ttyACM0 (buffer-size 512)...
Trying to connect to REPL  connected
Retrieving sysname ... rp2
Testing if sys.stdin.buffer exists ... Y
Retrieving root directories ... 
Setting time ... Dec 22, 2022 16:02:28
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 1970
FiguredIT
  • 21
  • 2