Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
igozdev committed Jan 7, 2023
1 parent 3d0fb7c commit f1e44d0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# xorlit
# xorlit
Compile time string literal encryptor
# Example Usage
```c++
#include <iostream>
#include "xorlit.hpp"

int main()
{
std::cout << XORLITSTR("Hello world!");
return 0;
}
```
85 changes: 85 additions & 0 deletions include/xorlit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* MIT License
*
* Copyright (c) 2023 IGOZ
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#pragma once

#include <cstdint>

namespace xorlit
{
constexpr uint32_t time =
static_cast<uint32_t>(__TIME__[7] - '0') +
static_cast<uint32_t>(__TIME__[6] - '0') * 10U +
static_cast<uint32_t>(__TIME__[4] - '0') * 100U +
static_cast<uint32_t>(__TIME__[1] - '0') * 1000U +
static_cast<uint32_t>(__TIME__[3] - '0') * 10000U +
static_cast<uint32_t>(__TIME__[0] - '0') * 20000U;

template <size_t Index>
constexpr void XorString(char* d, char key, const char* s)
{
d[Index] = s[Index] ^ key;
XorString<Index - 1>(d, key, s);
}
template <>
constexpr void XorString<0>(char* d, char key, const char* s)
{
d[0] = s[0] ^ key;
}

template <size_t Size>
struct String
{
public:
constexpr String(const char (&s)[Size], char key)
: m_Data(), m_Key(key)
{
XorString<Size - 1>(m_Data, m_Key, s);
}

const char* Xor()
{
for (char& c : m_Data)
c ^= m_Key;
return m_Data;
}

private:
const char m_Key;
char m_Data[Size];
};

template <size_t Size>
constexpr String<Size> CreateString(const char (&s)[Size])
{
return String<Size>(s);
}
template <size_t Size>
constexpr String<Size> CreateString(const char (&s)[Size], char key)
{
return String<Size>(s, key);
}
}

#define XORLITSTR(s) xorlit::CreateString(s, static_cast<char>((xorlit::time + __LINE__ * 100000) % 255)).Xor()

0 comments on commit f1e44d0

Please sign in to comment.