Skip to content

Commit f1e44d0

Browse files
authored
Add files via upload
1 parent 3d0fb7c commit f1e44d0

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
# xorlit
1+
# xorlit
2+
Compile time string literal encryptor
3+
# Example Usage
4+
```c++
5+
#include <iostream>
6+
#include "xorlit.hpp"
7+
8+
int main()
9+
{
10+
std::cout << XORLITSTR("Hello world!");
11+
return 0;
12+
}
13+
```

include/xorlit.hpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2023 IGOZ
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#pragma once
26+
27+
#include <cstdint>
28+
29+
namespace xorlit
30+
{
31+
constexpr uint32_t time =
32+
static_cast<uint32_t>(__TIME__[7] - '0') +
33+
static_cast<uint32_t>(__TIME__[6] - '0') * 10U +
34+
static_cast<uint32_t>(__TIME__[4] - '0') * 100U +
35+
static_cast<uint32_t>(__TIME__[1] - '0') * 1000U +
36+
static_cast<uint32_t>(__TIME__[3] - '0') * 10000U +
37+
static_cast<uint32_t>(__TIME__[0] - '0') * 20000U;
38+
39+
template <size_t Index>
40+
constexpr void XorString(char* d, char key, const char* s)
41+
{
42+
d[Index] = s[Index] ^ key;
43+
XorString<Index - 1>(d, key, s);
44+
}
45+
template <>
46+
constexpr void XorString<0>(char* d, char key, const char* s)
47+
{
48+
d[0] = s[0] ^ key;
49+
}
50+
51+
template <size_t Size>
52+
struct String
53+
{
54+
public:
55+
constexpr String(const char (&s)[Size], char key)
56+
: m_Data(), m_Key(key)
57+
{
58+
XorString<Size - 1>(m_Data, m_Key, s);
59+
}
60+
61+
const char* Xor()
62+
{
63+
for (char& c : m_Data)
64+
c ^= m_Key;
65+
return m_Data;
66+
}
67+
68+
private:
69+
const char m_Key;
70+
char m_Data[Size];
71+
};
72+
73+
template <size_t Size>
74+
constexpr String<Size> CreateString(const char (&s)[Size])
75+
{
76+
return String<Size>(s);
77+
}
78+
template <size_t Size>
79+
constexpr String<Size> CreateString(const char (&s)[Size], char key)
80+
{
81+
return String<Size>(s, key);
82+
}
83+
}
84+
85+
#define XORLITSTR(s) xorlit::CreateString(s, static_cast<char>((xorlit::time + __LINE__ * 100000) % 255)).Xor()

0 commit comments

Comments
 (0)