--------------------------------- ABOUT -------------------------------------Original Author Adam YaxleyWebsite httpsgithubcomadamyaxleyLicense See end of fileObfuscateGuaranteed compile-time string l
/* --------------------------------- ABOUT -------------------------------------
Original Author: Adam Yaxley Website: https://github.com/adamyaxley License: See end of file
Obfuscate Guaranteed compile-time string literal obfuscation library for C++14
Usage: Pass string literals into the AY_OBFUSCATE macro to obfuscate them at compile time. AY_OBFUSCATE returns a reference to an ay::obfuscated_data object with the following traits: - Guaranteed obfuscation of string The passed string is encrypted with a simple XOR cipher at compile-time to prevent it being viewable in the binary image - Global lifetime The actual instantiation of the ay::obfuscated_data takes place inside a lambda as a function level static - Implicitly convertable to a char* This means that you can pass it directly into functions that would normally take a char* or a const char*
Example: const char* obfuscated_string = AY_OBFUSCATE("Hello World"); std::cout << obfuscated_string << std::endl;
----------------------------------------------------------------------------- */
#macro AY_CAT(X,Y) AY_CAT2(X,Y) #macro AY_CAT2(X,Y) X+Y #macro AY_LINE int(AY_CAT(LINE,U))
#ifndef AY_OBFUSCATE_DEFAULT_KEY ' The default 64 bit key to obfuscate strings with. ' This can be user specified by defining AY_OBFUSCATE_DEFAULT_KEY before ' including obfuscate.h #macro AY_OBFUSCATE_DEFAULT_KEY ay::generate_key(AY_LINE) #endif
type size_type = unsigned long long type key_type = unsigned long long
' Generate a pseudo-random key that spans all 8 bytes
function generate_key(seed as key_type) as key_type
' Use the MurmurHash3 64-bit finalizer to hash our seed
dim key as key_type = seed
key = key xor (key >> 33)
key = key * 0xff51afd7ed558ccd
key = key xor (key >> 33)
key = key * 0xc4ceb9fe1a85ec53
key = key xor (key >> 33)
' Make sure that a bit in each byte is set
key = key or 0x0101010101010101
return key
end function
' Obfuscates or deobfuscates data with key
sub cipher(data as string ptr, size as size_type, key as key_type)
' Obfuscate with a simple XOR cipher based on key
for i as size_type = 0 to size - 1
data[i] = chr$(asc(data[i]) xor (key >> ((i mod 8) * 8)) and &hFF)
next i
end sub
' Obfuscates a string at compile time
template obfuscator<N as size_type, KEY as key_type>
class obfuscator
public:
' Obfuscates the string 'data' on construction
sub new(data as string ptr)
' Copy data
for i as size_type = 0 to N - 1
m_data[i] = data[i]
next i
' On construction each of the characters in the string is
' obfuscated with an XOR cipher based on key
cipher(m_data, N, KEY)
end sub
function data() as string ptr
return @m_data[0]
end function
function size() as size_type
return N
end function
function key() as key_type
return KEY
end function
private:
dim as string * N m_data
end class
' Handles decryption and re-encryption of an encrypted string at runtime
template obfuscated_data<N as size_type, KEY as key_type>
class obfuscated_data
public:
sub new(obfuscator as obfuscator<N, KEY>)
' Copy obfuscated data
for i as size_type = 0 to N - 1
m_data[i] = obfuscator.data()[i]
next i
end sub
' Returns a pointer to the plain text string, decrypting it if
' necessary
operator function as string ptr
decrypt()
return @m_data[0]
end function
' Manually decrypt the string
sub decrypt()
if m_encrypted then
cipher(m_data, N, KEY)
m_encrypted = false
end if
end sub
' Manually re-encrypt the string
sub encrypt()
if not m_encrypted then
cipher(m_data, N, KEY)
m_encrypted = true
end if
end sub
' Returns true if this string is currently encrypted, false otherwise.
function is_encrypted() as boolean
return m_encrypted
end function
private:
' Local storage for the string. Call is_encrypted() to check whether or
' not the string is currently obfuscated.
dim as string * N m_data
' Whether data is currently encrypted
dim m_encrypted as boolean = true
end class
' This function exists purely to extract the number of elements 'N' in the
' array 'data'
function make_obfuscator(data as const string[N]) as obfuscator<N, KEY>
return new obfuscator<N, KEY>(data)
end function
' Obfuscates the string 'data' at compile-time and returns a reference to a ' ay::obfuscated_data object with global lifetime that has functions for ' decrypting the string and is also implicitly convertable to a char* #define AY_OBFUSCATE(data) AY_OBFUSCATE_KEY(data, AY_OBFUSCATE_DEFAULT_KEY)
' Obfuscates the string 'data' with 'key' at compile-time and returns a
' reference to a ay::obfuscated_data object with global lifetime that has
' functions for decrypting the string and is also implicitly convertable to a
' char*
#define AY_OBFUSCATE_KEY(data, key)
func auto obfuscated_data() as obfuscated_data<sizeof(data)/sizeof(data[0]), key>
static_if(sizeof(key) = sizeof(key_type), "key must be a 64 bit unsigned integer")
static_if(key >= (1ull << 56), "key must span all 8 bytes")
constexpr auto n = sizeof(data)/sizeof(data[0])
constexpr auto obfuscator = ay::make_obfuscator<n, key>(data)
dim shared as obfuscated_data<n, key> obfuscated_data = new obfuscated_data<n, key>(obfuscator)
return obfuscated_data
end func
/* -------------------------------- LICENSE ------------------------------------
Public Domain (http://www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------------- *
原文地址: https://www.cveoy.top/t/topic/hOvQ 著作权归作者所有。请勿转载和采集!