diff --git a/src/xorstring.h b/src/xorstring.h new file mode 100644 index 0000000..0351aff --- /dev/null +++ b/src/xorstring.h @@ -0,0 +1,87 @@ +#pragma once +// https://stackoverflow.com/questions/7270473/compile-time-string-encryption +//-------------------------------------------------------------// +// "Malware related compile-time hacks with C++11" by LeFF // +// You can use this code however you like, I just don't really // +// give a shit, but if you feel some respect for me, please // +// don't cut off this comment when copy-pasting... ;-) // +//-------------------------------------------------------------// + +//////////////////////////////////////////////////////////////////// +template struct EnsureCompileTime { + enum : int { + Value = X + }; +}; +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +//Use Compile-Time as seed +#define Seed ((__TIME__[7] - '0') * 1 + (__TIME__[6] - '0') * 10 + \ + (__TIME__[4] - '0') * 60 + (__TIME__[3] - '0') * 600 + \ + (__TIME__[1] - '0') * 3600 + (__TIME__[0] - '0') * 36000) +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +constexpr int LinearCongruentGenerator(int Rounds) { + return 1013904223 + 1664525 * ((Rounds> 0) ? LinearCongruentGenerator(Rounds - 1) : Seed & 0xFFFFFFFF); +} +#define Random() EnsureCompileTime::Value //10 Rounds +#define RandomNumber(Min, Max) (Min + (Random() % (Max - Min + 1))) +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +template struct IndexList {}; +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +template struct Append; +template struct Append, Right> { + typedef IndexList Result; +}; +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +template struct ConstructIndexList { + typedef typename Append::Result, N - 1>::Result Result; +}; +template <> struct ConstructIndexList<0> { + typedef IndexList<> Result; +}; +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +const char XORKEY = static_cast(RandomNumber(0, 0xFF)); +constexpr char EncryptCharacter(const char Character, int Index) { + return Character ^ (XORKEY + Index); +} + +template class CXorString; +template class CXorString > { +private: + char Value[sizeof...(Index) + 1]; +public: + constexpr CXorString(const char* const String) + : Value{ EncryptCharacter(String[Index], Index)... } {} + + char* decrypt() { + for(unsigned t = 0; t < sizeof...(Index); t++) { + Value[t] = Value[t] ^ (XORKEY + t); + } + Value[sizeof...(Index)] = '\0'; + return Value; + } + + char* get() { + return Value; + } +}; +#define XorS(X, String) CXorString::Result> X(String) +#define XorString( String ) ( CXorString::Result>( String ).decrypt() ) +////////////////////////////////////////////////////////////////////