1use proc_macro::TokenStream;
2use quote::quote;
3use syn::{parse_macro_input, Data, DeriveInput, Fields, Ident, Type};
4
5#[proc_macro_derive(Obfuscate)]
6pub fn derive_obfuscate(input: TokenStream) -> TokenStream {
7 let input = parse_macro_input!(input as DeriveInput);
8 let name = &input.ident;
9 let vis = &input.vis;
10 let obf_name = Ident::new(&format!("Obfuscated{name}"), name.span());
11
12 let data = match &input.data {
13 Data::Struct(s) => s,
14 _ => panic!("Obfuscate can only be derived for structs"),
15 };
16
17 let fields = match &data.fields {
18 Fields::Named(fields) => &fields.named,
19 _ => panic!("Only named fields are supported"),
20 };
21
22 let obf_fields = fields.iter().map(|f| {
23 let name = &f.ident;
24 quote! { #name: (Vec<u8>, [u8; 12]) }
25 });
26
27 let clear_args = fields.iter().map(|f| {
28 let name = &f.ident;
29 let ty = match &f.ty {
30 Type::Path(p) if p.path.is_ident("String") => quote! { &str },
31 Type::Path(p) if p.path.is_ident("u32") => quote! { u32 },
32 Type::Path(p) if p.path.is_ident("u64") => quote! { u64 },
33 Type::Path(p) if p.path.is_ident("i32") => quote! { i32 },
34 Type::Path(p) if p.path.is_ident("i64") => quote! { i64 },
35 Type::Path(p) if p.path.is_ident("bool") => quote! { bool },
36 _ => quote! { &str },
37 };
38 quote! { #name: #ty }
39 });
40
41 let clear_encrypt = fields.iter().map(|f| {
42 let name = &f.ident;
43 match &f.ty {
44 Type::Path(p) if p.path.is_ident("String") => quote! {
45 #name: rustcrypt_core::encrypt_string(#name, &DEFAULT_KEY)
46 },
47 Type::Path(p) if p.path.is_ident("u32") => quote! {
48 #name: rustcrypt_core::encrypt_u32(*#name, &DEFAULT_KEY)
49 },
50 Type::Path(p) if p.path.is_ident("u64") => quote! {
51 #name: rustcrypt_core::encrypt_u64(*#name, &DEFAULT_KEY)
52 },
53 Type::Path(p) if p.path.is_ident("i32") => quote! {
54 #name: rustcrypt_core::encrypt_i32(*#name, &DEFAULT_KEY)
55 },
56 Type::Path(p) if p.path.is_ident("i64") => quote! {
57 #name: rustcrypt_core::encrypt_i64(*#name, &DEFAULT_KEY)
58 },
59 Type::Path(p) if p.path.is_ident("bool") => quote! {
60 #name: rustcrypt_core::encrypt_bool(*#name, &DEFAULT_KEY)
61 },
62 _ => quote! {
63 #name: rustcrypt_core::encrypt_string(#name, &DEFAULT_KEY)
64 },
65 }
66 });
67
68 let decrypt_fields = fields.iter().map(|f| {
69 let name = &f.ident;
70 match &f.ty {
71 Type::Path(p) if p.path.is_ident("String") => quote! {
72 #name: rustcrypt_core::decrypt_string(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
73 },
74 Type::Path(p) if p.path.is_ident("u32") => quote! {
75 #name: rustcrypt_core::decrypt_u32(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
76 },
77 Type::Path(p) if p.path.is_ident("u64") => quote! {
78 #name: rustcrypt_core::decrypt_u64(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
79 },
80 Type::Path(p) if p.path.is_ident("i32") => quote! {
81 #name: rustcrypt_core::decrypt_i32(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
82 },
83 Type::Path(p) if p.path.is_ident("i64") => quote! {
84 #name: rustcrypt_core::decrypt_i64(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
85 },
86 Type::Path(p) if p.path.is_ident("bool") => quote! {
87 #name: rustcrypt_core::decrypt_bool(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
88 },
89 _ => quote! {
90 #name: rustcrypt_core::decrypt_string(&self.#name.0, &self.#name.1, &DEFAULT_KEY)
91 },
92 }
93 });
94
95 let expanded = quote! {
96 use rustcrypt_core::DEFAULT_KEY;
97
98 #[derive(Clone)]
99 #vis struct #obf_name { #(#obf_fields),* }
100
101 impl #obf_name {
102 pub fn new_clear(#(#clear_args),*) -> Self { Self { #(#clear_encrypt),* } }
103 pub fn get_clear(&self) -> #name { #name { #(#decrypt_fields),* } }
104 }
105 };
106
107 TokenStream::from(expanded)
108}