6363spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
6464esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
6565
66+ def HMAC (k , m ):
67+ """# HMAC implementation, as hashlib/hmac wouldn't fit
68+ From https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
6669
67- def set_backlight (val ):
68- """Adjust the TFT backlight.
69- :param val: The backlight brightness. Use a value between ``0`` and ``1``, where ``0`` is
70- off, and ``1`` is 100% brightness.
7170 """
72- val = max (0 , min (1.0 , val ))
73- board .DISPLAY .auto_brightness = False
74- board .DISPLAY .brightness = val
75-
76- # HMAC implementation, as hashlib/hmac wouldn't fit
77- # From https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
78- def HMAC (k , m ):
7971 SHA1_BLOCK_SIZE = 64
8072 KEY_BLOCK = k + (b'\0 ' * (SHA1_BLOCK_SIZE - len (k )))
8173 KEY_INNER = bytes ((x ^ 0x36 ) for x in KEY_BLOCK )
@@ -126,10 +118,11 @@ def int_to_bytestring(int_val, padding=8):
126118 return bytes (result )
127119
128120
129- # HMAC -> OTP generator, pretty much same as
130- # https://github.com/pyotp/pyotp/blob/master/src/pyotp/otp.py
131-
132121def generate_otp (int_input , secret_key , digits = 6 ):
122+ """ HMAC -> OTP generator, pretty much same as
123+ https://github.com/pyotp/pyotp/blob/master/src/pyotp/otp.py
124+
125+ """
133126 if int_input < 0 :
134127 raise ValueError ('input must be positive integer' )
135128 hmac_hash = bytearray (
@@ -147,6 +140,15 @@ def generate_otp(int_input, secret_key, digits=6):
147140
148141 return str_code
149142
143+ def display_otp_key (name , otp ):
144+ """Updates the displayio labels to display formatted OTP key and name.
145+
146+ """
147+ # display the key's name
148+ label_title .text = name
149+ # format and display the OTP
150+ label_secret .text = "{} {}" .format (str (otp )[0 :3 ], str (otp )[3 :6 ])
151+ print ("OTP Name: {}\n OTP Key: {}" .format (name , otp ))
150152
151153print ("===========================================" )
152154
@@ -180,8 +182,8 @@ def generate_otp(int_input, secret_key, digits=6):
180182key_group .append (label_secret )
181183
182184label_title = Label (font , max_glyphs = 14 )
183- label_title .text = "loading .."
184- label_title .x = ( display . width // 2 ) // 13
185+ label_title .text = " Loading .."
186+ label_title .x = 0
185187label_title .y = 5
186188key_group .append (label_title )
187189
@@ -221,7 +223,7 @@ def generate_otp(int_input, secret_key, digits=6):
221223print ("Monotonic time" , mono_time )
222224
223225# Add buttons to the interface
224- assert len (secrets ['totp_keys' ]) < 6 , "This code can only render 5 keys at a time"
226+ assert len (secrets ['totp_keys' ]) < 6 , "This code can only display 5 keys at a time"
225227
226228# generate buttons
227229buttons = []
@@ -230,7 +232,7 @@ def generate_otp(int_input, secret_key, digits=6):
230232for i in secrets ['totp_keys' ]:
231233 button = Button (name = i [0 ], x = btn_x ,
232234 y = 175 , width = 60 ,
233- height = 60 , label = i [0 ],
235+ height = 60 , label = i [0 ]. strip ( " " ) ,
234236 label_font = font , label_color = BTN_TEXT_COLOR ,
235237 fill_color = BTN_COLOR , style = Button .ROUNDRECT )
236238 buttons .append (button )
@@ -258,10 +260,8 @@ def generate_otp(int_input, secret_key, digits=6):
258260
259261# current button state, defaults to first item in totp_keys
260262current_button = secrets ['totp_keys' ][0 ][0 ]
261- # fill the first button
262263buttons [0 ].selected = True
263264
264-
265265while ALWAYS_ON or (countdown > 0 ):
266266 # Calculate current time based on NTP + monotonic
267267 unix_time = t - mono_time + int (time .monotonic ())
@@ -296,22 +296,15 @@ def generate_otp(int_input, secret_key, digits=6):
296296 current_button = name
297297 # Generate OTP
298298 otp = generate_otp (unix_time // 30 , secret )
299- # display the key's name
300- label_title .text = name
301- # format and display the OTP
302- label_secret .text = "{} {}" .format (str (otp )[0 :3 ], str (otp )[3 :6 ])
299+ display_otp_key (name , otp )
303300 else :
304301 b .selected = False
305302 else :
306303 for name , secret in secrets ['totp_keys' ]:
307304 if current_button == name :
308305 # Generate OTP
309306 otp = generate_otp (unix_time // 30 , secret )
310- #print('OTP: ', otp)
311- # display the key's name
312- label_title .text = name
313- # format and display the OTP
314- label_secret .text = "{} {}" .format (str (otp )[0 :3 ], str (otp )[3 :6 ])
307+ display_otp_key (name , otp )
315308 # We'll update every 1/4 second, we can hash very fast so its no biggie!
316309 countdown -= 0.25
317310 time .sleep (0.25 )
0 commit comments