-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (130 loc) · 3.9 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
//TODO: Incoerenza nell'import export non criptato. Attualmente se faccio un export e lo importo potrebbe non funzionare
// se decido di esportare senza spazi. (ma forse questo è anche normale)
//TODO: Implementare backup automatici?
//TODO: Implementare nei backup criptati la presenza di un hash della password stessa.
#include <assert.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <unistd.h>
#include "header.hpp"
using namespace std;
string privateKey;
vector <entry*> Entries;
void savePrivateKeyHash() {
ofstream passOut(privateKeyHashPath.c_str());
assert( passOut!=NULL );
passOut << hashPassword( privateKey );
passOut.close();
}
void save() {
ofstream out(passwordListPath.c_str());
out << Entries.size() << "\n";
for(int i=0;i<(int)Entries.size();i++) {
entry* ee=new entry();
*ee=*(Entries[i]);
ee->enc( privateKey );
out << ee->place << " " << ee->user << " " << ee->pass << "\n";
}
out.close();
}
void setPrivateKey( string s ) {
privateKey=s;
savePrivateKeyHash();
save();
}
void init () {
destroy();
cout << "This is the first time you execute password-keeper.\n\n";
choosePrivateKey();
ofstream listOut(passwordListPath.c_str());
listOut << "0\n";
listOut.close();
cout << "Password-keeper initialized\n" ;
}
void readAllPass( ) {
ifstream in(passwordListPath.c_str());
int entryNumber;
in >> entryNumber;
for(int i=0;i<entryNumber;i++) {
entry* newOne=new entry();
in >> newOne->place >> newOne->user >> newOne->pass;
newOne->dec( privateKey );
Entries.push_back(newOne);
}
in.close();
}
int login () {
privateKey=hiddenQuestionForm("Password:");
//DEBUG
if( privateKey == "destroy" ) {
destroy();
cout << "All saved passwords have been deleted.\n";
return -1;
}
ifstream in(privateKeyHashPath.c_str());
string correctHash;
in >> correctHash;
in.close();
string hashedKey = hashPassword( privateKey );
if ( hashedKey != correctHash ) {
cout << "Password is incorrect, try again.\n";
return 0;
}
cout << "Login successful.\n";
return 1;
}
int main(){
initPath();
if( access(mainFolderPath.c_str() , F_OK) != 0 ) init();
else {
cout << "Type the password to login, otherwise type 'destroy' to delete all saved password and quit.\n";
int response;
while( 1 ){
response= login();
if( response != 0 ) break;
}
if( response == -1 ) return 0;
}
readAllPass();
while(1) {
string command = questionForm(">");
if( command == "add" ) add();
else if( command == "remove" ) remove();
else if( command == "get" ) retrieve();
else if( command == "getAll" ) retrieveAll();
else if( command == "quit" ) {
save();
return 0;
}
else if( command == "import" ) importPasswords();
else if( command == "export" ) exportPasswords();
else if( command == "changeKey" ) changePrivateKey();
else if( command == "destroy" ) {
bool desBool=decisionForm("Are you sure you want to delete all your saved password and configuration?");
if( desBool ) {
destroy();
cout << "Everything has been deleted.\n";
return 0;
}
}
else if( command == "help" ) {
cout << "This are all the possible commands:\n";
cout << "\tadd\t: add an entry to the password list\n";
cout << "\tremove\t: remove an entry from the password list\n";
cout << "\tget\t: get an entry from the password list\n";
cout << "\tgetAll\t: get all entries from the password list\n";
cout << "\tquit\t: quit the program\n";
cout << "\timport\t: import the list of passwords\n";
cout << "\texport\t: export the list of passwords\n";
cout << "\tchangeKey\t: change the private key used for the encryption\n";
cout << "\tdestroy\t: destroy the password list and any configuration\n";
cout << "\thelp\t: gives help about the usage of password-keeper\n";
}
else {
cout << "Command doesn't exist, for help about the usage type help.\n";
}
}
}