-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseUser.h
47 lines (39 loc) · 1.19 KB
/
BaseUser.h
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
#include <cstring>
#include <iostream>
#include <openssl/sha.h>
#include <ostream>
#include <unistd.h>
const int PW_BUFFER_SIZE = 4;
// Adjust based on system architecture if needed (e.g. 8 for 64-bit).
const int MEM_ADDRESS_SIZE = sizeof(void *);
class BaseUser {
public:
BaseUser(const char *name, const char *pw) {
strcpy(this->name, name);
SHA256((const unsigned char *)pw, strlen(pw), this->password_hash);
}
BaseUser(int id, int cr, const char *n) {
ID = id;
credit = cr;
strcpy(name, n);
}
virtual void gainAccess() { std::cout << "Access granted." << std::endl; }
virtual void denyAccess() { std::cout << "Access denied." << std::endl; }
int get_credit() { return this->credit; }
void buy(int cost) { this->credit -= cost; }
void checkAccess(const char *pw) {
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256((const unsigned char *)pw, strlen(pw), hash);
if (memcmp(hash, password_hash, SHA256_DIGEST_LENGTH) == 0) {
gainAccess();
} else {
denyAccess();
}
}
private:
void set_credit(int credit) { this->credit = credit; }
char name[100];
unsigned char password_hash[SHA256_DIGEST_LENGTH];
int ID;
int credit;
};