Add refrerence symbolic strings

This commit is contained in:
Michael Mikovsky
2026-02-20 18:17:11 -07:00
parent 55d2deef1c
commit 689c4ac714
12 changed files with 153 additions and 96 deletions
+1 -2
View File
@@ -6,8 +6,7 @@ authors.workspace = true
include.workspace = true
[features]
default = ["obfuscate_none"]
obfuscate_none = []
default = []
obfuscate_aes = []
obfuscate_ref = []
+4 -2
View File
@@ -2,8 +2,10 @@ const ENV_KEY_NAME: &str = "OBFUSCATION_KEY";
const BACKUP_ENV_KEY: &str = "OBFUSCATION_KEY_DO_NOT_USE";
pub fn get_encryption_key() -> String {
std::env::var(ENV_KEY_NAME).unwrap_or({
if let Ok(key) = std::env::var(ENV_KEY_NAME) {
key
} else {
println!("Using default encryption key!");
BACKUP_ENV_KEY.to_owned()
})
}
}
-3
View File
@@ -14,9 +14,6 @@ pub fn xor(input: TokenStream) -> TokenStream {
return TokenStream::from(quote! { String::new() });
}
// --- Obfuscated Branch Logic ---
// This code runs at compile-time
let str_bytes = original_str.as_bytes();
let len = str_bytes.len();
+2 -2
View File
@@ -30,7 +30,7 @@ macro_rules! delete {
};
}
#[cfg(feature = "obfuscate_none")]
#[cfg(all(not(feature = "obfuscate_aes"), not(feature = "obfuscate_ref")))]
pub mod proc_impl {
use proc_macro::TokenStream;
use syn::{LitStr, parse_macro_input};
@@ -61,6 +61,6 @@ pub mod proc_impl {
unwrap_string!(xor);
delete!(junk_asm);
unwrap_string!(sym);
passtrough!(sym, crate::symbolic_ref::sym_ref);
unwrap_string!(sym_fn);
}
+47
View File
@@ -1 +1,48 @@
use std::collections::HashMap;
use base62::{Base62, hash};
use proc_macro::TokenStream;
use quote::quote;
use syn::{LitStr, parse_macro_input};
use crate::env::get_encryption_key;
static mut SYM_COUNTER: Vec<String> = Vec::new();
#[allow(static_mut_refs)]
pub fn get_symbol_number() -> usize {
unsafe { SYM_COUNTER.len() }
}
#[allow(static_mut_refs)]
pub fn get_symbol(text: String) -> usize {
unsafe {
if let Some(n) = SYM_COUNTER.iter().position(|r| r == &text) {
n
} else {
SYM_COUNTER.push(text);
SYM_COUNTER.len() - 1
}
}
}
pub fn sym_ref(input: TokenStream) -> TokenStream {
// Parse the input as a string literal
let lit_str = parse_macro_input!(input as LitStr);
let original_name = lit_str.value();
let n = get_symbol(original_name);
let data = base62::encode_usize(n);
let key = hash(&get_encryption_key().as_bytes());
let encoded = format!("_{}_", Base62::encode_full(&data, &key));
println!("Aliased '{}' as '{encoded}'", lit_str.value());
// Expand to a static string literal
TokenStream::from(quote! {
#encoded
})
}