Skip to content

Instantly share code, notes, and snippets.

@LeoNatan
Last active September 3, 2024 09:37
Show Gist options
  • Save LeoNatan/999282bfc53084f6c70e43b08dddc281 to your computer and use it in GitHub Desktop.
Save LeoNatan/999282bfc53084f6c70e43b08dddc281 to your computer and use it in GitHub Desktop.
Hidden Strings
/**
The MIT License (MIT)
Copyright (c) 2024 Léo Natan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 OR COPYRIGHT HOLDERS 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.
*/
//
// LNHiddenStrings.hh
//
#import <Foundation/Foundation.h>
#include <array>
CF_EXTERN_C_BEGIN
namespace hidden_strings {
extern "C++"
template <size_t N>
struct base64_string : std::array<char, N> {
consteval base64_string(const char (&input)[N]) : base64_string(input, std::make_index_sequence<N>{}) {}
template <size_t... Is>
consteval base64_string(const char (&input)[N], std::index_sequence<Is...>) : std::array<char, N>{ input[Is]... } {}
};
extern "C++"
template <size_t N>
consteval const base64_string<4 * (((N - 1) + 2) / 3) + 1> base64Encode(const char(&input)[N]) {
constexpr char kEncodingTable[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
size_t in_len = N - 1;
char output[4 * (((N - 1) + 2) / 3) + 1] {0};
size_t i = 0;
char *p = const_cast<char *>(output);
for (i = 0; in_len > 2 && i < in_len - 2; i += 3) {
*p++ = kEncodingTable[(input[i] >> 2) & 0x3F];
*p++ = kEncodingTable[((input[i] & 0x3) << 4) |
((int)(input[i + 1] & 0xF0) >> 4)];
*p++ = kEncodingTable[((input[i + 1] & 0xF) << 2) |
((int)(input[i + 2] & 0xC0) >> 6)];
*p++ = kEncodingTable[input[i + 2] & 0x3F];
}
if (i < in_len) {
*p++ = kEncodingTable[(input[i] >> 2) & 0x3F];
if (i == (in_len - 1)) {
*p++ = kEncodingTable[((input[i] & 0x3) << 4)];
*p++ = '=';
} else {
*p++ = kEncodingTable[((input[i] & 0x3) << 4) |
((int)(input[i + 1] & 0xF0) >> 4)];
*p++ = kEncodingTable[((input[i + 1] & 0xF) << 2)];
}
*p++ = '=';
}
return base64_string<4 * (((N - 1) + 2) / 3) + 1>(output);
}
extern "C++"
template <typename T>
CF_INLINE
NSString* decodeHiddenString(T encoded)
{
return [[NSString alloc] initWithData:[[NSData alloc] initWithBase64EncodedString:@(encoded.data()) options:0] encoding:NSUTF8StringEncoding];
}
} //namespace hidden_strings
#define HiddenString(input) hidden_strings::decodeHiddenString(hidden_strings::base64Encode("" input ""))
CF_EXTERN_C_END
//
// test.mm
//
#import "LNHiddenStrings.hh"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"%@", HiddenString("Hello, Secret World!"));
}
return 0;
}
/**
strings output:
➜ strings -a test
SGVsbG8sIFNlY3JldCBXb3JsZCE=
initWithBase64EncodedString:options:
initWithData:encoding:
stringWithUTF8String:
strings output without using HiddenString:
➜ strings -a test
Hello, Secret World!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment