xor_str_encode/lib.rs
1
2//! # XOR 编译期加密字符串并且运行时自动解密
3//!
4//! # XOR encrypts strings at compile time and decrypts them automatically at run time
5//!
6//! 为什么要用这个?
7//!
8//! 原因:项目编译成机器码后,数据库密码或者sql语句等敏感信息,是暴露在机器码中的,
9//!
10//! 如果通过gbk编码强行打开该exe文件,通过搜索"mysql"关键字,即可看到数据库链接信息,包括您的密码
11//!
12//! 通过使用该依赖,就可以隐藏重要文本数据
13//!
14//! Why use this?
15//!
16//! Reason: After the project is compiled into machine code, sensitive information such as database passwords or sql statements are exposed to the machine code
17//!
18//! If you force open the exe file with gbk encoding, you can see the database link information, including your password, by searching for the keyword "mysql"
19//!
20//! By using this dependency, you can hide important text data
21//! # 使用方式 how to use
22//! ```
23//! [dependencies]
24//! xor-str = "*"
25//!
26//! use xor_str::xor;
27//! use xor_str::encode;
28//! use xor_str::decode;
29//! fn main() {
30//! println!("{}",xor!("Hello, world!"));
31//! }
32//! ```
33
34
35///具体的xor加密代码都在这里实现
36use std::ops::Index;
37use syn::{Expr, Lit, parse_macro_input, Token};
38use syn::__private::{ Span, TokenStream, ToTokens};
39use syn::__private::quote::quote;
40use syn::parse::Parser;
41use syn::punctuated::Punctuated;
42
43#[proc_macro]
44pub fn encode(input: TokenStream) -> TokenStream {
45 let punctuated = Punctuated::<Expr, Token![,]>::parse_separated_nonempty.parse(input).unwrap();
46 let mut str = "".to_string();
47 let token_stream = TokenStream::from(punctuated.index(0).into_token_stream());
48 let string = parse_macro_input!(token_stream as Lit);
49 match string {
50 Lit::Str(lit) => {
51 str = lit.value();
52 },
53 Lit::ByteStr(lit) => {
54 str = String::from_utf8(Vec::from(lit.value())).unwrap();
55 }
56 _ => {}
57 };
58 let key = str.len();
59
60 let bytes = str.bytes().collect::<Vec<_>>();
61 let mut encrypted = Vec::with_capacity(bytes.len());
62 for (i, v) in bytes.iter().enumerate() {
63 encrypted.push(v ^ (key + i ) as u8);
64 }
65 let lit = syn::LitByteStr::new(encrypted.as_slice(), Span::call_site());
66 return quote! { #lit }.into()
67}
68
69#[cfg(test)]
70mod tests {
71 #[test]
72 fn it_works() {
73
74 }
75}