|
1 | 1 | # xorlit |
2 | | -Compile time string literal encryptor |
3 | | -# Example Usage |
| 2 | +Compile time string literal encryptor for C++. |
| 3 | +* [Usage](#usage) |
| 4 | +* [Example](#example) |
| 5 | + |
| 6 | +# Usage |
| 7 | +## XORLIT |
| 8 | +The `XORLIT` macro can be used to create an encrypted string which evaluates to its original contents (e.g. `XORLIT("foo")` would evalute as the string "foo", but be stored as some other encrypted value). As it wraps `xorlit::make_str`, one can pass either one argument, a string literal, or two arguments, a string literal and a key. In the case that one argument is passed, the key used will default to `xorlit::seed`. |
| 9 | +> [!WARNING] |
| 10 | +> Exercise caution when using `XORLIT`, `xorlit::make_str(__VA_ARGS__).rexor()`, or `xorlit::string`s in any context where they are not explicitly stored, as some compilers, such as MSVC, may sporadically encrypt strings non-constexpr-ly depending on the string literal's content or the context of its usage. It may be advisable to search binary contents for any unencrypted string literals in this case. |
| 11 | +
|
| 12 | +## xorlit::seed |
| 13 | +`xorlit::seed` is a `constexpr std::uint_least32_t` which is based on the predefined `__TIME__` macro to generate values. It is the default value used by `xorlit::make_str` and by extension `XORLIT` for the key. Since `xorlit::seed` or `static_cast<char>(xorlit::seed)` may evaluate to 0, which, in such a case, would lead to an unencrypted string, by defining `XORLIT_SEED_STATIC_ASSERT` before including the `xorlit.hpp` header, a static assertion by the compiler will be enabled to check if `xorlit::seed` is equal to 0. |
| 14 | + |
| 15 | +## xorlit::string |
| 16 | +`xorlit::string` is the provided type for storing encrypted strings. A `xorlit::string` should always be decorated with the `constexpr` specifier to ensure it is stored in its encrypted form. |
| 17 | +`xorlit::string` defines the following member functions: |
| 18 | +* `xorlit::string::rexor()`: xors the data of the string and returns it. |
| 19 | +* `xorlit::string::xor_data()`: returns a new `const char*` with a copy of the string's data xored. |
| 20 | +* `xorlit::string::data`: returns the data stored in the string. `const` and non-`const` variants are provided. |
| 21 | +* `xorlit::string::key`: returns the key stored in the string. |
| 22 | +> [!IMPORTANT] |
| 23 | +> Results of `xorlit::string::xor_data()` must be managed manually, whether that be via `delete`, smart pointers, etc. |
| 24 | +
|
| 25 | +# Example |
4 | 26 | ```c++ |
| 27 | +#include <string> |
5 | 28 | #include <iostream> |
6 | | -#include "xorlit.hpp" |
| 29 | +#include <format> |
| 30 | +#define XORLIT_SEED_STATIC_ASSERT // enable compiler check if xorlit::seed is 0 |
| 31 | +#include <xorlit.hpp> |
7 | 32 |
|
8 | 33 | int main() |
9 | 34 | { |
10 | | - std::cout << XORLITSTR("Hello world!"); |
11 | | - return 0; |
| 35 | + std::cout |
| 36 | + << XORLIT("Default, xorlit::seed is passed as the key\n") |
| 37 | + << XORLIT("Or you can use xorlit::seed yourself\n", xorlit::seed + __LINE__) // when passing your own seeds, you should check that they aren't 0 |
| 38 | + << XORLIT("Or you can choose your own key\n", 12) << std::endl; |
| 39 | + |
| 40 | + constexpr auto s = xorlit::make_str("You can manually store a xorlit::string"); // always declare the results of xorlit::make_str constexpr |
| 41 | + const std::string raw(s.data(), decltype(s)::size); |
| 42 | + const char* original_data = s.xor_data(); // results of xorlit::string::xor_data must be manually managed, smart pointers are recommended |
| 43 | + const std::string original(original_data, decltype(s)::size); |
| 44 | + delete[] original_data; |
| 45 | + std::cout << std::format("Raw: {}\nOriginal: {}", raw, original) << std::endl; |
12 | 46 | } |
13 | | -``` |
| 47 | + |
| 48 | +////////////////// Possible output: ////////////////// |
| 49 | +// Default, xorlit::seed is passed as the key |
| 50 | +// Or you can use xorlit::seed yourself |
| 51 | +// Or you can choose your own seed |
| 52 | +// |
| 53 | +// Raw: ?%p31>p=1>%1<<)p#$?"5p1p(?"<9$jj#$"9>7P |
| 54 | +// Original: You can manually store a xorlit::string |
| 55 | +``` |
0 commit comments