-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLogicGuard.h
More file actions
112 lines (92 loc) · 2.62 KB
/
LogicGuard.h
File metadata and controls
112 lines (92 loc) · 2.62 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
#ifndef LOGICGUARD_H
#define LOGICGUARD_H
#include <iostream>
#include <string>
#include <vector>
#include <random>
#include <conio.h>
using namespace std;
#define _S(str) LG::XorStr<sizeof(str)>(str)
namespace LG {
inline char _key() {
static random_device rd;
static mt19937 gen(rd());
static uniform_int_distribution<> dis(1, 255);
return static_cast<char>(dis(gen));
}
template <size_t N>
struct XorStr {
char data[N];
char key;
constexpr XorStr(const char(&str)[N]) : data{}, key(0) {
key = static_cast<char>(N * 33);
for (size_t i = 0; i < N; ++i) {
data[i] = str[i] ^ (key + i);
}
}
};
class String {
private:
vector<char> buffer;
char key;
void toggle() {
for (auto& c : buffer) c ^= key;
}
public:
// Constructor for Compile-Time Obfuscated Strings
template <size_t N>
String(const XorStr<N>& obs) {
key = _key();
buffer.reserve(N - 1);
for (size_t i = 0; i < N - 1; ++i) {
buffer.push_back(obs.data[i] ^ (obs.key + i));
}
toggle();
}
// Constructor for Runtime Secure Input
String(const vector<char>& raw) {
key = _key();
buffer = raw;
toggle();
}
~String() {
if (!buffer.empty()) {
volatile char* p = buffer.data();
for (size_t i = 0; i < buffer.size(); ++i) p[i] = 0;
}
}
string str() {
toggle();
string s(buffer.begin(), buffer.end());
toggle();
return s;
}
friend ostream& operator<<(ostream& os, String& s) {
return os << s.str();
}
};
static String Input(bool mask = true) {
vector<char> raw;
raw.reserve(64);
char ch;
while ((ch = _getch()) != '\r') {
if (ch == '\b') {
if (!raw.empty()) {
if (mask) cout << "\b \b";
raw.pop_back();
}
} else {
raw.push_back(ch);
if (mask) cout << '*';
}
}
cout << endl;
String secure(raw);
// Wipe traces
volatile char* p = raw.data();
for (size_t i = 0; i < raw.capacity(); ++i) p[i] = 0;
raw.clear();
return secure;
}
}
#endif