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