-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_map.cpp
More file actions
124 lines (106 loc) · 2.5 KB
/
hash_map.cpp
File metadata and controls
124 lines (106 loc) · 2.5 KB
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
#include <iostream>
#include <map>
using namespace std;
// initialize map that associates string with a map of string
map<string, map<string, string>> dataAkun;
// hash function
int hashFunction(string password)
{
unsigned long hash = 5381; // initialize prime number
for (char c : password) // loop each character in password
{
hash = ((hash << 5) + hash) ^ static_cast<unsigned long>(c);
// hash << 5: shift binary bits of hash by 5 positions to the left
// + hash: result added to original hash value
// static_cast<unsigned long>(c): convert to ascii
// ^ = XOR
}
return hash;
}
void registerAkun()
{
string username, password, fullname;
cout << "Masukkan Username: " << endl;
cin >> username;
cout << "Masukkan Password: " << endl;
cin >> password;
cout << "Masukkan Nama Lengkap: " << endl;
cin >> fullname;
string hash_key = to_string(hashFunction(password));
map<string, string> dataDetail;
dataDetail["username"] = username;
dataDetail["fullname"] = fullname;
// insert to map
dataAkun[hash_key] = dataDetail;
}
void loginAkun()
{
string username, password;
cout << "Insert Username: " << endl;
cin >> username;
cout << "Insert Password: " << endl;
cin >> password;
string hash_key = to_string(hashFunction(password));
cout << endl;
if (dataAkun.find(hash_key) != dataAkun.end())
{
cout << "Login succeeds!\n"
<< endl
<< endl;
cout << "==== Account Details ====\n"
<< endl;
cout << "Username: " << dataAkun[hash_key]["username"] << endl;
cout << "Fullname: " << dataAkun[hash_key]["fullname"] << endl;
}
else
{
cout << "\nLogin failed!" << endl
<< endl;
}
}
void printAkun()
{
cout << endl;
for (auto i = dataAkun.begin(); i != dataAkun.end(); i++)
{
cout << "Key: " << i->first << "\t"
<< " Username: " << i->second["username"] << " Fullname: " << i->second["fullname"] << endl;
}
}
// Menu
void menu()
{
while (true)
{
int input_user;
cout << endl;
cout << "==== MENU ====" << endl;
cout << "1. Register" << endl;
cout << "2. Login" << endl;
cout << "3. Exit" << endl;
cout << endl;
cout << "\nInsert number from menu: " << endl;
cin >> input_user;
if (input_user == 1)
{
registerAkun();
}
else if (input_user == 2)
{
loginAkun();
}
else if (input_user == 3)
{
printAkun();
break;
}
else
{
cout << "Invalid!" << endl;
}
}
}
int main()
{
menu();
}