Advertisement
Brick

XorStr

Dec 23rd, 2015
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #pragma once
  2.  
  3. constexpr int seed =
  4.     static_cast<int>(__TIME__[7]) * 1 +
  5.     static_cast<int>(__TIME__[6]) * 10 +
  6.     static_cast<int>(__TIME__[4]) * 60 +
  7.     static_cast<int>(__TIME__[3]) * 600 +
  8.     static_cast<int>(__TIME__[1]) * 3600 +
  9.     static_cast<int>(__TIME__[0]) * 36000;
  10.  
  11. template < int N >
  12. struct RandomGenerator
  13. {
  14. private:
  15.     static constexpr unsigned a = 16807; // 7^5
  16.     static constexpr unsigned m = 2147483647; // 2^31 - 1
  17.  
  18.     static constexpr unsigned s = RandomGenerator< N - 1 >::value;
  19.     static constexpr unsigned lo = a * (s & 0xFFFF); // Multiply lower 16 bits by 16807
  20.     static constexpr unsigned hi = a * (s >> 16); // Multiply higher 16 bits by 16807
  21.     static constexpr unsigned lo2 = lo + ((hi & 0x7FFF) << 16); // Combine lower 15 bits of hi with lo's upper bits
  22.     static constexpr unsigned hi2 = hi >> 15; // Discard lower 15 bits of hi
  23.     static constexpr unsigned lo3 = lo2 + hi;
  24.  
  25. public:
  26.     static constexpr unsigned max = m;
  27.     static constexpr unsigned value = lo3 > m ? lo3 - m : lo3;
  28. };
  29.  
  30. template <>
  31. struct RandomGenerator<0>
  32. {
  33.     static constexpr unsigned value = seed;
  34. };
  35.  
  36. template <int N, int M>
  37. struct RandomInt
  38. {
  39.     static constexpr unsigned value = RandomGenerator<N + 1>::value % M;
  40. };
  41.  
  42. template <int N>
  43. struct RandomChar
  44. {
  45.     static constexpr char value = char(RandomInt<N, 0xFF>::value);
  46. };
  47.  
  48. template <size_t N, int K>
  49. struct XorString
  50. {
  51. private:
  52.     const char _key;
  53.     char _encrypted[N + 1];
  54.  
  55.     constexpr char encryptc(char c) const
  56.     {
  57.         return c ^ _key;
  58.     }
  59.  
  60.     char decryptc(char c) const
  61.     {
  62.         return c ^ _key;
  63.     }
  64.  
  65. public:
  66.     template < size_t... Is >
  67.     constexpr __forceinline XorString(const char* str, std::index_sequence< Is... >) : _key(RandomChar< K >::value), _encrypted{ encryptc(str[Is])... }
  68.     {
  69.     }
  70.  
  71.     __forceinline char* decrypt(void)
  72.     {
  73.         for (size_t i = 0; i < N; ++i) {
  74.             _encrypted[i] = decryptc(_encrypted[i]);
  75.         }
  76.         _encrypted[N] = '\0';
  77.         return &_encrypted[0];
  78.     }
  79. };
  80.  
  81. #define XorStr(s) ( XorString <sizeof(s) - 1, __COUNTER__>(s, std::make_index_sequence<sizeof( s ) - 1>() ).decrypt() ) // 496 char max, 384 before name truncation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement