/* --------------------------------- ABOUT -------------------------------------\n\nOriginal Author: Adam Yaxley\nWebsite: https://github.com/adamyaxley\nLicense: See end of file\n\nObfuscate\nGuaranteed compile-time string literal obfuscation library for C++14\n\nUsage:\nPass string literals into the AY_OBFUSCATE macro to obfuscate them at compile\ntime. AY_OBFUSCATE returns a reference to an ay::obfuscated_data object with the\nfollowing traits:\n\t- Guaranteed obfuscation of string\n\tThe passed string is encrypted with a simple XOR cipher at compile-time to\n\tprevent it being viewable in the binary image\n\t- Global lifetime\n\tThe actual instantiation of the ay::obfuscated_data takes place inside a\n\tlambda as a function level static\n\t- Implicitly convertable to a char*\n\tThis means that you can pass it directly into functions that would normally\n\ttake a char* or a const char*\n\nExample:\nconst char* obfuscated_string = AY_OBFUSCATE("Hello World");\nstd::cout << obfuscated_string << std::endl;\n\n----------------------------------------------------------------------------- /\n\n#pragma once\n\n// Workaround for LINE not being constexpr when /ZI (Edit and Continue) is enabled in Visual Studio\n// See: https://developercommunity.visualstudio.com/t/-line-cannot-be-used-as-an-argument-for-constexpr/195665\n#ifdef _MSC_VER\n\t#define AY_CAT(X,Y) AY_CAT2(X,Y)\n\t#define AY_CAT2(X,Y) X##Y\n\t#define AY_LINE int(AY_CAT(LINE,U))\n#else\n\t#define AY_LINE LINE\n#endif\n\n#ifndef AY_OBFUSCATE_DEFAULT_KEY\n\t// The default 64 bit key to obfuscate strings with.\n\t// This can be user specified by defining AY_OBFUSCATE_DEFAULT_KEY before \n\t// including obfuscate.h\n\t#define AY_OBFUSCATE_DEFAULT_KEY ay::generate_key(AY_LINE)\n#endif\n\nnamespace ay\n{\n\tusing size_type = unsigned long long;\n\tusing key_type = unsigned long long;\n\n\t// Generate a pseudo-random key that spans all 8 bytes\n\tconstexpr key_type generate_key(key_type seed)\n\t{\n\t // Use the MurmurHash3 64-bit finalizer to hash our seed\n\t key_type key = seed;\n\t key ^= (key >> 33);\n\t key = 0xff51afd7ed558ccd;\n\t key ^= (key >> 33);\n\t key = 0xc4ceb9fe1a85ec53;\n\t key ^= (key >> 33);\n\n\t // Make sure that a bit in each byte is set\n\t key |= 0x0101010101010101ull;\n\n\t return key;\n\t}\n\n\t// Obfuscates or deobfuscates data with key\n\tconstexpr void cipher(char data, size_type size, key_type key)\n\t{\n\t // Obfuscate with a simple XOR cipher based on key\n\t for (size_type i = 0; i < size; i++)\n\t {\n\t data[i] ^= char((key >> ((i % 8) * 8)) & 0xFF);\n\t }\n\t}\n\n\t// Obfuscates a string at compile time\n\ttemplate <size_type N, key_type KEY>\n\tclass obfuscator\n\t{\n\tpublic:\n\t // Obfuscates the string 'data' on construction\n\t constexpr obfuscator(const char data)\n\t {\n\t // Copy data\n\t for (size_type i = 0; i < N; i++)\n\t {\n\t m_data[i] = data[i];\n\t }\n\n\t // On construction each of the characters in the string is\n\t // obfuscated with an XOR cipher based on key\n\t cipher(m_data, N, KEY);\n\t }\n\n\t constexpr const char data() const\n\t {\n\t return &m_data[0];\n\t }\n\n\t constexpr size_type size() const\n\t {\n\t return N;\n\t }\n\n\t constexpr key_type key() const\n\t {\n\t return KEY;\n\t }\n\n\tprivate:\n\n\t char m_data[N]{};\n\t};\n\n\t// Handles decryption and re-encryption of an encrypted string at runtime\n\ttemplate <size_type N, key_type KEY>\n\tclass obfuscated_data\n\t{\n\tpublic:\n\t obfuscated_data(const obfuscator<N, KEY>& obfuscator)\n\t {\n\t // Copy obfuscated data\n\t for (size_type i = 0; i < N; i++)\n\t {\n\t m_data[i] = obfuscator.data()[i];\n\t }\n\t }\n\n\t ~obfuscated_data()\n\t {\n\t // Zero m_data to remove it from memory\n\t for (size_type i = 0; i < N; i++)\n\t {\n\t m_data[i] = 0;\n\t }\n\t }\n\n\t // Returns a pointer to the plain text string, decrypting it if\n\t // necessary\n\t operator char*()\n\t {\n\t decrypt();\n\t return m_data;\n\t }\n\n\t // Manually decrypt the string\n\t void decrypt()\n\t {\n\t if (m_encrypted)\n\t {\n\t cipher(m_data, N, KEY);\n\t m_encrypted = false;\n\t }\n\t }\n\n\t // Manually re-encrypt the string\n\t void encrypt()\n\t {\n\t if (!m_encrypted)\n\t {\n\t cipher(m_data, N, KEY);\n\t m_encrypted = true;\n\t }\n\t }\n\n\t // Returns true if this string is currently encrypted, false otherwise.\n\t bool is_encrypted() const\n\t {\n\t return m_encrypted;\n\t }\n\n\tprivate:\n\n\t // Local storage for the string. Call is_encrypted() to check whether or\n\t // not the string is currently obfuscated.\n\t char m_data[N];\n\n\t // Whether data is currently encrypted\n\t bool m_encrypted{ true };\n\t};\n\n\t// This function exists purely to extract the number of elements 'N' in the\n\t// array 'data'\n\ttemplate <size_type N, key_type KEY = AY_OBFUSCATE_DEFAULT_KEY>\n\tconstexpr auto make_obfuscator(const char(&data)[N])\n\t{\n\t return obfuscator<N, KEY>(data);\n\t}\n}\n\n// Obfuscates the string 'data' at compile-time and returns a reference to a\n// ay::obfuscated_data object with global lifetime that has functions for\n// decrypting the string and is also implicitly convertable to a char*\n#define AY_OBFUSCATE(data) AY_OBFUSCATE_KEY(data, AY_OBFUSCATE_DEFAULT_KEY)\n\n// Obfuscates the string 'data' with 'key' at compile-time and returns a\n// reference to a ay::obfuscated_data object with global lifetime that has\n// functions for decrypting the string and is also implicitly convertable to a\n// char*\n#define AY_OBFUSCATE_KEY(data, key) \n\t -> ay::obfuscated_data<sizeof(data)/sizeof(data[0]), key>& { \n\t static_assert(sizeof(decltype(key)) == sizeof(ay::key_type), "key must be a 64 bit unsigned integer"); \n\t static_assert((key) >= (1ull << 56), "key must span all 8 bytes"); \n\t constexpr auto n = sizeof(data)/sizeof(data[0]); \n\t constexpr auto obfuscator = ay::make_obfuscator<n, key>(data); \n\t thread_local auto obfuscated_data = ay::obfuscated_data<n, key>(obfuscator); \n\t return obfuscated_data; \n\t}()\n\n/* -------------------------------- LICENSE ------------------------------------\n\nPublic Domain (http://www.unlicense.org)\n\nThis is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or distribute this\nsoftware, either in source code form or as a compiled binary, for any purpose,\ncommercial or non-commercial, and by any means.\n\nIn jurisdictions that recognize copyright laws, the author or authors of this\nsoftware dedicate any and all copyright interest in the software to the public\ndomain. We make this dedication for the benefit of the public at large and to\nthe detriment of our heirs and successors. We intend this dedication to be an\novert act of relinquishment in perpetuity of all present and future rights to\nthis software under copyright law.\n\nTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF\nCONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n----------------------------------------------------------------------------- */

Obfuscate: C++14 Compile-Time String Literal Obfuscation Library

原文地址: https://www.cveoy.top/t/topic/pxA3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录