63
63
spi = busio .SPI (board .SCK , board .MOSI , board .MISO )
64
64
esp = adafruit_esp32spi .ESP_SPIcontrol (spi , esp32_cs , esp32_ready , esp32_reset )
65
65
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
66
69
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.
71
70
"""
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 ):
79
71
SHA1_BLOCK_SIZE = 64
80
72
KEY_BLOCK = k + (b'\0 ' * (SHA1_BLOCK_SIZE - len (k )))
81
73
KEY_INNER = bytes ((x ^ 0x36 ) for x in KEY_BLOCK )
@@ -126,10 +118,11 @@ def int_to_bytestring(int_val, padding=8):
126
118
return bytes (result )
127
119
128
120
129
- # HMAC -> OTP generator, pretty much same as
130
- # https://github.com/pyotp/pyotp/blob/master/src/pyotp/otp.py
131
-
132
121
def 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
+ """
133
126
if int_input < 0 :
134
127
raise ValueError ('input must be positive integer' )
135
128
hmac_hash = bytearray (
@@ -147,6 +140,15 @@ def generate_otp(int_input, secret_key, digits=6):
147
140
148
141
return str_code
149
142
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 ))
150
152
151
153
print ("===========================================" )
152
154
@@ -180,8 +182,8 @@ def generate_otp(int_input, secret_key, digits=6):
180
182
key_group .append (label_secret )
181
183
182
184
label_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
185
187
label_title .y = 5
186
188
key_group .append (label_title )
187
189
@@ -221,7 +223,7 @@ def generate_otp(int_input, secret_key, digits=6):
221
223
print ("Monotonic time" , mono_time )
222
224
223
225
# 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"
225
227
226
228
# generate buttons
227
229
buttons = []
@@ -230,7 +232,7 @@ def generate_otp(int_input, secret_key, digits=6):
230
232
for i in secrets ['totp_keys' ]:
231
233
button = Button (name = i [0 ], x = btn_x ,
232
234
y = 175 , width = 60 ,
233
- height = 60 , label = i [0 ],
235
+ height = 60 , label = i [0 ]. strip ( " " ) ,
234
236
label_font = font , label_color = BTN_TEXT_COLOR ,
235
237
fill_color = BTN_COLOR , style = Button .ROUNDRECT )
236
238
buttons .append (button )
@@ -258,10 +260,8 @@ def generate_otp(int_input, secret_key, digits=6):
258
260
259
261
# current button state, defaults to first item in totp_keys
260
262
current_button = secrets ['totp_keys' ][0 ][0 ]
261
- # fill the first button
262
263
buttons [0 ].selected = True
263
264
264
-
265
265
while ALWAYS_ON or (countdown > 0 ):
266
266
# Calculate current time based on NTP + monotonic
267
267
unix_time = t - mono_time + int (time .monotonic ())
@@ -296,22 +296,15 @@ def generate_otp(int_input, secret_key, digits=6):
296
296
current_button = name
297
297
# Generate OTP
298
298
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 )
303
300
else :
304
301
b .selected = False
305
302
else :
306
303
for name , secret in secrets ['totp_keys' ]:
307
304
if current_button == name :
308
305
# Generate OTP
309
306
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 )
315
308
# We'll update every 1/4 second, we can hash very fast so its no biggie!
316
309
countdown -= 0.25
317
310
time .sleep (0.25 )
0 commit comments