Skip to content

Commit 812f9fd

Browse files
added support for 8 digit tokens (#2)
1 parent 1f36c56 commit 812f9fd

File tree

1 file changed

+54
-15
lines changed

1 file changed

+54
-15
lines changed

ui/totp.hpp

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace menu {
3636

3737
char delta_buf[32] = {};
3838
char epoch_buf[12] = {};
39-
char current_token_buf[7] = {};
40-
char next_token_buf[7] = {};
39+
char current_token_buf[16] = {};
40+
char next_token_buf[16] = {};
4141
char seconds_left_buf[7] = {};
4242

4343
constexpr static uint32_t step_size = 2;
@@ -86,6 +86,32 @@ namespace menu {
8686
// current entry
8787
uint32_t current = 0;
8888

89+
/**
90+
* @brief Get get the token based on the entry and the current time
91+
*
92+
* @param entry
93+
* @param current
94+
* @return uint32_t
95+
*/
96+
uint32_t get_token(const storage::entry entry, const klib::time::s current) {
97+
// check the amount of digits we should return
98+
switch (entry.digits) {
99+
case storage::digit::digits_6:
100+
return klib::crypt::totp<hash, 6>::hash(
101+
reinterpret_cast<const uint8_t*>(entry.key.data()),
102+
entry.key.size(), current.value, 30, 0
103+
);
104+
case storage::digit::digits_8:
105+
return klib::crypt::totp<hash, 8>::hash(
106+
reinterpret_cast<const uint8_t*>(entry.key.data()),
107+
entry.key.size(), current.value, 30, 0
108+
);
109+
}
110+
111+
// for all the other formats return 0
112+
return 0;
113+
}
114+
89115
public:
90116
totp():
91117
current(0)
@@ -168,21 +194,26 @@ namespace menu {
168194

169195
if (totp_changed) {
170196
// update the tokens
171-
const auto current_token = klib::crypt::totp<hash, 6>::hash(
172-
reinterpret_cast<const uint8_t*>(entries[current].key.data()),
173-
entries[current].key.size(), last_epoch.value, 30, 0
174-
);
197+
const uint32_t current_token = get_token(entries[current], last_epoch);
175198

199+
// copy the token to the buffer and set the width
200+
// based on the amount of digits
176201
klib::string::itoa(current_token, current_token_buf);
177-
klib::string::set_width(current_token_buf, 6, '0');
202+
klib::string::set_width(current_token_buf,
203+
static_cast<uint8_t>(entries[current].digits), '0'
204+
);
178205

179-
const auto next_token = klib::crypt::totp<hash, 6>::hash(
180-
reinterpret_cast<const uint8_t*>(entries[current].key.data()),
181-
entries[current].key.size(), last_epoch.value + 30, 30, 0
206+
// get the next token
207+
const uint32_t next_token = get_token(
208+
entries[current], last_epoch + klib::time::s(30)
182209
);
183210

211+
// copy the token to the buffer and set the width
212+
// based on the amount of digits
184213
klib::string::itoa(next_token, next_token_buf);
185-
klib::string::set_width(next_token_buf, 6, '0');
214+
klib::string::set_width(next_token_buf,
215+
static_cast<uint8_t>(entries[current].digits), '0'
216+
);
186217
}
187218

188219
// get the delta as a string
@@ -202,15 +233,15 @@ namespace menu {
202233

203234
// draw the entry text with a small font above the token if we have any
204235
if (klib::string::strlen(entries[current].str)) {
205-
// draw the current token using the large font
236+
// draw the title at the top of the screen
206237
screen_base::small_text::template draw<FrameBuffer>(
207238
frame_buffer,
208239
"profile",
209240
klib::vector2i{3, 3} - offset.cast<int32_t>(),
210241
klib::graphics::white
211242
);
212243

213-
// draw the current token using the large font
244+
// draw the profile name below the title
214245
screen_base::large_text::template draw<FrameBuffer>(
215246
frame_buffer,
216247
entries[current].str,
@@ -223,15 +254,23 @@ namespace menu {
223254
screen_base::large_text::template draw<FrameBuffer>(
224255
frame_buffer,
225256
current_token_buf,
226-
klib::vector2i{60, 60} - offset.cast<int32_t>(),
257+
klib::vector2i{
258+
static_cast<int32_t>(
259+
156 - (klib::string::strlen(current_token_buf) * screen_base::large_text::font::width)
260+
), 60
261+
} - offset.cast<int32_t>(),
227262
klib::graphics::white
228263
);
229264

230265
// draw the next token using the small font
231266
screen_base::small_text::template draw<FrameBuffer>(
232267
frame_buffer,
233268
next_token_buf,
234-
klib::vector2i{108, 78} - offset.cast<int32_t>(),
269+
klib::vector2i{
270+
static_cast<int32_t>(
271+
156 - (klib::string::strlen(next_token_buf) * screen_base::small_text::font::width)
272+
), 78
273+
} - offset.cast<int32_t>(),
235274
klib::graphics::white
236275
);
237276

0 commit comments

Comments
 (0)