mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 07:03:25 +00:00
improvement
This commit is contained in:
@@ -108,30 +108,39 @@ namespace omath
|
|||||||
{
|
{
|
||||||
template<class T>
|
template<class T>
|
||||||
class VarAnchor;
|
class VarAnchor;
|
||||||
|
|
||||||
template<class T, std::size_t key_size, std::array<std::uint8_t, key_size> key>
|
template<class T, std::size_t key_size, std::array<std::uint8_t, key_size> key>
|
||||||
class EncryptedVariable final
|
class EncryptedVariable final
|
||||||
{
|
{
|
||||||
bool m_is_encrypted;
|
using value_type = std::remove_cvref_t<T>;
|
||||||
T m_data;
|
|
||||||
|
bool m_is_encrypted{};
|
||||||
|
value_type m_data{};
|
||||||
|
|
||||||
OMATH_FORCE_INLINE constexpr void xor_contained_var_by_key()
|
OMATH_FORCE_INLINE constexpr void xor_contained_var_by_key()
|
||||||
{
|
{
|
||||||
std::span bytes{reinterpret_cast<std::uint8_t*>(&m_data), sizeof(m_data)};
|
// Safe, keeps const-correctness, and avoids reinterpret_cast issues
|
||||||
|
auto bytes = std::as_writable_bytes(std::span<value_type, 1>{&m_data, 1});
|
||||||
|
|
||||||
for (size_t i = 0; i < bytes.size(); ++i)
|
for (std::size_t i = 0; i < bytes.size(); ++i)
|
||||||
bytes[i] ^= static_cast<std::uint8_t>(key[i % key.size()] + (i * key_size));
|
{
|
||||||
m_is_encrypted = true;
|
const std::uint8_t k = static_cast<std::uint8_t>(key[i % key_size] + (i * key_size));
|
||||||
|
bytes[i] ^= static_cast<std::byte>(k);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OMATH_FORCE_INLINE constexpr explicit EncryptedVariable(const T& data): m_is_encrypted(false), m_data(data)
|
OMATH_FORCE_INLINE constexpr explicit EncryptedVariable(const value_type& data)
|
||||||
|
: m_is_encrypted(false), m_data(data)
|
||||||
{
|
{
|
||||||
encrypt();
|
encrypt();
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] constexpr bool is_encrypted() const
|
[[nodiscard]] constexpr bool is_encrypted() const
|
||||||
{
|
{
|
||||||
return m_is_encrypted;
|
return m_is_encrypted;
|
||||||
}
|
}
|
||||||
|
|
||||||
OMATH_FORCE_INLINE constexpr void decrypt()
|
OMATH_FORCE_INLINE constexpr void decrypt()
|
||||||
{
|
{
|
||||||
if (!m_is_encrypted)
|
if (!m_is_encrypted)
|
||||||
@@ -139,6 +148,7 @@ namespace omath
|
|||||||
xor_contained_var_by_key();
|
xor_contained_var_by_key();
|
||||||
m_is_encrypted = false;
|
m_is_encrypted = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
OMATH_FORCE_INLINE constexpr void encrypt()
|
OMATH_FORCE_INLINE constexpr void encrypt()
|
||||||
{
|
{
|
||||||
if (m_is_encrypted)
|
if (m_is_encrypted)
|
||||||
@@ -146,32 +156,33 @@ namespace omath
|
|||||||
xor_contained_var_by_key();
|
xor_contained_var_by_key();
|
||||||
m_is_encrypted = true;
|
m_is_encrypted = true;
|
||||||
}
|
}
|
||||||
[[nodiscard]]
|
|
||||||
OMATH_FORCE_INLINE constexpr T& value()
|
[[nodiscard]] OMATH_FORCE_INLINE constexpr value_type& value()
|
||||||
{
|
{
|
||||||
return m_data;
|
return m_data;
|
||||||
}
|
}
|
||||||
[[nodiscard]]
|
[[nodiscard]] OMATH_FORCE_INLINE constexpr const value_type& value() const
|
||||||
OMATH_FORCE_INLINE constexpr const T& value() const
|
|
||||||
{
|
{
|
||||||
return m_data;
|
return m_data;
|
||||||
}
|
}
|
||||||
OMATH_FORCE_INLINE ~EncryptedVariable()
|
|
||||||
|
constexpr OMATH_FORCE_INLINE ~EncryptedVariable()
|
||||||
{
|
{
|
||||||
decrypt();
|
decrypt();
|
||||||
}
|
}
|
||||||
[[nodiscard]]
|
|
||||||
OMATH_FORCE_INLINE auto drop_anchor()
|
[[nodiscard]] constexpr OMATH_FORCE_INLINE auto drop_anchor()
|
||||||
{
|
{
|
||||||
return VarAnchor{*this};
|
return VarAnchor{*this};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class EncryptedVarType>
|
template<class EncryptedVarType>
|
||||||
class VarAnchor
|
class VarAnchor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// ReSharper disable once CppNonExplicitConvertingConstructor
|
// ReSharper disable once CppNonExplicitConvertingConstructor
|
||||||
OMATH_FORCE_INLINE constexpr VarAnchor(EncryptedVarType& var): m_var(var) // NOLINT(*-explicit-constructor)
|
OMATH_FORCE_INLINE constexpr VarAnchor(EncryptedVarType& var): m_var(var)
|
||||||
{
|
{
|
||||||
m_var.decrypt();
|
m_var.decrypt();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user