-
Notifications
You must be signed in to change notification settings - Fork 24
/
str_obfuscator_no_template.hpp
64 lines (54 loc) · 1.17 KB
/
str_obfuscator_no_template.hpp
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
#ifndef STR_OBFUSCATOR_HPP_
#define STR_OBFUSCATOR_HPP_
namespace detail {
struct encryptor {
static constexpr void encrypt(char *dest, const char *str, size_t len, char key) {
for (size_t index = 0; index < len; index++) {
dest[index] = str[index] ^ key;
}
}
};
}
class cryptor
{
public:
class string_encryptor
{
public:
constexpr string_encryptor(const char *str, size_t len) :
_buffer{}, _decrypted{ false }, _key{ static_cast<const char>(len % 255) }, _length{len}
{
detail::encryptor::encrypt(_buffer, str, _length, _key);
}
#ifdef _WIN32
__declspec(noinline)
#elif __GNUC__
__attribute__((noinline))
#endif
const char *decrypt() const {
if (_decrypted) {
return _buffer;
}
for (size_t i = 0; i < _length; i++) {
_buffer[i] ^= _key;
}
_decrypted = true;
return _buffer;
}
char getKey() const {
return _key;
}
const char* getBuffer() const {
return _buffer;
}
private:
mutable char _buffer[64];
mutable bool _decrypted;
const char _key;
size_t _length;
};
static constexpr auto create(const char* str, size_t len) {
return string_encryptor(str, len);
}
};
#endif // STR_OBFUSCATOR_HPP_