forked from hexne/NeLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringEncrypt.cppm
58 lines (46 loc) · 1.08 KB
/
StringEncrypt.cppm
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
module;
#include <iostream>
#include <string>
#include "tools.h"
export module StringEncrypt;
export
NAMESPACE_BEGIN(nl)
constexpr size_t GetKey() {
#define TO_INT(ch1,ch2) (((ch1)-'0')*10 + ((ch2)-'0'))
// 00:00:00\
0 1 : 3 4 : 6 7
return TO_INT(__TIME__[6], __TIME__[7])
+ TO_INT(__TIME__[3], __TIME__[4])
+ TO_INT(__TIME__[0], __TIME__[1]);
#undef TO_INT
}
constexpr char Encrypt(const unsigned char ch, const size_t key) {
return ch ^ key;
}
char Decrypt(const unsigned char ch, const size_t key) {
return ch ^ key;
}
template<std::size_t N>
struct StringEncrypt {
char s[N]{};
size_t key = GetKey();
constexpr explicit StringEncrypt(const char (&str)[N]) {
for (int i = 0; i < N; ++i) {
s[i] = Encrypt(str[i],key);
}
};
explicit operator std::string () const {
std::string ret(s);
for (int i = 0; i < N; ++i) {
ret[i] = Decrypt(ret[i], key);
}
return ret;
}
};
NAMESPACE_END(nl)
export NAMESPACE_BEGIN(nl::string_encrypt_literals)
template<nl::StringEncrypt se>
std::string operator""_crypt() {
return se;
}
NAMESPACE_END(nl::string_encrypt_literals)