-
Notifications
You must be signed in to change notification settings - Fork 1
/
totp_command.cpp
61 lines (52 loc) · 1.61 KB
/
totp_command.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <array>
#include <exception>
#include <time.h>
#include "totp_command_exception.hpp"
#include "totp_command.hpp"
using namespace std;
TOTP_Command::TOTP_Command(std::string otp_name) : name(otp_name) {
if (name.empty()) {
throw TOTP_CMD_Exception("invalid argument");
}
}
std::vector<char> TOTP_Command::get_cmd() {
vector<char> cmd;
string header("TOTP:");
for (auto it = header.begin(); it != header.end(); ++it) {
cmd.push_back(*it);
}
for (auto it = name.begin(); it != name.end(); ++it) {
cmd.push_back(*it);
}
cmd.push_back(',');
std::array<char, 11> time_string_buf;
int64_t unix_time = time(nullptr);
if (unix_time < 0) {
throw std::exception();
}
int time_string_size = sprintf(time_string_buf.data(), "%lu", unix_time);
if (time_string_size < 0) {
throw std::exception();
}
for (auto it = time_string_buf.begin(); it != (time_string_buf.begin() + time_string_size); ++it) {
cmd.push_back(*it);
}
cmd.push_back('\r');
return cmd;
}
void TOTP_Command::parse_answer(std::list<char> answer) {
string answer_str;
for (auto it = answer.begin(); it != answer.end(); ++it) {
answer_str.push_back(*it);
}
auto found = answer_str.find("TOTP:");
if (found != string::npos) {
answer_str.replace(0, string("TOTP:").size(), "");
// TODO: check if result is actually a number
} else {
answer_str = "ERROR parsing answer";
printf("received: %s\n", answer_str.c_str());
// TODO: throw exception?
}
result.set_value(answer_str);
}