xorstr C++ Library

repository·master·Indexed 23 days ago

https://github.com/justasmasiulis/xorstr

A vectorized C++17 compile-time string encryption library designed to hide string literals from static analysis by embedding them directly into code. It provides the xorstr and xorstr_ macros for encryption and decryption, and the xor_string struct API for managing encrypted data. Supports Clang 5.0+, GCC 7.1+, and MSVC v141+, with optional AVX support via JM_XORSTR_DISABLE_AVX_INTRINSICS.

Tokens
879
Snippets
3
Records
5
Agent score
31%

What's inside xorstr

  1. Quick start with xorstr

    master

    To encrypt a string at compile time, wrap your string literal in the xorstr_ macro. This macro creates an encrypted instance and immediately returns a decrypted pointer ready for use (e.g., with std::puts).

    int main() {
        std::puts(xorstr_("an extra long hello_world"));
    }
  2. Use the xorstr and xorstr_ macros

    master

    The library provides two primary macros for string encryption:

    • xorstr(string): Creates an encrypted xor_string instance. This instance holds the encrypted data and requires manual decryption via .crypt_get() or .crypt() to access the plaintext.
    • xorstr_(string): A convenience macro that performs both encryption and immediate decryption, returning a pointer to the decrypted string.
    // This macro creates an encrypted xor_string string instance.
    #define xorstr(string) xor_string<...>{string}
    
    // For convenience sake there is also a macro to instantly decrypt the string
    #define xorstr_(string) xorstr(string).crypt_get()
  3. The xor_string struct API

    master

    The xor_string<CharType, ...> struct manages the encrypted data. Key methods include:

    • size(): Returns the string size in characters (excluding the null terminator).
    • crypt(): Runs the encryption/decryption algorithm on the internal storage in-place.
    • get(): Returns a const_pointer or pointer to the storage without modifying it.
    • crypt_get(): Runs crypt() and returns a pointer to the decrypted internal storage.
    struct xor_string<CharType, ...> {
        using size_type     = std::size_t;
        using value_type    = CharT;
        using pointer       = value_type*;
        using const_pointer = const value_type*;
        
        // Returns string size in characters, not including null terminator.
        constexpr size_type size() const;
        
        // Runs the encryption/decryption algorithm on the internal storage.
        void crypt() noexcept;
        
        // Returns const pointer to the storage, without doing any modifications to it.
        const_pointer get() const;
        
        // Returns non const pointer to the storage, without doing any modifications to it.
        pointer get();
    
        // Runs crypt() and returns the pointer to the internal storage.
        pointer crypt_get();
    }