Macro obfstr::obfstr

source ·
macro_rules! obfstr {
    ($(let $name:ident = $s:expr;)*) => { ... };
    ($name:ident = $s:expr) => { ... };
    ($buf:ident <- $s:expr) => { ... };
    ($s:expr) => { ... };
}
Expand description

Compiletime string constant obfuscation.

The purpose of the obfuscation is to make it difficult to discover the original strings with automated analysis. String obfuscation is not intended to hinder a dedicated reverse engineer from discovering the original string. This should not be used to hide secrets in client binaries and the author disclaims any responsibility for any damages resulting from ignoring this warning.

The obfstr! macro returns the deobfuscated string as a temporary &str value and must be consumed in the same statement it was used:

use obfstr::obfstr;

const HELLO_WORLD: &str = "Hello 🌍";
assert_eq!(obfstr!(HELLO_WORLD), HELLO_WORLD);

Different syntax forms are supported to reuse the obfuscated strings in outer scopes:

use obfstr::obfstr;

// Obfuscate a bunch of strings
obfstr! {
	let s = "Hello world";
	let another = "another";
}
assert_eq!(s, "Hello world");
assert_eq!(another, "another");

// Assign to an uninit variable in outer scope
let (true_string, false_string);
let string = if true {
	obfstr!(true_string = "true")
}
else {
	obfstr!(false_string = "false")
};
assert_eq!(string, "true");

// Return an obfuscated string from a function
fn helper(buf: &mut [u8]) -> &str {
	obfstr!(buf <- "hello")
}
let mut buf = [0u8; 16];
assert_eq!(helper(&mut buf), "hello");