-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
237 lines (211 loc) · 4.59 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "src/impl.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
#include <memory>
#include <iostream>
#include <cstring>
void SetStdinEcho(bool enable = true)
{
#ifdef _WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
if( !enable )
mode &= ~ENABLE_ECHO_INPUT;
else
mode |= ENABLE_ECHO_INPUT;
SetConsoleMode(hStdin, mode );
#else
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
if( !enable )
tty.c_lflag &= ~ECHO;
else
tty.c_lflag |= ECHO;
(void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#endif
}
std::string GetUserPassword(bool needConfirmation)
{
SetStdinEcho(false);
std::string password1, password2;
if (needConfirmation)
{
while (true)
{
std::cout << "Enter the password phrase:\n";
std::cin >> password1;
std::cout << "Confirm the password phrase:\n";
std::cin >> password2;
if (password1 != password2)
{
std::cout << "Password phrases does not match! Try again\n";
}
else
{
break;
}
}
}
else
{
std::cout << "Enter the password phrase:\n";
std::cin >> password1;
}
SetStdinEcho(true);
std::cout << "\nGood choice!\n";
return password1;
}
bool Parse(int argc, char *argv[], int& command, Arguments& args)
{
if (argc < 2)
return false;
if (strcmp(argv[1], "sign") == 0)
{
if (argc != 3 && argc != 5)
return false;
args.m_filePath = argv[2];
bool prKeyProvided = false;
if (strcmp(argv[3], "--pr") == 0)
{
args.m_privateKeyPath = argv[4];
prKeyProvided = true;
}
else if (strcmp(argv[3], "--l") == 0)
{
args.m_numberOfDigits = atoi(argv[4]);
}
else
{
return false;
}
if (args.m_numberOfDigits < 6)
{
std::cout << "Number of digits is too small, 6 will be used!\n";
args.m_numberOfDigits = 6;
}
args.m_password = GetUserPassword(!prKeyProvided);
prKeyProvided ? command = 2 : command = 1;
}
else if (strcmp(argv[1], "verify") == 0)
{
command = 3;
if (argc != 5)
return false;
args.m_filePath = argv[2];
args.m_signatureFilePath = argv[3];
args.m_publicKeyPath = argv[4];
}
else if (strcmp(argv[1], "generate") == 0)
{
command = 4;
if (argc != 2 && argc != 4)
return false;
if (argc == 4)
{
args.m_numberOfDigits = atoi(argv[3]);
if (args.m_numberOfDigits < 6)
{
std::cout << "Number of digits is too small, 6 will be used!\n";
args.m_numberOfDigits = 6;
}
}
args.m_password = GetUserPassword(true);
}
else if (strcmp(argv[1], "help") != 0)
{
return false;
}
return true;
}
std::string Execute(const int& command, Arguments& args)
{
auto cmd = std::unique_ptr<ICommand>(CreateCommand(command, args));
auto result = cmd->Do();
return result;
}
int main(int argc, char *argv[])
{
if (argc > 1)
{
try
{
int commandName = 0;
Arguments args;
if (Parse(argc, argv, commandName, args))
{
const auto result = Execute(commandName, args);
std::cout << result;
}
else
{
std::cout << "Wrong commang!\nType \"ds help\" to get help";
}
}
catch (std::exception& e)
{
std::cout << "Error:\t" << e.what() << '\n';
}
}
else // Dialog mode (temporary)
{
std::string cmd;
std::cout << "Dialog mode started. Enter the command:\n";
std::getline(std::cin, cmd);
while (cmd != "exit")
{
if (cmd.empty())
{
std::getline(std::cin, cmd);
continue;
}
int argc = 1;
char **argv = new char*[10];
int i = 0;
std::string cur = "";
while (i < cmd.size())
{
while (i < cmd.size() && cmd[i] != ' ' && cmd[i] != '\t')
{
cur += cmd[i++];
}
argv[argc] = new char[cur.size() + 1];
std::copy(cur.c_str(), cur.c_str() + cur.size(), argv[argc]);
argv[argc][cur.size()] = 0;
argc++;
while (i < cmd.size() && cmd[i] == ' ' || cmd[i] == '\t')
++i;
cur = "";
}
try
{
int commandName = 0;
Arguments args;
if (Parse(argc, argv, commandName, args))
{
const auto result = Execute(commandName, args);
std::cout << result;
}
else
{
std::cout << "Wrong commang!\nType \"ds help\" to get help\n";
}
}
catch (std::exception& e)
{
std::cout << "Error:" << e.what() << '\n';
}
for (int i = 1; i < argc; ++i)
delete argv[i];
delete[]argv;
std::cout << "\nEnter next command or 'exit' to exit\n";
std::getline(std::cin, cmd);
}
}
std::cout << '\n';
return 0;
}