-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserManager.cpp
More file actions
77 lines (58 loc) · 1.69 KB
/
userManager.cpp
File metadata and controls
77 lines (58 loc) · 1.69 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
#include "userManager.h"
#define SALT_SIZE 6
userManager::userManager()
{
}
userManager::~userManager()
{
}
bool userManager::validate(std::string user, std::string pass)
{
//hash pass and store results in pass
std::ifstream userFile(FILE_NAME);
if (userFile.is_open())
{
std::string subString;
while (getline(userFile, subString))
{
std::size_t pos = subString.find(' ');
std::string name = subString.substr(0, pos);
std::string passWord = subString.substr(pos + 1);
//checks if the plaintext password provided when hashed with the stored salt
//results in the same hash as the one that is stored
if (user == name && compare_salted(passWord, pass))
{
return true;
}
}
userFile.close();
}
return false;
}
bool userManager::create(std::string user, std::string pass)
{
for (int x = 0; x < user.length(); x++)
{
if (user[x] == ' ')
{
printf("Error user name may not contain spaces!\n");
return false;
}
}
std::fstream userFile;
userFile.open(FILE_NAME, std::ios_base::app);
if (userFile.is_open())
{
//password is stored as a salt and hash
//so it should look like "[user] [salt]:[hash]"
//the salt and hash will be used for verification instead of
//just the plaintext password
pass = salt_and_hash(pass);
std::string userInfo ="\n" + user + ' ' + pass;
userFile << userInfo;
userFile.close();
return true;
}
printf("user file failed to open");
return false;
}