Skip to content

Commit

Permalink
Support u8 string
Browse files Browse the repository at this point in the history
  • Loading branch information
dolag233 committed Dec 27, 2023
1 parent 674a53f commit 1ac08ec
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions obfuscate.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ namespace ay
cipher(m_data, N, KEY);
}

// Obfuscates the string 'data' on construction
constexpr obfuscator(const char8_t* data)
{
// Copy data
for (size_type i = 0; i < N; i++)
{
m_data[i] = data[i];
}

// On construction each of the characters in the string is
// obfuscated with an XOR cipher based on key
cipher(m_data, N, KEY);
}

constexpr const char* data() const
{
return &m_data[0];
Expand Down Expand Up @@ -148,6 +162,14 @@ namespace ay
return m_data;
}

// Returns a pointer to the plain text string, decrypting it if
// necessary
operator char8_t*()
{
decrypt();
return reinterpret_cast<char8_t*>(m_data);
}

// Manually decrypt the string
void decrypt()
{
Expand Down Expand Up @@ -191,6 +213,14 @@ namespace ay
{
return obfuscator<N, KEY>(data);
}

// This function exists purely to extract the number of elements 'N' in the
// array 'data'
template <size_type N, key_type KEY = AY_OBFUSCATE_DEFAULT_KEY>
constexpr auto make_obfuscator(const char8_t(&data)[N])
{
return obfuscator<N, KEY>(data);
}
}

// Obfuscates the string 'data' at compile-time and returns a reference to a
Expand Down

0 comments on commit 1ac08ec

Please sign in to comment.