CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EncryptedString.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Hash.hpp"
11#include "String.hpp"
12
13namespace CeresEngine {
14
15 template<typename Sequence> class EncryptedString;
16 template<std::size_t... Index> class EncryptedString<std::index_sequence<Index...>> {
17 private:
18 mutable char mValue[sizeof...(Index) + 1];
19 mutable bool mEncrypted = true;
20
21 public:
22 constexpr EncryptedString(StringView plainString) noexcept : mValue{encryptCharacter(plainString[Index], Index)...} {}
23
24 [[nodiscard]] StringView decrypt() const noexcept {
25 if(mEncrypted) {
26 for(std::size_t t = 0; t < sizeof...(Index); t++) {
27 mValue[t] = mValue[t] ^ (XORKEY + t);
28 }
29 mValue[sizeof...(Index)] = '\0';
30 mEncrypted = false;
31 }
32
33 return mValue;
34 }
35
36 private:
37 template<typename T> static constexpr T linearCongruentGenerator(T rounds) noexcept {
38 return 1013904223 + 1664525 * ((rounds > 0) ? linearCongruentGenerator(rounds - 1) : staticHash32(StringView(__TIME__)) & 0xFFFFFFFF);
39 }
40
41 template<typename T> static constexpr T random() noexcept { return linearCongruentGenerator(10); }
42 template<typename T> static constexpr T randomNumber(T minimum, T maximum) noexcept { return minimum + (random<T>() % (maximum - minimum + 1)); }
43
44 static const char XORKEY = randomNumber<int>(0, 0xFF);
45 static constexpr char encryptCharacter(const char character, const std::size_t index) noexcept { return character ^ (XORKEY + index); }
46 };
47
48#define CE_ENCRYPTED_STRING(X, String) ::CeresEngine::EncryptedString<std::make_index_sequence<sizeof(String) - 1>> X(String);
49
50} // namespace CeresEngine
Definition EncryptedString.hpp:15
Definition Application.hpp:19
constexpr UInt32 staticHash32(BasicStringView< T > string, const UInt32 value=0x811c9dc5) noexcept
Definition Hash.hpp:77
BasicStringView< char > StringView
Narrow string view used for handling narrow encoded text in UTF-8.
Definition String.hpp:190
@ Index
Index buffer type.
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668