LicenseSpring C++ SDK
Easily add Software Licensing to your application
Loading...
Searching...
No Matches
EncryptString.h
Go to the documentation of this file.
1#ifndef LS_ENCRYPT_STRING_H
2#define LS_ENCRYPT_STRING_H
3
4// -----------------------------------------------------------------------------------------------------------------
5// This code provided as is, without any guarantees.
6// Credit to
7// https://stackoverflow.com/questions/4102320/c-how-to-encrypt-strings-at-compile-time/56847099#56847099
8// https://creativecommons.org/licenses/by-sa/4.0/
9// -----------------------------------------------------------------------------------------------------------------
10
11#include <iostream>
12
13namespace LicenseSpring
14{
15
16#if 1
17// Convert __TIME__ == "hh:mm:ss" to a sum of seconds this gives a compile-time seed
18#define TBX_XSTR_SEED \
19 ((__TIME__[7] - '0') * 1ull + (__TIME__[6] - '0') * 10ull + (__TIME__[4] - '0') * 60ull + \
20 (__TIME__[3] - '0') * 600ull + (__TIME__[1] - '0') * 3600ull + \
21 (__TIME__[0] - '0') * 36000ull)
22#else
23// Use constant seed
24#define TBX_XSTR_SEED (3600ull)
25#endif
26
27// -----------------------------------------------------------------------------
28
29// return a pseudo random number clamped at 0xFFFFFFFF
30constexpr unsigned long long linear_congruent_generator(unsigned rounds)
31{
32 return 1013904223ull + (1664525ull * ((rounds > 0) ? linear_congruent_generator(rounds - 1)
33 : (TBX_XSTR_SEED))) %
34 0xFFFFFFFF;
35}
36
37// -----------------------------------------------------------------------------
38
39#define Random_LCG() linear_congruent_generator(10)
40#define XSTR_RANDOM_NUMBER(Min, Max) (Min + (Random_LCG() % (Max - Min + 1)))
41
42// -----------------------------------------------------------------------------
43
44constexpr const unsigned long long XORKEY = XSTR_RANDOM_NUMBER(0, 0xFF);
45
46// -----------------------------------------------------------------------------
47
48template <typename Char> constexpr Char encrypt_character(const Char character, int index)
49{
50 return character ^ static_cast<Char>(XORKEY + index);
51}
52
53// -----------------------------------------------------------------------------
54
55template <unsigned size, typename Char> class Xor_string
56{
57public:
58 const unsigned _nb_chars = (size - 1);
59 Char _string[size];
60
61 // If everything goes right this constructor should be executed at compile time
62 inline constexpr Xor_string(const Char *string) : _string{}
63 {
64 for (unsigned i = 0u; i < size; ++i)
65 _string[i] = encrypt_character<Char>(string[i], i);
66 }
67
68 // This is executed at runtime
69 const Char *decrypt()
70 {
71 Char *string = _string;
72 for (unsigned t = 0; t < _nb_chars; t++)
73 string[t] = string[t] ^ static_cast<Char>(XORKEY + t);
74 string[_nb_chars] = '\0';
75 return string;
76 }
77};
78
79} // namespace LicenseSpring
80
81// -----------------------------------------------------------------------------
82
83#define XorS(name, my_string) \
84 constexpr LicenseSpring::Xor_string<(sizeof(my_string) / sizeof(char)), char> name(my_string)
85#define XorString(my_string) \
86 [] { \
87 constexpr LicenseSpring::Xor_string<(sizeof(my_string) / sizeof(char)), char> expr( \
88 my_string); \
89 return expr; \
90 }() \
91 .decrypt()
92
93#define XorWS(name, my_string) \
94 constexpr LicenseSpring::Xor_string<(sizeof(my_string) / sizeof(wchar_t)), wchar_t> name( \
95 my_string)
96#define XorWideString(my_string) \
97 [] { \
98 constexpr LicenseSpring::Xor_string<(sizeof(my_string) / sizeof(wchar_t)), wchar_t> expr( \
99 my_string); \
100 return expr; \
101 }() \
102 .decrypt()
103
112#define EncryptStr(s) XorString(s)
113
122#define EncryptWStr(s) XorWideString(s)
123
124#endif // LS_ENCRYPT_STRING_H
#define XSTR_RANDOM_NUMBER(Min, Max)
#define TBX_XSTR_SEED
constexpr Xor_string(const Char *string)
constexpr Char encrypt_character(const Char character, int index)
constexpr unsigned long long linear_congruent_generator(unsigned rounds)
constexpr const unsigned long long XORKEY