Skip to content

Commit 8366c48

Browse files
committed
improvement
1 parent d2e418c commit 8366c48

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

include/omath/containers/encrypted_variable.hpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,70 +108,81 @@ namespace omath
108108
{
109109
template<class T>
110110
class VarAnchor;
111+
111112
template<class T, std::size_t key_size, std::array<std::uint8_t, key_size> key>
112113
class EncryptedVariable final
113114
{
114-
bool m_is_encrypted;
115-
T m_data;
115+
using value_type = std::remove_cvref_t<T>;
116+
117+
bool m_is_encrypted{};
118+
value_type m_data{};
116119

117120
OMATH_FORCE_INLINE constexpr void xor_contained_var_by_key()
118121
{
119-
std::span bytes{reinterpret_cast<std::uint8_t*>(&m_data), sizeof(m_data)};
122+
// Safe, keeps const-correctness, and avoids reinterpret_cast issues
123+
auto bytes = std::as_writable_bytes(std::span<value_type, 1>{&m_data, 1});
120124

121-
for (size_t i = 0; i < bytes.size(); ++i)
122-
bytes[i] ^= static_cast<std::uint8_t>(key[i % key.size()] + (i * key_size));
123-
m_is_encrypted = true;
125+
for (std::size_t i = 0; i < bytes.size(); ++i)
126+
{
127+
const std::uint8_t k = static_cast<std::uint8_t>(key[i % key_size] + (i * key_size));
128+
bytes[i] ^= static_cast<std::byte>(k);
129+
}
124130
}
125131

126132
public:
127-
OMATH_FORCE_INLINE constexpr explicit EncryptedVariable(const T& data): m_is_encrypted(false), m_data(data)
133+
OMATH_FORCE_INLINE constexpr explicit EncryptedVariable(const value_type& data)
134+
: m_is_encrypted(false), m_data(data)
128135
{
129136
encrypt();
130137
}
138+
131139
[[nodiscard]] constexpr bool is_encrypted() const
132140
{
133141
return m_is_encrypted;
134142
}
143+
135144
OMATH_FORCE_INLINE constexpr void decrypt()
136145
{
137146
if (!m_is_encrypted)
138147
return;
139148
xor_contained_var_by_key();
140149
m_is_encrypted = false;
141150
}
151+
142152
OMATH_FORCE_INLINE constexpr void encrypt()
143153
{
144154
if (m_is_encrypted)
145155
return;
146156
xor_contained_var_by_key();
147157
m_is_encrypted = true;
148158
}
149-
[[nodiscard]]
150-
OMATH_FORCE_INLINE constexpr T& value()
159+
160+
[[nodiscard]] OMATH_FORCE_INLINE constexpr value_type& value()
151161
{
152162
return m_data;
153163
}
154-
[[nodiscard]]
155-
OMATH_FORCE_INLINE constexpr const T& value() const
164+
[[nodiscard]] OMATH_FORCE_INLINE constexpr const value_type& value() const
156165
{
157166
return m_data;
158167
}
159-
OMATH_FORCE_INLINE ~EncryptedVariable()
168+
169+
constexpr OMATH_FORCE_INLINE ~EncryptedVariable()
160170
{
161171
decrypt();
162172
}
163-
[[nodiscard]]
164-
OMATH_FORCE_INLINE auto drop_anchor()
173+
174+
[[nodiscard]] constexpr OMATH_FORCE_INLINE auto drop_anchor()
165175
{
166176
return VarAnchor{*this};
167177
}
168178
};
179+
169180
template<class EncryptedVarType>
170181
class VarAnchor
171182
{
172183
public:
173184
// ReSharper disable once CppNonExplicitConvertingConstructor
174-
OMATH_FORCE_INLINE constexpr VarAnchor(EncryptedVarType& var): m_var(var) // NOLINT(*-explicit-constructor)
185+
OMATH_FORCE_INLINE constexpr VarAnchor(EncryptedVarType& var): m_var(var)
175186
{
176187
m_var.decrypt();
177188
}

0 commit comments

Comments
 (0)